Coding Standards

By reading the title, Questions will come into the mind are:

What is coding standards? Why I have to follow the coding standards? What is the importance of it? Why it’s needed?

Well, Let me explain what it is, why it’s needed and what the importance of it. Coding standards are the set of rules which developer must have to follow to maintain the code in well-structured format, easy understanding, good readability and to keep consistency.

Let me give you one good example. Let’s say you recently joined a new company and you have to work on a big product they have. Obviously, you will check the code and try to understand it. If the code is not well structured, commented and if the variable naming convention is not followed then it will be really difficult to understand the logic right? Same thing will happen if any other developer tries to understand your code. So it is really important for every developer to follow the coding standards.

Coding Standards Every Developer Must Have to Follow

This article contains all the standards with an example in PHP language. You can follow almost all these coding standards for any other language like Javascript, Jquery, Java etc.

1. Variable Naming Conventions

Always try to give a meaningful name to the variables. Also, prefix data type of variable with its name. So let’s say you are storing user count in one variable then name it like  $intUserCount = 50; instead of  $userCount = 50; 

Below are the variable naming conventions which developer must have to follow.

Example:

The date should always be prefixed with “str” if we are storing them for display purpose as a string but if it’s an object in Javascript or PHP always use “obj” prefix.

For all variables where it is expected and assumed that there will be only single character stored, we must use “chr” as a variable prefix.

2. Braces should always be included

When writing the code using if, for, while etc blocks, braces should always be included. Leaving out braces makes code harder to maintain in the future and can also cause bugs that are very difficult to track down.

3. Keep function outside of the Loop if it returns the same value

If function is going to return the same value or perform the same task, always try to keep function outside of the loop.

4. Operator Precedence

Even if you think you know the order, don’t guess because chances are you’ll get it wrong and cause an unexpected bug that will be very difficult to find. Also, it will make maintaining your program a living nightmare for anyone who doesn’t know the precedence tables in so much depth. Always use brackets to make it absolutely clear what you are doing.

5. Quoting a String

String in PHP can either be quoted with single quotes (”) or double quotes (“”). The difference between the two is that the parser will use variable-interpolation in double-quoted strings, but not with single-quoted string.

So if your string contains no variables, use single quotes and save the parser the trouble of attempting to interpolate the string for variables.

You can use double quotes (“”) for combining text string along with variables.

6. Indexing and Line Length

Make sure that your Editor is not configured to convert TABs into Spaces, because either in Netbeans or any other editor if TABs gets converted to Spaces then it will mess coding structure.  If a company is following coding standards then make sure that all the developers have same TAB size.

It is recommended to keep lines at approximately 75-85 characters long for better code readability.

7. Arrays

If the array size is more than 3 then always split the array and keep assignments in the same alignment for better readability. When splitting array definitions onto several lines, the last value may also have a trailing comma. This is valid PHP syntax and helps to keep code diffs minimal.

8. Split the Long if Statements into Several Lines

Split the long if statements into several lines when the character/line limit will exceed. By doing this, a developer can easily understand the logic behind it.

9. Alignment of Assignment and Code Spacing

To support readability, the equal signs should be aligned in block-related assignments.

Do not keep more than one space between two line of code.

Always keep a space before and after the arithmetic operator, less than, greater than, the semicolon in for loop, equal to etc.

10. Recommendation

  • Always use SWITCH case instead of IF ELSE when encountering more than 3 conditions
  • Always initialize your variables, this will stop unnecessary notices in case variable is not assigned value.
  • Always set sequence number like 2, 5, 8, 11… like these. It helps to change/add something in between later on and saves lot of time afterward.

If you are following any other good coding standards then let me know in comment and I will add it in my list.

Happy Coding 🙂

You may also like: 10 Sublime Shortcuts for Developer

Sharing is caring!