DEV Community

Awais Butt
Awais Butt

Posted on • Updated on

Best practices for writing clean code

Software engineering is not just all about getting to know a language and building some software program. As a software engineer or software program developer, you're anticipated to put in writing the correct software. So the query is what makes good software? The desirable software program may be judged through studying some piece of code written inside. If the code is simple to understand and smooth to trade then absolutely it’s a good software program and developer love to work on that. It doesn’t depend if you are a beginner or a skilled programmer, you need to constantly try and emerge as a good programmer
Before moving into the discussion of how to write clean code let's see its characteristics first.

  1. Written code should be readable. If someone reads your code they should have an understanding of the code.
  2. Code should be pleasing to read and make a smile on your face.
  3. Code should be easy to understand and easy to change.

Now, we come to our main discussion.

How to write clean code?

Naming Conventions
The naming convention is a great way to get started. It keeps code clear and lets you know what you’re working with.

Comments
Comments help others to understand your code behavior. Comments in your code are good maybe it means that your code is not self-explanatory.

Here is a famous quote about writing comments by Jeff Atwood.
“While comments are neither inherently good nor bad, they are frequently used as a crutch. You should always write your code as if comments didn’t exist. This forces you to write your code in the simplest, plainest, most self-documenting way you can humanly come up with.” — Jeff Atwood

We do not say that comments are bad but if you write code well with proper naming convention then your code does not need any comments.

Avoid redundant code
Many programmers face this problem they can write too much code and they follow all best practices but somehow their code is repeated. So, make sure that your code is efficient and not repeated again and again.
Lets talk about repetitive code example
Console.log(9 * 1);
Console.log (9 * 2);
Console.log (9 * 3);
Console.log (9 * 4);
Console.log (9 * 5);
Console.log (9 * 6);
Console.log (9 * 7);
Console.log (9 * 8);
Console.log (9 * 9);
Console.log (9 * 10);
}

This code will work but it's not efficient because the same piece of code is repeated.
Good practice:
We can rewrite that code using a for loop in JavaScript like so:
for (var i = 1; i < 11; i++) {
console.log(9 * i);
}

Avoid Large Functions
When a function is much larger, then it is better to separate it into multiples. This will make code easier, clean, easy to understand, and also reusable.
Suppose we need to add and subtract two numbers. We can do it with a single function. But the good practice is to divide them into two. When there are individual functions, then this will be reusable in the whole application.

Conclusion
All these tips help you when you write some code and any programmer will understand your logic and flow of the code.

Top comments (0)