My FeedDiscussionsHashnode Enterprise
New
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more
How to write clean and better code?

How to write clean and better code?

Puskar Adhikari's photo
Puskar Adhikari
·Apr 27, 2020

Being a programmer doesn’t mean to just write code that works, but it must be written cleverly and well so that your team or people that work with it afterward, can understand it easily. To write better code means mainly to understand it and keep it consistent.

We all know that even bad code works. But it takes time and resources to make a program good. Moreover, other developers mock you when they are trying to find what all is happening in your code.

So here I have mentioned how we can write better and clean code so that other developers can better understand it. Here I have given a quick introduction to certain techniques I use while writing codes, and show you how you can do the same.

1) Plan it out⁣

Before beginning to code, plan out your projects. Write down everything you’ll need, break it down into steps.⁣

This will result in better code and will increase your workflow.⁣

2) Use Comments⁣

Commenting your code will not only help you but will help other people working with your code.⁣

Good comments explain why things are done, not what is done. The code itself explains what is done.⁣

3) D.R.Y⁣

D.R.Y stands for: Don’t Repeat Yourself. Always make sure your code is D.R.Y when possible by re-using variables, functions, etc.⁣

4) Naming⁣

Make your names for classes, variables, methods, functions, and so on, descriptive and unique.⁣

This helps everyone understand what is happening and makes it easier to support.⁣

5) Avoid Long Nesting⁣

Too many levels make code harder to read and follow, so don’t have a while loop inside an if inside an else, inside a for loop, inside an else if, inside a do/while loop, inside…well, you see where we’re going with this.⁣

6) Test Your Code⁣

Before releasing your product, make sure you have fully tested your code for bugs and errors.⁣

There are many tools you can use to automate this process too!⁣

7) Version Control⁣

Using version control software helps you keep track of changes and keep every team member working off the latest version. ⁣

⁣8) Document Your Code⁣

Documenting your code is just as important as all the others. If you want people to follow your conventions and understand how your product works, you need good documentation.

Note: ⁣In this article, I shared my experience to write a clean and better code which is very essential for every developer who wants to be remarkable in the career path.