Lazy Coding In PHP; A Mini Rant

4th January 2011 - 5 minutes read time

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.

Comments

Permalink
You are so correct, I tend to avoid Wordpress as mush as possible simply because of this; and the way designers code templates... so frustrating. I like to follow Zend Framework's coding standard quite closely, and I highly recommend every write their PHP code in a similar way.

Andrew Cobby (Tue, 01/25/2011 - 04:58)

Permalink

I'm glad someone agrees! It's a good point on the template code though. I have seen some really weird formatting and massive white space issues in some themes.

Even in ZF there is need to mix PHP and HTML in one file, and I don't think there is a good way to do this. Take a simple example of a variable that gets printed with a surrounding div element if it has been set within a template. I have seen this done in a number of different ways, but this sort of thing tends to be quite common:

  1. <?php if (is_set($right)) { ?>
  2. <div>
  3. <?php print $right; ?>
  4. </div>
  5. <?php } ?>

This might look a little messy, but how else would you go about adding this? The indentation rules normally used in PHP coding standards kind of break down here as you would introduce odd formatting into the HTML output. I haven't seen any good ideas about this so I have had to invent things as I go along to make the code at least a little bit readable.

Permalink

Personally I find the long form looks needless when a single line statement was all that was required.  Let the person who might modify the code worry about the process of modification.  If a programmer is incapable of adding braces around a code block when then need to expand an inline control structure then they should not be a programmer.

However I do agree with conforming to styles, so if the rules are set they should be followed.

Anonymous (Tue, 08/23/2011 - 14:10)

Permalink
I'm sorry, but is "inline control structure" really the best name we could come up with? Could we not use 'inline function' or 'inline statement'? Hell I'd even go with 'Stop being lazy'. That's just really obscure. Or am I just making mountains? Either way, Anonymous is right - there is a standard and it needs to be used.

MrThatOneFreeman (Sun, 08/12/2012 - 03:50)

Add new comment

The content of this field is kept private and will not be shown publicly.