Any fool can write code that a computer can understand. Good programmers write code that humans can understand—Martin Fowler

Web programmers write code to create software, application for business purpose. If we do not maintain a general coding guideline, we don’t recognize our own code after few months. After all we are human not a typist. So from the beginning of our career we should follow some good coding practice. In this article, I will share my experience and knowledge about general rules of writing good, clean and secure code. If you’re a developer or designer, please follow below points from your next project and you will surely see some difference in your code.

1.Think before code

Think two wise before write new code. First think about purpose of your code, can you create a function and pass two three parameter, which will give you more options to use this code. Like you need to insert and update data into database via php. Do not create two difference functions, create one function, pass a parameter $id. If id has value code is work  for update task, else code will do insert part.

2.Break your code into small parts

This is another great option to reuse you code for multiple task. Do not write a function which has more lines ex 200. Break your big function or coding block into small chunk. Each chunk should do specific task. Then call those small functions into your main functions and you will get your ultimate output.

3.Optimization your code

Your code should be optimization. Write same type of code, in same file. Like you have 9-10 ajax call in your application. So write all ajax call methods in same page. Do not put any other type of code here. If you are a designer, then do not put responsive css in your main stylesheet. Just create a separate responsive css and write all your device oriented css here. Another thing is that you should think about your fellow programmers. Use proper name for  variable, function and class. Maintain same coding structure/syntax through out the project like below:

for($i=0;$i<=10;$i++){
echo $i;
}

OR

for($i=0;$i<=10;$i++)
{
echo $i;
}

You can follow any of the above but do not write code using both of them in a same project. Maintain proper web framework architecture rule if you follow any framework. Like you write code in M-V-C architecture, do not write insert statement on controller. Always use model, this is M-V-C architecture rule.

4. Extensibility

Software features always extended for betterment to end user. So you should write your code in such a way that in future other developer can extend and add more features in it. It will obviously be easier to do so if the code was designed to be extensible from the beginning. Code that was not written this way may lead people into having to implement ugly hacks to add features.

5.Keep your code simple

Don’t write complex code. You will surely call some danger in future for you or others. Every programmer should remember this two lines. Less code, less error, less time to debug, easy life. More code, more bugs, more debug, more time, hard life. Code should do only what it need with simple way.

if($checking_value1==$value){

echo “result1”;

}elseif($checking_value2==$value){

echo “result2”;

}elseif($checking_value3==$value){

echo “result3”;

}else{

echo “result 4”;

}

The same thing can be expressed in a simpler way using the switch case, which greatly reduces the operational time:

switch($checking_value){

case Value1:

echo “result1”;

break;

case Value2:

echo “result2”;

break;

case Value3:

echo “result3”;

break;

default:

echo “result4”;

break;

}

Both of the above example give you same output but you always follow 2nd example

5.Comments

Comments showing bad code. Good code should be understandable without a line of comments. But we must use proper comments in our code with some technique. Do not write comments in every 2 or 3 lines. First add your comments at top of the page by describing why you create the page. Second add comment before creating a function, what your function do and how. This will give people more chances to come up with better implementation of this method. Write the comments as you develop the code, if you think you’ll add them later, you make your life difficult.

6.Don’t write all code at once

Don’t write code all your code at once. Write your code task wise or function wise. Take 4-5 minutes break after writing 2-3 hours of successful code. It will help you to generate less error in coding. If you take some breaks your mind will be cool, new idea will come into your mind. This will improve your performance also.

7.Choose a good code editor

A good code editor always save your time and help you to write good, clean code. Editor has some common features like tab maintain, coding style, methods or functions naming rule, basic syntax error detection etc.  So by choosing a good code editor, your basic coding structure problem will be solve.

8.Use Version Control for code management

All elements of your system—code, documentation, tool sources, build scripts, test data—should be under version control. Git and GitHub make this task cheap and hassle-free, but many other similarly powerful tools and services are available. You should be able to build and test your program on a properly configured system, simply by checking out the code from the repository.

So after reading my article, you learn how you can make your code more clean and beautiful. I am sure that you already follow some of the above points in your code. If you have any better idea  to make our coding practices good, you can share with us. We all know that Messy code is hard to read, and people may lose interest if they cannot understand what the code tries to do. So write clean code, think about coding community and at last happy coding.

Spread the love
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
General guidelines to write clean, secure and easy to maintain code