Loading...
 
Skip to main content

Coding Standards

Purpose of Having Coding Standards

  • A coding standard gives a uniform appearance to the codes written by different developers.
  • It improves readability, and maintainability of the code and it reduces complexity also.
  • It helps in code reuse and helps to detect error easily.
  • It promotes sound programming practices and increases efficiency of the programmers.

Our coding standards:

1. Naming conventions for local variables, global variables, constants and functions:

  • Meaningful and understandable variables name helps anyone to understand the reason of using it.
    • Example: a class for a block presenting a service:
    • Good name: services-presentation
    • Wrong name: home-block-2
  • Local variables: in camel case lettering starting with small letter: localData,
  • Global variables: names should start with a capital letter: GlobalData,
  • Constant: names should be formed using capital letters only: CONSDATA,
  • It is better to avoid the use of digits in variable names: ,
  • The names of the function should be written in camel case starting with small letters, but then each concatenated word should start with a capital letter: createProduct,
  • The name of the function must describe the reason of using the function clearly and briefly.
    • Example: a function to update a product:
    • Good name: updateProduct
    • Wrong name: functionTmp

2. Indentation:

Proper indentation is very important to increase the readability of the code. For making the code readable, programmers should use White spaces properly. Some of the spacing conventions are given below:

  • There must be a space after giving a comma between two function arguments,
  • Each nested block should be properly indented and spaced,
  • Proper Indentation should be there at the beginning and at the end of each block in the program,
  • All braces should start from a new line and the code following the end of braces also start from a new line.
Example:
function sum(int $x, int $y) {
     $z = $x + $y;
     return $z;
}

3. Error return values and exception handling conventions

All functions that encountering an error condition should either return a 0 or 1 for simplifying the debugging. On the other hand, Coding guidelines give some general suggestions regarding the coding style that to be followed for the betterment of understandability and readability of the code. Some of the coding guidelines are given below:

4. Avoid using a coding style that is too difficult to understand

Code should be easily understandable. The complex code makes maintenance and debugging difficult and expensive.

5. Avoid using an identifier for multiple purposes

Each variable should be given a descriptive and meaningful name indicating the reason behind using it. This is not possible if an identifier is used for multiple purposes and thus it can lead to confusion to the reader. Moreover, it leads to more difficulty during future enhancements.

6. Code should be well documented

The code should be properly commented for understanding easily. Comments regarding the statements increase the understandability of the code. Comments must be in English. When doing modifying a third-party template, module (…) it is needed to add a comment indicating a Gopadma developer did the change.
Example:
// Gopadma – Date (if third party template/module modification)
// Target of the modification (description)
Some code…
// Your comment
Some code…
If condition // why the if, and what happens if it fails
Some code…

7. Length of functions should not be very large

Lengthy functions are very difficult to understand. That’s why functions should be small enough to carry out small work and lengthy functions should be broken into small ones for completing small tasks.

8. Standard headers for modules

For better understanding and maintenance of the code, the header of different modules should follow some standard format and information. The header format must contain below things that is being used in various companies:

  • Logo: Gopadma logo (logo only)
  • Name of the module: in English and in French
  • Description: Synopsis of the module about what the module does. In English and in French.
  • Date of creation
  • Version:
    • 1.0.0 if new (first version)
    • 1.0.X: after each set of minor corrections
    • 1.X.0: after each set of major corrections
    • X.0.0: for new versions (when adding a new functionality or modifying the module's behavior)
  • Author: Gopadma Technologies
  • Modifications history: in a text file in the module, named “Version_x.x.x.txt”