Importance of Writing Clean Code
Here are some advantages of writing clean code-
2. You work in a team- As an employee or co-founder; you work in a team to develop software. If your team member is unable to understand your logic by reading your code, you’re not doing it right. The Same case comes with you, if your team member doesn’t write the code well, you will waste your valuable time in solving conflicts, diffing & merging codes and understand the logics. Don’t give your team members or client a code that barely stays intact.
3. Easily maintainable and extensible- If you didn’t follow a pattern while developing a project, you feel it very hard to add a new module or change in existing module after six months. If the client tells you to add a new property in student’s profile, you should know how many classes will be affected. For example, if you follow a good ORM approach, you just need to add a new field in student’s model class and add some little code to add a new property.
4. Easy debugging- You will develop a project which is looking beautiful from outside. After deployment and user interactions, you see the user is unable to use a feature called A. It gets crashed if you continuously stay on it for more than 2 minutes. Now how will you debug it? What will be your approach? If you have separate classes or methods for this feature, if you have done logging properly then you can debug it step by step and solve the exact issue. If you have just written everything is one file, without logs and there is no method, you will end up with the spinning head.
5. You’re not the owner of product- If you’re developing a project for your client; you deploy the build after completion of the project. Does the client become the owner of this project with this build only? No. The client actually pays you for the source code. If he wants to modify it later then he should get ease in it.
Here are some best practices for clean code writing-
int a = b(x);
int salary = getSalary(employee);
2. Modularization- Write reusable code. For example, don’t write salary calculation code everywhere wherever you need the salary of an employee. Just write it in one place, create a method and use it everywhere. You save the line of codes, scale and extension time, creates easy understanding, minimize the chances of errors. Use OOPS concepts. Create classes, write generic code, use inheritance, and create methods for each different task.
3. Create the map of the house- While making a house, do you say let’s make the house first and we will see if there are any changes in it. Will we destroy it and will make it again? It’s necessary to build a map of the house first. So don’t just start writing code. You must have a well-written document which has ER-diagram, Use-case, sequential diagram so that you can understand what exactly you’re making. Having clarity since day one helps you to write good code. Having a good architect will help you to develop a mind blowing project.
4. Keep it simple and small- In point no. 1, method getSalary should only calculate salary and returns it. It should not do any other thing i.e. calculating a number of paid leaves he took in last one year. You should have a separate method to calculate leaves and call it from getSalary() method if you need it while calculating salary.
5. Choose the right tool- The right tool for the right task makes work easy, time savvy and well written. For example, you are creating a dynamic java web project; you will feel it difficult to create folders nesting, compilation, and creating manifest. You might do errors and spelling mistakes in file naming and coding as well. If you use Eclipse IDE, you will get structure already created which will help you to write code in right folder and file. You will see reasons for errors, you can compile easily, create WAR files easily, debug easily.
6. Be the best artist- You’re the author, you’re the artist of writing code. Improve your code writing every day. Each day is the new day to improve what you learned yesterday. Improve your coding day by day. Try to make it cleaner, beautiful, small every day. Be the best artist of writing source code.