Back to vBulletin 3 Articles

Industry coding standards
by filburt1 27 Apr 2003

Half of these are from NASA's Java style guide that I use at work(http://aaa.gsfc.nasa.gov/Files/JavaFinal.doc).

1. Use whitespace liberally for readability:
Wrong:
PHP Code:
if($somevar==aconstant){dothis();dothat();} 
Right:
PHP Code:
if ($somevar == aconstant)
{
    
dothis();
    
dothat();

2. Braces on newlines:
PHP Code:
if ($result) {
    
dowhatever();

Right:
PHP Code:
if ($result)
{
    
dowhatever();

(this might not look important but in big files it greatly helps)

3. Don't exceed 80 chars per line (for code printing and reading on low-res and effectively-low-res environments). No example because it'll break the layout here, but if a line exceeds 80 chars, break it up a bit before 80, and then continue it on the next line while indenting one tab (NASA suggests two tabs but it can make the code very messy).

4. Use a 4-space (not a tab character) indent.
Wrong (right from vB2...):
PHP Code:
if ($HTTP_POST_VARS['action']=="doupload") {

  if (
$HTTP_POST_FILES['avatarfile']) {
    
$avatarfile $HTTP_POST_FILES['avatarfile']['tmp_name'];
    
$avatarfile_name $HTTP_POST_FILES['avatarfile']['name'];
  }
  
copy("$avatarfile","../$avatarpath/$avatarfile_name");

  
$avatarpath.="/$avatarfile_name";

  echo 
"<p>avatar successfuly uploaded</p>";

  
$HTTP_POST_VARS['action']="insert";

Right:
PHP Code:
if ($HTTP_POST_VARS['action'] == "doupload")
{
    if (
$HTTP_POST_FILES['avatarfile'])
    {
        
$avatarfile $HTTP_POST_FILES['avatarfile']['tmp_name'];
        
$avatarfile_name $HTTP_POST_FILES['avatarfile']['name'];
    }
    
copy("$avatarfile","../$avatarpath/$avatarfile_name");
    
    
$avatarpath .= "/$avatarfile_name";
    echo 
"<p>avatar successfuly uploaded</p>";
    
$HTTP_POST_VARS['action'] = "insert";

5. NEVER use deprecated methods of coding. Examples:
1. $HTTP_POST_VARS
2. $somearray[somenonconstant]
3. mysql_db_query()

Fixed:
1. $_POST
2. $somearray['somenonconstant'] (a very common problem that you need to lose!)
3. mysql_query()

There are more and I don't mean to sound pushy, but every single thing I've said in this post is considered the professional industry standard and if you program for a profession then you'd be expected to follow these.

vblts.ru supports vBulletin®, 2022-2024