Overview

A coding standards document tells developers how they must write their code. Instead of each developer coding in their own preferred style, they will write all code to the standards outlined in the document. This makes sure that a large project is coded in a consistent style — parts are not written differently by different programmers. Not only does this solution make the code easier to understand, it also ensures that any developer who looks at the code will know what to expect throughout the entire application.

Introduction

Coding standards, sometimes referred to as programming styles or coding conventions, are a very important asset to programmers. Unfortunately, they are often overlooked by junior as well as some program developers due to the fact that many of the recommended coding standards do not actually affect the compilation of the code itself

The best applications are coded properly. This sounds like an obvious statement, but here ‘properly’ means that the code not only does its job well, but is also easy to add to, maintain and debug.

As PHP developers, we need to define and adhere to a coding style not because one is better than another but because we need a standard by which to collaborate.

Why do I need to follow coding standards?

Well this is the question which comes in our mind every time we write code. We just want to write a code that performs the required action. But to answer that question I’d like you to answer following questions-

  1. Can you actually read the code? Is it spaced out clearly?
    • Do you separate blocks of code into ‘paragraphs’ so that different sections are easily defined?
    • Are you using indentation to show where control structures (if, else, while and other loops) begin and end, and where the code within them is?
    • Are your variable naming conventions consistent throughout the code and do they briefly describe that data that they’ll contain?
    • Are functions named in accordance with what they do?
  2. If you come back to the code in a few weeks or months, will you be able to work out what’s happening without needing to look at every line?
  3. How are you commenting the work?
  4. Can a new member in your team understand your code without scratching his head?

If you are not able to answer any of the above questions positively then it’s time to change your habit and start writing code following some coding standard.

PHP CODING STANDARDS

Like all other programming languages php also has some coding standards which help programmers to write clean and easy to understand codes. Below is the list of coding standard followed by php developers.

  1. PEAR PHP
  2. Autoloading Standard(PSR-0)
  3. Basic coding standard (PSR-1)
  4. Coding Style Guide(PSR-2)

Basic code standard you should follow

1. Naming convention

Name your variables/functions and classes in a proper way so that one can easily understand the purpose of them.

e.g

class Calculator

{  

  public function addXandY()

  {

      //method for adding two variables.

  }

}
  • Variables – all small case with underscore separator.
    • eg variable_name or name
  • Constants-Class constants MUST be declared in all uppercase with underscore separators. CONSTANT_NAME
  • Functions- use camel case. First word should start with small letter after that all words should start with capital letter.
    • eg- function , functionName, functionNameFunction
  • Classes- Class name should start with capital letter. All words in class should start with capital letter.
    • eg- ClassName, ClassNameClass

2. Line indentation

Use 4 spaces for indentation instead of tab.

3.

If file contains only php code then you should skip using “?>” at the end of the file.

4.

Php files must have one blank line at the end of file.

5.

opening brace for classes and functions must go on the next line of declaration and closing brace must go on the next line of body

e.g

class ClassName

{

   function functionName()

  {

          //function body

  }

   //body   

}

6.

Opening brace for control structures like if/else ,for, while etc. must be on the same line and closing brace must go on the next line of body.

e.g

if (a>b) {

//do someting

} else {

//do something

}

7.

There must be a blank space between parenthesis of control structure and control structure keyword. but in function there must not be a blank space between parenthesis and function name.

e.g

function functionName(arguments)

if (condition) {

8.

Use proper indentation in your code, it increases readability of your code.

9.

Each line must have one statement only. And line must not be longer than soft limit (120 characters) if line exceeds this limit it is better to break it into two or more lines.

Recommendation

All php developers are advised to follow coding standards. It will be annoying in starting but once you start writing clean code, your code will be more efficient, readable/understandable and easy to modify for changes in future.

Spend 10 minutes everyday to read these standards. Learn them and next time you write your code follow these standards.

There are various tools available which inspect your code against defined coding standards in php. Use these tools to check your code and fix coding standards in your code.

IDE likes Sublime and NetBeans have plugins which checks your code against coding standard and indicate errors in your code at the time of development. Use these IDEs and integrate these plugins so that you can write code that follows php coding standards.

Leave a Reply

Your email address will not be published. Required fields are marked *