Importance of Writing Clean Code
Tags: code, programming, cleancode, bestpractices
Published: 2017-04-24 12:18:11

---
title: Importance of Writing Clean Code
published: true
description: “Programming is an art of telling another human what one wants the computer to do. - Donald Knuth
tags: Code, Programming, Clean Code, Best practices
---

_“Programming is an art of telling another human what one wants the computer to do.”
-Donald Knuth_
 
If you speak well, people will love to listen to you. Communication is an art which either can make someone fall in love with you or can give you critics.  Similarly in the world of programming. If you write horrible code, you will not be able to tell another programmer why you had written it. It’s a harsh reality that you work under the pressure of deadlines which makes for hastily written code. But keep this fact in mind, a badly written codebase is going to make your life more difficult.

_“Anyone can write a code that computer can understand. An only good programmer can write a code that human can understand.”_

Programmers usually want to learn different programming languages. In today’s world, there is a wrong perception that one who knows a number of technologies or latest technologies, is a hero. But believe me, there is no meaning of learning Java, CSS, AngularJS, Perl, Ruby, Python, COBOL if at the end of the day, you are not able to admire your own code. At the end, you become “Jack of all, Master of none”. It’s not just the technology; it’s the code that makes software powerful.
 
## Here are some advantages of writing clean code-
**1.      Your code is your responsibility-** You’re writing a code and it may not appear the same to you after six months. You are not able to identify your own code. You are now confused and telling yourself, “Why the hell I wrote this?  What was I trying to do? You put your efforts again to understand your own code. If you followed best practices, you could have saved a lot of time.
**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-
**1.      Naming conventions-** Can you tell what this one line code is doing- 

```
int a = b(x);
```

On what is this a, b, x man? How can anyone tell why is this coding written for? But what if same code is written like this-

```
int salary = getSalary(employee);
```

Now anyone can tell there is a method which takes employee object in parameter and returns his salary in Integer format.
**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.
 
When you start to embrace this thinking, you tell yourself, “This can much be better”. You make it better and better then start to admire your code. You start feeling proud of converting an if-else into ternary operator just to save three lines. I hope this article motivates you to start embracing this ideology. Start it from today and see the difference.

Read [CODING BEST PRACTICES PART-1 (NAMING CONVENTIONS & CLASS DESIGNING PRINCIPLES)](https://dev.to/mohitrajput987/coding-best-practices-part-1-naming-conventions--class-designing-principles) to learn some best practices of coding.

### Read some case studies of apps developed by me recently-
[Jokaamo](https://www.engineerbabu.com/blog/2017/04/jokaamo-services-doorstep/)
[CadVise](https://www.engineerbabu.com/blog/2017/04/developed-healthcare-app-automate-120-hospitals/)