Over the years I have developed a “TAIT Style”, or a PHP – HTHL Coding Standard, that has helped me make reading code easier. Recently I was on a forum, answering a question about a WordPress Theme, and included a statement that said something like:
[php]<?php if ( $var == ‘something’ ) { ?>[/php]
if ( $var == 'something' ) {
Someone there told me I was “wrong” (can working PHP be wrong?) and it should be in a style with “Yoda Conditions” like:
[php]<?php if ( ‘something’ == $var ) { ?>[/php]
if ( 'something' == $var ) {
I was actually amused, not only does my wife sometimes refer to me as “Yoda” (the wise and powerful Jedi Master), but because “his” statement was actually the way I would normally code it! I had just got in a hurry in an example on that forum. That person directed me to the WordPress Coding Standards (I have included below). I never really considered that WordPress had a coding standard! To my amazement, they were very near my own coding standards (perhaps I should call it the “Tightly Aligned Integrated Technology Style”, T.A.I.T. Style). I haven’t really tried to put TAIT Style on paper before, but it is similar to the WordPress Coding Standards, with a few exceptions…
For those that did not know the term “Yoda Conditions” is post 1980′s. Prior to that, in the late 1960′s and early 1970′s “TAIT-COMS” was used (Tightly Aligned Integrated Technology – Conditions? Considerations? Connectors?)
I am going to stick with my own coding standard (TAIT Style) as I believe it is better, I think WordPress compromised in a couple areas, with people who are seeking to be cute, or take short cuts… Or who just learned one way, and think because they learned it that way, that is logically the best for everyone! I disagree, but not too loudly.
Anyway here are my few exceptions…
Single and Double Quotes
My rule is slightly different, with basically the same meaning. Always use a single quote, unless you need a double quote. That means you need to learn when a single quote won’t work and when you need a double quote.
Brace Style
Braces should always be used except for a single line like this:
[php]if ( condition )
action1();[/php]
Space Usage
Basically I use one space wherever I can!
Obvious exceptions are before the semicolon “;” at the end of the line, used basically as a period. I do not use a space in the brackets of an empty function. I also do not uses spaces in type casting!
( array ) looks ridiculous to me so I use (array)
No one has a problem with elseif so do not put a space in it!
A place that require no space is before the parenthesis right after the function name.
Ternary Operator
Frankly most people have problems with them so I do not used them. Read the WordPress section “Clever Code” below and let the word “readability” sink in!
elseif
No one has a problem with elseif so do not put the space in it!
Cast to array
One other thing I think is likely good policy is to cast to array before foreach.
Better
This:
[php]foreach ( (array) $array as $var ) {[/php]
Not this:
[php]foreach ( $array as $var ) {[/php]
If you accidentally pass a var instead of an array this code will still run!
Final Thoughts
Well, that pretty much reflects my coding standard. I have learned that by following these virtually everyone accepts my code!
Here now is the rest of the WordPress Coding Standards…

Recent Comments