DEV Community

Mohit Rajput
Mohit Rajput

Posted on

Importance of Writing Clean Code

“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) to learn some best practices of coding.

Read some case studies of apps developed by me recently-

Jokaamo
CadVise

Top comments (33)

Collapse
 
frantisekmasa profile image
František Maša

Nice article. I would add just one thing. There will always be "better" and "worse" parts of your codebase, because it's natural, that one you get better, you tend to solve stuff in different way. You should just accept that there is rarely (if ever) finished project and that there is no way you'll have code that you're 100% satisfied with what you got.

Anyway, keep writing!

Collapse
 
mohitrajput987 profile image
Mohit Rajput

Thanks, František. Follow & keep reading my articles.

Collapse
 
rishabh52510351 profile image
Rishabh Raj

Thank You for sharing such an informative post. I like the way you present the things and make the post more presentable. At PegaLogics, You will get the best industry-leading experience in the field of Mobile App Development & Web Designing. Visit our website, Regards-
pegalogics.com/services/mobile-app...

Collapse
 
_ghati profile image
Wilkister Ghati

Awesome peace of article. I should follow this all the time.
Thanks for sharing

Collapse
 
mohitrajput987 profile image
Mohit Rajput

Thanks, Wilkister. Keep reading my articles.

Collapse
 
exon profile image
Benoit Tremblay

I can't stress enough how everytime I made something crappy for the sake of "We need to ship now", the code smells are hurting even for shipping now. Just don't. Great article :)

Collapse
 
mohitrajput987 profile image
Mohit Rajput

Thanks Benoit.
I hope this article will help you to make tasty smells of code

Collapse
 
pavanchoudhary_ profile image
pavan choudhary

Very good article Mohit, thanks for share.

Collapse
 
mohitrajput987 profile image
Mohit Rajput

Thanks Pavan.

Collapse
 
_yerk0n profile image
Yerkon

👍

Collapse
 
megharajput profile image
Megha Rajput

Very nice article for those who have a big fear of programming....If they read this,then definitely they will start loving programming.... :)

Collapse
 
mohitrajput987 profile image
Mohit Rajput

Thanks, Megha. Follow & keep reading my articles.

Collapse
 
sapnagolhani profile image
sapnagolhani

Nice article!

Collapse
 
mohitrajput987 profile image
Mohit Rajput

Thank you Sapna

Collapse
 
gauravwebdesign profile image
gauravwebdesign • Edited

Really help full to write clean code, In future i will keep all the points in mind before writing any code.

Thanks and keep update me regards to your nicely written article.

Collapse
 
mohitrajput987 profile image
Mohit Rajput

Thanks Gaurav, read my other articles also and share your valuable feedback.

Collapse
 
lucasjg1994 profile image
LucasJG

Thank you Mohit for this awesome article. I'll make sure to use these practices to learn more about clean code.

Collapse
 
mohitrajput987 profile image
Mohit Rajput

Yeah sure. Do share your ideas too.

Collapse
 
apptunix profile image
Nikhil Bansal

Beautiful code, like beautiful prose, is the result of many small decisions. The right method length here, the right object name there.

Collapse
 
mohitrajput987 profile image
Mohit Rajput

Yeah

Collapse
 
anasshekhamis profile image
ECMAman

Nice article ☺
I also wrote about the subject. If interested ☺
anasshekhamis.com/tag/clean-code/

Collapse
 
mohitrajput987 profile image
Mohit Rajput

Thanks. I will read your article.

Collapse
 
xyz85046927 profile image
Charles Xavier

Programs must be written for people to read, and only incidentally for machines to execute. ― Harold Abelson

Collapse
 
ruben69695 profile image
Ruben Arrebola

Great article, thanks for share, I love it!

Collapse
 
mohitrajput987 profile image
Mohit Rajput

Thanks Ruben. Keep reading here.

Collapse
 
itsaderm_35 profile image
Adam

Good article. :)

Collapse
 
mohitrajput987 profile image
Mohit Rajput

Thanks Adam