How to write good code – A tip for wordpress developers

Recently I’ve automatically updated my WordPress installation and a few plugins. To my surprise after the update, the site just stopped working. There was a PHP fatal error in one of the plugins. How did this get past QA with the developer?

I began the task of debugging the plugin, because it’s a good plugin and I needed it. Upon seeing the code I was reaching for the hammer to break my computer screen and anything else in sight. This wasn’t the first time I’ve seen a poorly written WordPress plugin or theme. I really hope it’s the last time.

If you are coding for WordPress or really anything on the web for backend or frontend, please read on. I will outline some coding standards that will make your code easy to read, understand and it will be consistent.

Formatting

A lot of developers prefer to use Spaces instead of Tabs. In different editors the tab spacing can be setup differently. To ensure that your code shows correct indenting in any editor, set your editors’ tab setting to use spaces, probably 4 spaces is best.

On the topic of indenting, your code will look so much nicer if you use indenting and nesting properly. Here is an example of ugly formatting:

<?php
    if ($this == $that) {
?>
  <div>This is <strong>bold text</strong></div>
<?php
    } else {
    doSomethingElse();
}

  $myArray = array(10 => array(
'Apples'
), 'Pears');
    ?>

Now wouldn’t this be easier to read:

<?php
    if ($this == $that) {
        ?>
        <div>
            This is <strong>bold text</strong>
        </div>
        <?php
    } else {
        doSomethingElse();
    }

    $myArray = array(
        10 => array('Apples'),
        'Pears'
    );
?>

Note how the HTML is nested. I prefer to see block level tags broken out into a few lines, while inline level tags displayed on the same line, just as the browser intends to render them.

Naming Conventions

One of my biggest pet hates about the WordPress codebase is functions like:

<?php
    _e();
?>

Seriously? What the hell is that meant to be? What does the function do?!

Some people have different styles of naming functions and that’s fine. You can use camelcase and underscores all you like. Just make sure your function is named something that resembles what it does:

<?php
    getDirectoryContents();
    displayForm();
    show_headers();
    _die_with_honor();
?>

PHP Portability

Please please please don’t use short open tags (<? or <?=). There is a PHP setting to disable them, whereas there isn’t one to disable normal open tags. Some people have different settings but if you use the standard method then your code will just work everywhere. Similarly, do not use the <SCRIPT> tags to delimit PHP code.

Other tidbits

  • Comment the hell out of your code if you intend for others to read it.
  • You don’t have to use a closing ?> tag at the end of your document. Sometimes if you are using a closing tag, additional whitespace can be printed to the browser unintentionally. This can cause problems for scripts/classes/includes that are used before print headers.
  • Ternary conditions are handy, but only in small doses. Try not to have nested ternaries or more than one per line.
  • HEREDOC Syntax is not recommended. Most PHP programmers won’t know what it is anyway :)

I could go into more depth with my own personal coding standards but you should be free to create your own. Whatever style you choose to write with, the main point is that it is formatted correctly and readable.

Be Sociable, Share!
This entry was posted by jc on Monday, January 23rd, 2012 at 12:41 pm and is filed under Uncategorized. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

One Comment : Leave a Reply

  1. Denis says:

    Not only in PHP also coding in other languages formatting always matter and is must be easily understandable as you mentioned above. Nice list of points I note down.

Leave a Reply