9 tips for writing cleaning code

9 tips for writing cleaning code

Productivity

Introduction

Software development is not just writing code. Its about writing cleaner code which can be easily understood by other developers.

In this article, I have mentioned 9 tips which will help you to write much cleaner and readable code.

1. Use describing names

Clean code is easy to understand. While writing code we use short names for variables, parameters etc. in our code but we should use describing words for declaraing variables, parameters, functions etc.

Don't

const n = 100;

const it = 200;

Do

const number = 100;

const iterations = 200;

2. Use proper whitespaces & indentation

Many of us think that using whitespaces & indentation will affect the speed of compiler but you should use the whitespaces & proper indentation in your code because this make your code more easy to understand

Don't

function start() {
var name = 'John';
var code = 200;
}

Do

function start() {
  var name = 'John';
  var code = 200;
}

3. Try to reduce the number of parameters in a function

Since, we are trying to make our code clean. So, we should make a easy to read function for which we should make the function small by reducing the no. to parameters in it. If we need to use more than two or three parameters, then we can send one single object as a parameter in place of three parameters

Don't

function register(name, email, password, phone, address, intresets){

}

Do

function register(info){

}

4. Each function perform single task

Multitasking is great but not in terms of writing clean code. In many cases, developers create a function that do more than one task but we need to avoid that because it make difficult for others to understand our code. By creating one function for one task we can easily organise our code in more easily.

5. Try to reduce the size of functions

It is easier to understand small sized function in the place of functions which are large on size or contains huge code. If you are working on large projects you can use classes in the place of functions.

6. Try to reduce the characters in a line

Since,we are writing a code which is easy to understand and easy to read. So, we need to reduce the characters in our code lines. So that our code can be easily fit on screen and here is no need to scroll horizontally to see the code.

7. Always describe the reason why you create a commit

You should always define why you are committing a code in github repository. it gives us an idea what are the errors or bugs in our code some months ago and what changes are done with the code to improve. You should define this in four to ten words words or you may also use a word which can easily define your message.

8. Avoid repetition of code

Focus on creating reusable code. Most of beginner don't do that. Atleast 70% of your code should be reusable. This reduces the size of our code and also it makes easier for you to organise and work with code. You can break down your work into small task and make a function for that task.

9. Don't use unnecessary comments

Since we have already given describing and directed another, therefore, there is no need of comments in the code. Only write comments when you are using some third party APIs', applications or requests. Avoiding comments make the code much cleaner and easier to understand.

Read the full article https://techwithpie.blogspot.com/2022/09/tips-for-writing-clean-code.html

Thank you, and wish you all a great programming experience.