[geeklog-devel] Coding standards clarification

Heather Engineering info at heatherengineering.com
Wed May 23 22:03:46 EDT 2007


Hi,

I'm trying to be good lately. Just like to clarify a couple of coding  
standards points. Any feedback welcomed.


/*  A  */

< from http://wiki.geeklog.net/wiki/index.php/Coding_Guidelines and  
PEAR >
Including Code

Anywhere you are unconditionally including a class file, use  
require_once. Anywhere you are conditionally including a class file  
(for example, factory methods), use include_once. Either of these  
will ensure that class files are included only once. They share the  
same file list, so you don't need to worry about mixing them - a file  
included with require_once will not be included again by include_once.

Note: include_once and require_once are statements, not functions.  
Parentheses should not surround the subject filename.

< end snippet >

So which of the following should I be writing?

(1) require_once '../../../lib-common.php';           // no brackets,  
one space before, no space after
(2) require_once('../../../lib-common.php');         // brackets, no  
spaces
(3) require_once( '../../../lib-common.php' );       // brackets, spaces

I would assume (1) from the guidelines, but I see some of (2) and  
lots of (3) in core geeklog code.


/*  B  */

Operators in control structures. Didn't see this anywhere in the  
guidelines, but all examples in the php manual use (1):

(1) if( $groupid == $_CONF['remote_users_group_id'] )      // spaces  
around comparison operator
(2) if( $groupid==$_CONF['remote_users_group_id'] )        // no  
spaces around comparison operator

(1) is easier to read I think, but I see lots of (2) as well.


/*  C  */

Indenting. Never quite sure about this. The following line has to  
wrap. How many spaces should I indent the second line? I would  
normally do about 16 spaces more than the $retval in this case.

$retval .= COM_startBlock($LANG_AR_ADMIN[1], '',
                 COM_getBlockTemplate('_admin_block', 'header'));

But I also do things like this:

         $result = DB_query("SELECT DISTINCT ug_uid
                              FROM {$_TABLES['group_assignments']}
                              WHERE ug_main_grp_id={$rootgroup}
                              ORDER BY ug_uid
                              LIMIT 1");

lining up the SELECT, FROM, WHERE, ORDER, LIMIT. Is this permitted?  
It's easy to read but takes up lots of space. Particularly when it's  
something like this (MySQL which could probably be improved, I'm sure):

     $sql = "SELECT @now:={$iid},
                ( SELECT sortorder
                  FROM   {$_TABLES['doc_item']}
                  WHERE  iid=@now
                  LIMIT 1
                ) AS current,
                ( SELECT did
                  FROM   {$_TABLES['doc_item']}
                  WHERE  iid=@now
                  LIMIT 1
                ) AS currentdid,
                ( SELECT rid
                  FROM   {$_TABLES['doc_item']}
                  WHERE  iid=@now
                  LIMIT 1
                ) AS id,
                ( SELECT iid
                  FROM   {$_TABLES['doc_item']}
                  WHERE  sortorder<current AND rid=id
                  ORDER  by sortorder DESC
                  LIMIT  0,1
                ) AS previid,
                ( SELECT did
                  FROM   {$_TABLES['doc_item']}
                  WHERE  iid=previid
                  LIMIT  1
                ) AS prevdid,
                ( SELECT iid
                  FROM   {$_TABLES['doc_item']}
                  WHERE  sortorder>current AND rid=id
                  ORDER  by sortorder ASC
                  LIMIT  0,1
                ) AS nextiid,
                ( SELECT did
                  FROM   {$_TABLES['doc_item']}
                  WHERE  iid=nextiid
                  LIMIT  1
                ) AS nextdid
             FROM   {$_TABLES['doc_item']}
             ORDER  by sortorder
             LIMIT 1";

Should this kind of thing be avoided?



Cheers,

Euan.



More information about the geeklog-devel mailing list