General guidelines to write clean, secure and easy to maintain code
“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 won’t be able to 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.
Think before code
Think twice before writing new code. First, think about the 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 different functions, create one function, pass a parameter $id. If id has value code is work for update task, else code will do insert part.
Break your code into small parts
This is another great option to reuse your code for multiple tasks. Do not write a function which has more lines ex 200. Break your big function or coding block into small chunks. Each chunk should do specific task. Then call those small functions into your main functions and you will get your ultimate output.
Optimization your code
Your code should be optimized. Write same type of codes 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 throughout the project like below:
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.
Software features are always extended for betterment to end-user. So you should write your code in such a way that in future other developers can extend and add more features in it. It will obviously be easier to do so if the code is designed to be extensible from the beginning. Code that is not written this way may lead people into having to implement ugly hacks to add features.
Keep your code simple
Don’t write complex code or 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. The code should do only what it need with simple way.
echo “result 4”;
The same thing can be expressed in a simpler way using the switch case, which greatly reduces the operational time:
Both of the above examples give you the same output but you always follow 2nd example
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 does and how. This will give people more chances to come up with a 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.
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 2-3 hours of successful coding. It will help you to generate less error in coding. If you take some breaks your mind will be cool, new ideas will come into your mind. This will improve your performance also.
Choose a good code editor
A good code editor always saves 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 solved.
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.