Coding Standards


Writing programs should be meaningful and in a long future, if you or anyone looks back at the code, then he/she must understand it. We all write codes, but do we have a standard of writing the code ? If we write codes without any standards and just go for it, then the program would be messy and no other programmer would like to read it.

Here’s what I’m talking about : A guy writes code as he like :

<?
//A function to forget. Just a demo function for showing code standards. Nothing is Nothing.
function forget ($get=false) {
echo $get===true ? "Yes":"No";
}
?>

The above code is messy and doesn’t look good and here’s how it is made better :

<?php
/**
 * A function to forget. Just a demo function for
 * showing code standards. Nothing is Nothing.
 */
function forget($get = false) {
  echo $get === true ? "Yes" : "No";
}
?>

You can see that, now the code looks better and gives a temptation to read it. Not only does it help you, but others who might read the code in the future as well.

This post is about the coding standards that I use and maybe you could use it too ! This coding standards is loosely based on the PEAR coding standards.

The PHP File

On most PHP systems, <?php works perfectly, but on others the short code is not enabled. In this case, <? won’t work. So, starting of PHP code should be by <?php :

<?php
echo "Hello World";
?>

If the file is full PHP, then it need not be closed by ?>. All files should end with a newline (\n) and not Windows newline (\r\n).

Semicolons is important. It should be placed. Do this :

<?php echo "hello"; ?>

and not this :

<?php echo "hello" ?>

Indents & Spacing

The indents should be of 2 spaces and no tabs. Tabs won’t work equally perfect in all text editors. Hence, instead of tabs, whitespaces should be used.

Lines should not have trailing whitespaces at the end. A space should be in between operators such as :

+, =, -, !=, !==, >

etc… Examples :

$myVariable = "myContent";
if(5 < 4) { }

rather than :

$myVariable="myContent";
if(5<4){}

Also, a space after “,” should be made.

Control Structures

Control structures include if, for, while, switch, etc… Here is an example of if statement :

if (5 > 4 || 9 > 8) {
  doAction1();
}elseif ("a" == "a") {
  doAction2();
}else{
  doAction3();
}

Always use elseif and not else if. For siwtch statements :

switch (condition) {
  case 1:
    action1;
    break;

  case 2:
    action2;
    break;

  default:
    defaultaction;
}

Lines

All lines should not exceed 80 characters. If it does, make the overflowing chars into next line :

if($array['this']['that']['here']['pen'] ==
$array['there']['that']['myBigHat']['insideAPen']) {
  doAction1();
}

This applies to comments too :

/**
 * A function to forget. Just a demo function for
 * showing code standards. Nothing is Nothing.
 */

There might be a need to extend from 80 characters if it is a control structure. In this case, we can on one condition : it must be understandable.

Another thing is that all lines that are kept out for prettiness mustn’t have any characters, even whitespaces.

Variables & Functions

As said before, a space should be in between ‘=’ in variables :

$var = "content";
$var .= "content";

In case of variables that are together of different sizes, adding more space will help to read it better :

$name                 = "";
$fullName             = "";
$aLitttleLongVariable = ""; 

In the case of functions :

myFunction($var1, $var2);

Functions should return meaningful data. It’s better to avoid NULL as return and instead use boolean false. Use :

function myFunction() { return true; }

and not :

function myFunction() { return "true"; }

or :

function myFunction() { return TRUE; }

Arrays

The items of arrays should be separated with whitespace and inbetween “=>” :

$myArray = array('hello', 'world', 'me' => 'him');

It can be also made more pretty by :

$myArray = array(
 'hello' => 'a',
 'world' => 'b',
 'me' => 'him'
);

Quotes

It’s better to use double quotes (“) in PHP, but both single quote and double quote have good uses.

Comments

Comments should start by “/*” and end with “*/”. Each line of comment must start with “” too :

/**
 * Reads configuration & set Software according to it
 * All lines must start with *
 */

This also have the 80 chars limit.

Naming

All function names and variable names must be in lowercase and words should be separated by underscore (_) or you can do the caps way :

myFunction()
my_function() 

But, your project must only use one of the above methods.

Conclusion

It’s better if all your projects follow the same coding standards as it will help you and other who read the code as well. This coding standards are my opinion only. You can choose your own and write it down in a document or on your mind and follow it forever in your coding life.