[wp-hackers] coding standards on if.

Haluk Karamete halukkaramete at gmail.com
Fri Feb 22 17:31:16 UTC 2013


On this page ( http://codex.wordpress.org/WordPress_Coding_Standards ),
there is a section on if statements - which goes like this...

Single line blocks can omit braces for brevity:

if ( condition )
>     action1();


and a few the lines down the page, we also have a section that says


In general, readability is more important than cleverness or brevity.



This matter is of course very subjective. Yes, there is PEAR standards and
there is also the WordPress and in between there are a lot of give and
take.. I get that.
And I understand the logic as to why

if ( condition ) {
    action1();
}

may not be preferred over the none curly bracketed alternative; it's the
brevity that is and it's the "c'mon dude it's a one liner so what's so
complex about it" approach. So I can see that point of view.

But isn't there a contradiction between this approach and
the approach which guides you that "readability is more important than
cleverness or brevity."

To me, the concept of "readability" is more how the readability is now...
As time goes, your code grows and you find yourself trying to read the page
from top to bottom and your eyes are already trained to look for the
closing brackets or the "endif:" to trace where the code structures close.
The closers do help vertically all the time.  So, when we allow a none
closer ( as in the example ), the readability is effected.

Plus, when you need to add more lines under that if condition sometime down
the road, it's no longer it's just an insert anymore, cause in that case
you are forced to add the curly brackets to make it all work.
This kind of approach goes against the wisdom as to why we should have the
final comma (,) just before we close that final associative array element.

I was just wondering.. wouldn't it be better that if the codex were to
suggest that even though the single line if's may look attractive to drop
the curly brackets, it is still better to stick to the curly brackets
methodology all the time and that's all for the sake of consistency with
the rest of the coding standards and readibility and usability down the
road.

That's how I look at it.

Since there are great minds in this list and centuries of coding experience
combined, I wanted to pick your brains and see what would be the counter
argument to my point.


More information about the wp-hackers mailing list