[wp-hackers] Coding style

Ryan Boren ryan at boren.nu
Tue Jul 4 01:22:08 GMT 2006


Mark Jaquith wrote:
> 1. Tabs instead of spaces for block intents

This is a religious thing for some folks.  I prefer spaces myself, but 
tabs is the WP standard.  So, to head off a long tabs vs. spaces thread, 
tabs is here to stay. :-)

> 3. Use of whitespace around and within said block items:
>     bad.    if('something' == $foo){
>     good.    if ( 'something' == $foo ) {

I'm bad about using the whitespace after the opening paren and before 
the closing paren, so there is quite a bit of code that lacks that 
padding.  Bad Ryan.

> 4. 'something' == $foo, rather than $foo == 'something'

Right, to avoid accidentally doing an assignment.

> 5. If at all possible, omit curly braces.

For my personal code, I prefer to keep the curly braces.  But, most WP 
code omits them so we might as well make it official.  Also, in 
templates we tend to use the colon/endif style, especially when we're 
jumping in and out of <?php tags a lot.

> 6. cast to array before foreach
>     bad.    foreach ( $foo as $bar ) {
>     good.    foreach ( (array) $foo as $bar ) {

Yes, I think we still need to get your patch for this in.

> 7. Spaces between parameters:

Yes, yes, yes.

> 8. Eliminate unnecessary trailing tabs or spaces:

You bet.


Maybe we need a style guide and a best practices guide.  Best practices 
such as use js_escape() when putting arbitrary data in an onclick.

Ryan


More information about the wp-hackers mailing list