1. Code Commenting & Documentation: IDE(Integrated Development Environment) has made commenting the code more useful. IDE and other tools can utilize your comments in different ways. Every routine should be started with a comment indicating what the routine does, its parameters, what it returns and possible errors and exceptions. The role of each file and class, contents of each class field and major steps of the complex codes should be summarized in comments.
For example:
/* function X is use for xxxxxxxxxx functionality */
function Add()
{
// your logic
}
2. Consistent Indentation: There are different indentation styles available to be followed. There is no particular style to be followed, you can choose any style. However, it is essential to maintain a consistent indentation style throughout your code.
For example:
3. Avoid Obvious Comments: You should ensure that you do not comment your code unnecessarily.
For example:
if() // this is my condition in if
{ // now I start brace
// here I started logic part
} // now I start brace
4. Code Grouping: Group your tasks in separate blocks/functions of code separating them with spaces. You can also add a comment at the beginning of every block.
For example:
/* This function is use for x funxtionlity*/
function Add()
{
// your logic
}
/* This function is use for y funxtionlity*/
function Delete()
{
// your logic
}
5. Consistent naming scheme: Two of the popular naming conventions are:
CamelCase :This entails capitalizing the first letter of each word except the first word.
For example:
/ Always use relevant names /
function deleteRecords($userName) // this name represents that this function “deleteRecords” is used for delete records
{
// your logic
}
Underscore: It entails using underscore between words such as mysql_real_string()
For example:
/ Always use relevant names /
function delete_records($user_name) // this name represents that this function “delete_records” is used for delete records
{
// your logic
}
You can opt for any naming scheme but it is vital to maintain the consistency of the naming scheme throughout. Generally, Camel case is used in Java while underscore is used in PHP development.
6. DRY Principle: It stands for Don’t Repeat Yourself. It is also known as DIE (Duplication is Evil). Under no circumstances you should copy and paste codes. Most software programs aim to automate repetitive tasks. Hence, the coding and web applications should be such that the same code is not repeated over and over again.
7. Avoid Deep Nesting: Having too many levels of nesting makes the code hard to comprehend. Hence, deep nesting should be avoided.
For example:
/* don’t use multiple conditions */
if()
{
if()
{
if()
{
//your logic
}
}
}
8. Limit Line Length: Reading tall and narrow columns of text is more comfortable for the eyes. Hence, you should try to use short line lengths. The ideal line length would be 80 characters.
For example:
The following line is not the standardized way of coding.
echo ‘Hello world’; echo ‘Welcome’; echo ‘Anything’;
Instead it should be split into short lines as below:
echo ‘Hello world’;
echo ‘Welcome’;
echo ‘Anything’;
9. File and Folder Organization: Although it is possible to write an entire application code in a single file, its readability and maintenance will be a problem. Hence, it is recommended to organize it into folders.
10. Object-oriented vs. Procedural :Well structured codes can be created through object-oriented programming whereas procedural functions facilitate performing specific tasks independently.
11. Read open source code: In the case of open source projects, the software is built using inputs of multiple developers. Hence, it is vital to maintain code readability so that the team can work on it. It is beneficial to browse through the source code of these projects to have an idea of what the developers are doing.
12. Code Refactoring”: Refactoring a code entails incorporating changes to enhance its readability without altering its functionality.