fbpx

Beautiful Code

by | Oct 3, 2018 | PHP | 0 comments

I have been working on researching this very topic the last while. This is such a broad topic with many opinions from so many people. When joining different agencies, I have been given rules to follow every time. There have been times when I argued we should do something one other way because of reason x, but generally I have just adapted to the unique ways each agency has already set.

In PHP, a group called the “PHP Framework Interoperability Group”, made up of volunteers, has constructed a set of guidelines for coding styles. The PSR-2 is a coding style guide that was created to “reduce cognitive friction when scanning code from different authors”. The guidelines includes suggestions like “PHP keywords MUST be in lowercase”, and “you MUST use an indent of 4 spaces, and MUST NOT use tabs for indenting”.

In WordPress (or CodeIgniter or another framework), one rule I have personally been using is when you’re working on a template file, I will not use the curly brace in if else statements or anything, but in classes I will.

For example, in a controller file you might have an index function for the home page. So this is what I will use:

if ( $something ) {} while ( true ) {} for ( blah ) {}

In a theme file, for example index.php, when you have html code in the file, I will use code like this:

if ( $something ): endif; while( true ): endwhile; for ( blah ): endfor;

Making html more readable, we should try and keep as much php away as possible, and if we used MVC frameworks (for example Laravel and CodeIgniter), we should be able to keep as much PHP code away from our views as possible.

Naming your variables

One step you can take while writing beautiful code is to make your code intentions clear. When you’re naming a variable, make sure you name it properly. If you name your variables properly, it will not only help yourself out in the future when you have to look at the code quickly, but it will help new developers find out what your intentions were.

Here’s an example:

$name = “Joe”; //bad example
$userFirstName = “Joe”; // good example

The above variable example is using camel case formatting. A camel case is a variable where there are multiple words in the variable, and the first word is lower case, the rest of the words start with a capital letter. Some frameworks encourage different conventions for variables.

Formatting conventions

WordPress encourages using a different convention. WordPress encourages using underscores to separate words in variables, and to keep everything as lower case.

For more formatting and best practices with PHP when using WordPress, you should always refer to this page https://make.wordpress.org/core/handbook/best-practices/coding-standards/php/.

If we were working with WordPress files, my “good example” would be a bad one also. Here’s what my $userFirstName variable looks like with the WordPress best practice:

$user_first_name = “Joe”; // good example

In my second reference, there are many useful tips for writing beautiful, readable code. Remember, just because something you wrote is clever, does not make it readable or better code. Squishing a section of code onto one line just because it saves lines, does not make it beautiful code. Try to make your functions as short as possible, and don’t squish multiple functionalities into one function. When you name your functions, you should use verbs or verb phrases, for example savePost or addPage instead of just save or add.

Only use one letter variables when they are index variables, and probably that should even be kept to a minimum. For example, $i = 0 or $index = 0.
for ( $i = 0; … ) or you can have for ( $index = 0; … )

Don’t be afraid to add spaces between lines of code to make it more readable. The most important concept to start using while beautifying your code is to be consistent. If you start writing one concept one way, make sure you stick to that way of using that concept throughout the rest of your code. For example, in_array() and array_search both use needle, and haystack in that order in the function.

Here’s an example of the way both functions work: in_array( “example”, $array_one ); array_search( “example”, $array_one );. It would be confusing if one of these functions dealing with searching arrays required the array to be in the first parameter vs the other, it would result in us being inconsistent which makes the code unreadable and confusing.

References:
https://www.php-fig.org/psr/psr-2/
https://medium.com/coding-skills/clean-code-101-meaningful-names-and-functions-bf450456d90c

Written By Carl

Written by Carl Wuensche, a dedicated problem solver with a passion for helping businesses thrive through innovative solutions and strategic development.

Related Posts

WordPress Core Contributor

I’m officially unofficially a WordPress core contributor! Earlier this month I decided that I wanted to contribute to WordPress. The first step I took was joining the WordPress slack channel and I mostly silently (with the odd sentence here and there) observed various...

read more

Single Responsibility Principle

There are many ways of architecting software. There are many ways of organising code. When an architect (in this case Software engineer or programmer) decides to create a plugin, he/she must decide how they’re going to organise the logic of the code. In the Single...

read more

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *