The importance of WordPress coding standards

With the size of the WordPress project and the gigantic ecosystem created by it, it’s important we (as developers) do certain things the same. Following the WordPress coding standards makes us ‘format’ our code all in the same way, while still giving us all the room for our own creativity. In this post I want to highlight some PHP coding standards, if you’re interested in the full list you can view the Handbook page on WordPress PHP coding standards here.

Indentation

While the handbook explains a few things on indentation, for me the most important rule in this chapter is:

Use real tabs and not spaces, as this allows the most flexibility across clients.

One of the most annoying things is opening someone else’s code to find a mess as a result of incorrect indentation.

No Shorthand PHP Tags

I can keep it pretty short on this one. Never use shorthand PHP start tags. Always use full PHP tags. Not sure what shorthand PHP tags are? Here’s a small code snippet:

Ternary Operator

I really like ternary operators, so I’m happy the WordPress Coding standards allow them. There’s a rule for ternary operators though, they must always test if the statement is true. This way the ‘if result’ is always ran when the boolean condition returns true, and the ‘else result’ when false.

Yoda Conditions

I was first introduced to this because of the WordPress coding standards and I loved this rule right away. The handbook states:

When doing logical comparisons, always put the variable on the right side, constants or literals on the left.

If you accidentally forget one equals sign in the above code, you will get a parse error because you can’t assign a value to a constant like true. If you wouldn’t be using Yoda Conditions and would forget an equals sign you’ve possibly just created one of the most hard to track down bugs in your code.

A little bizarre, it is, to read. Get used to it, you will.

I hope this helps any one out reading this. Missing something? Found a problem? Got an improvement? Please let me know in the comments below.

Related Posts

Powered By Related Posts for WordPress
Click Here to Learn More About Related Posts for WordPress

2 thoughts on “The importance of WordPress coding standards

  1. Thanks. I never knew why the Yoda rule was in place until reading this. I thought someone just decided it was a good idea and rolled with it.

Leave a Reply

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