Editor Settings

  1. Use Tabs. In order to keep all of the code consistant, we will be using only tabs to indent the code. No spaces may be used. If possible, set the tab character width in your editor to be equal to 4 spaces.
  2. Use Unix Line Breaks. Make sure your editor uses Unix line breaks (\n) when saving and not Windows line breaks (\r\n) or Macintosh line breaks (\r).
^ Top

Coding Format

  1. Variable Names. Most variables should follow a specific format: all lowercase, with words separated by an underscore (_). The only exception is a variable representing a class. In this case, the first letter of each word should be capital.
    Examples:
    $total_users = get_user_count();
    $Database = new Database();
    Variable names should be descriptive, but concise. Don't use long sentences; use short phrases.
  2. Loop Indices. Variables used in loop indices are the only variables that can be one character long. Loop indices start at $i and continue advancing through the alphabet for each nested loop.
    Examples:
    for( $i = 0; $i < 3; $i++ )
    {
    	for( $j = 0; $j < 3; $j++ )
    	{
    		$ij = $i + $j;
    	}
    }
  3. Function Names. Function names are named in the same way variables are named. Function names must be descriptive of the purpose of the function, while not being unnecessarily long, and all words must be delimited by an underscore (_). There are not to be any uppercase letters in the function names and no functions should be named using studlyCaps.
    Examples:
    message()
    create_date()
    get_user_count()
  4. Function Arguments. Function arguments should be spaced according to the spacing guidelines mentioned later. All mandatory arguments should be listed first, in a logical order, followed by optional arguments and their default values (spaced accordingly). The variables used as arguments in functions must follow the same guidelines as normal variables.
    Examples:
    get_user_count()
    create_date( $format, $gmepoch, $timezone )
    message( $header, $message, $admin = FALSE, $redirect = FALSE, $url = FALSE )
^ Top

Code Layout

  1. Standard Header. 
    /*******************************************************************
     * Filename:
     * Copyright: © 2003-2004 CMSformE
     * $Id$
     *******************************************************************
     * This program is free software; you can redistribute it and/or modify
     * it under the terms of the GNU General Public License as published by
     * the Free Software Foundation; either version 2 of the License, or
     * (at your option) any later version.
     *
     * This program is distributed in the hope that it will be useful,
     * but WITHOUT ANY WARRANTY; without even the implied warranty of
     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     * GNU General Public License for more details.
     *******************************************************************/
    This header should be added to the beginning of every file. Be sure to fill in the file name in the correct place. If the file is in any directories inside the root directory, be sure to include them in the format of: dir1/dir2/file.ext. The header should have a blank line both above and below it.
    If it is a PHP file, then the <?php can go above the blank line.
    If it is an HTML or template file, abbreviate the header as <!--$Id$--> and put it on its own line, without any blank lines before it, and space the file as necessary.
    If it is a plain text file, it MUST have a blank line before it, even if there isn't anything else there. This includes SQL files, which may have the header commented out differently to conform to the standards of that particular DBMS.
  2. Braces. All conditionals, loops, and function definitions MUST be enclosed in braces. This includes if, elseif, else, while, for, foreach, switch, function, and class. Also, braces always get their own line.
    Examples:
    if( !( empty( $row['post_text_short'] ) ) && $mode != 'more' )
    {
    	/* [...] */
    }
    elseif( $article && $mode == 'more' )
    {
    	/* [...] */
    }
    else
    {
    	/* [...] */
    }
    
    while( $row = $Database->fetch_assoc( $result ) )
    {
    	/* [...] */
    }
    
    for( $i = 0; $i < $blockcount; $i++ ) 
    {
    	/* [...] */
    }
    
    foreach( $row as $colname => $value )
    {
    	/* [...] */
    }
    
    function sid()
    {
    	/* [...] */
    }
    
    class Database
    {
    	/* [...] */
    }
  3. Spaces. All tokens and functions must be spaced properly. Variables should be separated from tokens by one space on each side. Functions should NOT have a space between the function name and the parentheses, but there should be one space on the inside of each parenthesis, except when there aren't any arguments inside or if casting a value. Arguments separated by commas (,) should have one space after each comma (, ). There shouldn't be any spaces between the ending semicolon (;) and the code before it.
    Examples:
    define( 'ROOT', './' );
    $news = array();
    $post_limit = '';
    $article = ( isset( $_GET['article'] ) ) ? (int) $_GET['article'] : '';
  4. Parentheses. Parentheses (()) should always be used when there could be a question about hierarchy. Multiple comparisons on the same line should be separated from each other by parentheses, spaced accordingly. Parentheses should surround a negated statement, with the negation symbol (!) on the outside of the parentheses. This includes the negation of single functions and single variables. All functions, with the exception of print (which is actually a language construct), shall be followed immediately by an open parenthesis. Examples:
    $news = array();
    define( 'ROOT', './' );
    while( ( $file = @readdir( $dir ) ) !== FALSE )
^ Top

Valid XHTML 1.0   Valid CSS   Bobby WorldWide Approved 508   Hosted by SourceForge   Donate to CMSformE

Last updated on: November 25, 2004 8:43PM GMT