Friday, 10 August 2018

Lazy Coding In PHP; A Mini Rant

If there is one thing in the PHP world that really annoys me it's programmers writing what I call "lazy code". This is code that works but takes the least amount of time (generally meaning keystrokes) to create. This is almost always a bad thing as it is difficult to read, hard to change and almost always uncommented. The main problem is that PHP is quite a forgiving and fluid language in that you allows you to write code in a variety of different ways and formats.
The most common problem I have come across with lazy coding is when programmers write if statements. These can be written in a variety of colourful ways, the most common approach I have found is to leave out the curly brackets, like this:
  1. if ($variable==true)
  2. echo 'true';
This works fine, but what happens if you want to add something to the if statement? Taking the same example, lets try to add another line of code to change the valule of the variable.
  1. if ($variable==true)
  2. $variable = false;
  3. echo 'true';
In PHP, only the first instruction of a bracketless if statement if counted as part of the statement, so the code above will always print "true", no matter what the value of the variable is. Taking another example, lets assume that we have an if/else statement like this.
  1. if($variable==1)
  2. echo '1';
  3. elseif($variable==2)
  4. echo '2';
  5. else
  6. echo '3';
If we add another line of code to the if or elseif blocks we are greated with a syntax error as PHP can't match the block below with the block above, try running the following code and you'll see what I mean.
  1. if($variable==1)
  2. echo '1';
  3. elseif($variable==2)
  4. $variable = 3;
  5. echo '2';
  6. else
  7. echo '3';
So, in order to modify these statements we are required to add the curly brackets in, which might not seem like much, but it means adding more code than we originally intended and can lead to syntax errors and broken code very easily when all we wanted to do was make a simple change. I can remember at least one instance where I wanted to modify a single if statement that had 30 or so clauses and rather than add the braces in I decided to write a secondary if statement that allowed me to alter what happened in one section of the original statement. This is bad in terms of maintenence, but it gets much worse if a bunch of bracketless if statements are put together. They are generally written one per line, which makes for very unreadable code.
  1. if ($variable1==1) echo '1';
  2. if ($variable2==2) echo '2';
  3. if ($variable3==3) echo '2'; elseif ($variable4==4) echo '4';
This gets worse if there is a variety of bracketed and unbracketed if statements in the same block. Trying to figure out where one starts and the other ends can be a absolute pain.
When writing code I urge you to write it in a way that is both readable and maintainable, along with appropriate comments. This not only helps others read your code but will allow yourself to tell what you were trying to do 6 months down the line. You should, at the very least, be writing PHP doc comments for your functions and classes.
With PHP coding becomming more professional in general with the advent of things like frameworks lazy coding is starting to become less common. There area a few projects (namely WordPress and other similar open source CMS systems) that still have a lot of this sort of thing. To prevent lazy coding creeping into your own code and projects you can use something like PHP_CodeSniffer to automatically go through your code and let you know about this sort of thing. If you try to leave brackets out of PHP if statements (like I have discussed above) PHP_CodeSniffer will let you know with the following error:
  1. > phpcs test.php
  2.  
  3. FILE: test.php
  4. --------------------------------------------------------------------------------
  5. FOUND 1 WARNING(S) AFFECTING 1 LINE(S)
  6. --------------------------------------------------------------------------------
  7. 2 | WARNING | Inline control structures are discouraged
  8. --------------------------------------------------------------------------------
I would love to hear everyone's thoughts on the subject of lazy coding. Do you have any personal pet hates in terms of PHP coding and what is the worst case of lazy coding you have seen? Or do you think this really doesn't matter as long as the code gets written? Leave a comment below.

0 comments:

Post a Comment