Step Up your code with Clean Code!
By: Sailee Renapurkar
Tags: Clean Code, Programming

When I graduated and entered the software engineering profession, I never thought of writing clean code as an important part when it comes to software development. I used to think, It is great if the software under development works properly. It’s good enough and so I thought design to be of utmost and only important activity.

However, when I Joined Sahaj, I went through a program called Gurukul wherein I got to know how important it is to write a clean code. It is not like I am great at it as of now. I am exploring more every passing day, I realized that writing clean code is not something that you can learn overnight. It needs practice and patience. We need to get our hands dirty writing code, finding code smell (understand and identify what a bad code is ), and then refactor it.

So here is what I have experienced why writing clean code is an important part of software development:

Easy to read and understand. Easy to debug and refactor. Maintainable and extensible. Easy for new developers to work on. Less time required in maintenance.

Here are some of the characteristics of clean code:

1. Code should be easy to read, the person reading it should find it pleasant.

2. It is focused i.e each class, each function serves a single purpose.

3. It passes all the unit tests written.

4. It contains the correct number of entities required.

5. There is no code duplication.

6. The code is expressive, it clearly tells what it is doing.

7. It is simple :)

8. Small changes in code should not break major things.

How Can I write clean code:

Well, you won’t appreciate the good code until you have seen the bad code. So, whenever we write a piece of code, we must try spending some time checking if code smell exists and consider refactoring it. Code smells are usually not bugs, they are not technically incorrect and do not prevent the program from functioning. Instead, they indicate weaknesses in design that may for eg. slow down development. Here are some of the fundamental ways to kick off writing clean code.

Meaningful Names:

The name should be revealing intention. It may require some time to get the right word for variable, function, or classes.

It basically should tell why it exists, what it does and how it is used. An extra comment must not be needed to tell the intent of the variable, function, or class.

e.g Variables:

int a; // required amount in rupees.

Instead, we must have something like:

int amountInDollars or int amount. But we can prefer it as amount, in case we need to switch to dollars in the future.

e.g Classes:

The name of a class should always be a Noun. viz Customer, Account, etc. It should never be a Verb. e.g AccountManager or OrderManager

e.g Method names:

Method names should be a verb, explaining what is their value.

int calculateAreaOfCircle(final int radius){ … }

We also need to be consistent when it comes to methods. Like we have used calculateAreaOfCircle we must stick with calculate and should not go for compute or evaluate. There should be consistency in naming.

Functions:

When it comes to functions, they should serve only one purpose. If there are some if-else ladders it should be handled within separate functions. The number of lines allowed in a function can be ideally really less if it serves the right purpose. When it comes to the function parameters, they must be final, because they should be immutable. The function should have no right to modify the parameters that are being passed to it, It should use it to serve its purpose.

Comments:

When you need to write a comment to explain the functionality, think it through and try to write code that pretty well expresses it. Because sometimes the comment can remain the same and the intent of function can change. i.e comments can lie, not always but sometimes, but the code always speaks the truth. Ideally, there is no need to use comments while coding.

If you feel you want to use those, maybe you are going wrong there and it’s time to reflect and see why there is a need for usage of comment..

Code Formatting:

The code throughout the project should be uniformly formatted because, if it is not It makes the reader wonder about the difference between the styles followed. The code throughout the project base should be as if written by a single person. There is no essential or best way to do code formatting, Instead, the team should agree upon a standard and set them in check style rules to ensure uniformity all over the code.

I have realized that writing clean code does take time, but with practice slowly we can improve our speed and efficiency. Because if and when there will be a problem in the future, our clean code will do wonders for us in helping us solve the problem/issue at hand. It not only helps the person who wrote the code but for anyone else who is looking to solve the problem. Though it takes more time while writing the code the first time, the effort is worth it.

And this is not the end of this topic. I will surely have a part 2, going in-depth to the ways in which we can write clean code. I recommend reading the book “ The Clean Code” By Robert Martin. It is such an awesome book that will make you see both the ugly and beautiful sides of code and make you proud to write the best code.