Home Practical Tips for Writing Beautiful PHP Code

Enter a search term to find articles.
practical-tips-for-writing-beautiful-php-code
2022-04-30
821

Practical Tips for Writing Beautiful PHP Code

Here are some straight-forward and practical tips on how to make your PHP code beautiful.

Give it some space

As romantically-cliche as it may sound, sometimes all your code needs is a bit of space. A little bit of breathing room so you and your team can easily read it better. And as basic as it may seem, many, both new and experienced developers tend to still forget the idea of adding spaces on their code.

Take a look at these code blocks below...

    public function test_can_upload_avatar()
    {
        $this->user = User::factory()->create();
        $avatar = UploadedFile::fake()->image($filename = 'avatar.jpeg');
        $this->post(route('user.profile.avatar'), ['avatar' => $avatar]);
        $this->assertDatabaseHas('media', [
            'model_type' => $this->user->getMorphClass(),
            'model_id' => $this->user->id,
            'file_name' => $avatar->hashName(),
        ]);
        $this->assertNotNull($this->user->avatar->first());
        $this->assertInstanceOf(Media::class, $this->user->avatar->first());
    }
    public function test_can_upload_avatar()
    {
        $this->user = User::factory()->create();

        $avatar = UploadedFile::fake()->image($filename = 'avatar.jpeg');

        $this->post(route('user.profile.avatar'), ['avatar' => $avatar]);

        $this->assertDatabaseHas('media', [
            'model_type' => $this->user->getMorphClass(),
            'model_id' => $this->user->id,
            'file_name' => $avatar->hashName(),
        ]);

        $this->assertNotNull($this->user->avatar->first());
        $this->assertInstanceOf(Media::class, $this->user->avatar->first());
    }

They are obviously the same piece of code. I don't know about you, but just eye-balling these code blocks, I definitely find the latter much easier to take in and understand because of its ample spacing. Like, I don't feel the need to bring my face closer to the screen of my laptop just to be able to understand what each line of code does, if you know what I mean.

As a rule of thumb, try to put each statement (anything that ends with a semi-colon) on its own line unless 2 or more lines are doing the same type of action (i.e. the assertions on the example above, assigning variables, etc.).

Use PHP CS Fixer

One advantage PHP has over other programming languages is that we have our own set of coding standards that developers generally agree upon. The PHP Standards Recommendation or PSR are set of recommendations for styling our code. From Basic Coding Standard, Coding Style and Autoloading Standards the PSR has you covered so you don't have to invent your own.

Another good thing is that you can automate the implementation of these standards on your IDE using the PHP-CS-Fixer tool. Modern IDEs like Sublime Text, PhpStorm and VS Code have their own packages that you can just download from their respective package managers to set this up. All you need to do is to configure the set of rules that may want to implement for you or your team.

If you have no idea on what rules to implement, here's a link to a gist of set of rules that I personally use in my projects. I have mine set to auto run whenever I save the PHP file so that I am sure that these standards will always apply no matter what.

You can also refer to this PHP-CS-Fixer Cheat Sheet if you want to check on the meaning and effect of each rule.

Leave Meaningful Comments

Another very basic yet many-tend-to-forget way of beautifying their code and helping themselves and/or their teammates understand their code better is by using comments. Not just comments, meaningful comments. Comments that actually makes sense when we you read it.

I personally believe that writing meaningful comments actually reflects how much care a developer have put into their code. This is actually one reason why Laravel framework is so popular. And like-minded developers tend to gravitate towards it, developers who actually care for their code.

Personally, as a rule of thumb, I leave comments on areas that I feel like, if I don't remember the full context of how the code works, it will be quite hard for me to understand it again if I read it 6 months from the time that I wrote it. I also try to follow Laravel's way of writing comments - where each line is a couple of characters shorter the the one above it.

Marvin Quezon

Full Stack Web Developer
Marvin Quezon · Copyright © 2024 · Privacy · Sitemap