5 Simple Ways to Write Clean CodeBlog

Code for People, Not Machines


BY Shubham Pathania - Senior Developer - @Shubham53825007 / ON Nov 05, 2020

5 Simple Ways to Write Clean Code

6min read

Software development is a complex process, and coding is the crux of the development process. The complexity of the software is directly related to the quality of code. If the code is easy to understand and easy to change, then definitely it’s good software.

Every code works, even the bad one. The difference between good and bad code is the ability to perform a task more efficiently.

Writing clean code is a sign of good coding, but it is not an easy task. It takes time and practice to write clean code. Sometimes developers avoid writing clean code due to deadline pressure or being lazy. Either way, it hurts the overall quality of the software.

Clean code is easy to manage. Developers love working with clean code, as it makes it easier for them to manage the whole application. Nobody wants to work on an application with a horrible or messy code. It is a nightmare for most developers.

I’ve been coding for more than five years. I’ve realized that some developers unknowingly write bad code to save time. But they end up wasting more time as it creates more bugs, which they need to fix later by going back on the same piece of code.

Today, I am going to share some techniques to write clean code. These techniques transform you into a better programmer, no matter if you are a beginner or an experienced programmer.

1. Code for People, Not Machines

There are many good coders out there who can write complex functionalities in a breath. The only problem is that many programmers forget that they are not coding for machines.

I mean, of course, they are building software which will require some enhancement or maintenance in the future. They might not be around to do that, and probably some other programmers will be working on their code. That’s why programmers should code keeping people in mind, and not machines.

You can start doing this by giving proper class, method, and variable names. They should be simple and meaningful. Whatever name you mention in your code, it should fulfill three purposes: what it does, why it exists, and how it is used.

Make your code good enough that other developers can understand it and don’t have to spend much time decoding it. That’s one of the desired qualities of clean code.

2. Your Code Is Not the Swiss Army Knife

Many young programmers often write complex code unknowingly. They assign too many responsibilities within a single class, which makes it complex and hard to maintain.

Let’s understand this with an example.

Suppose we have a Candidate class with two responsibilities, as mentioned below:

Here, the Candidate class is handling two responsibilities: inserting data into the table and generating some reports for the candidate.

Now, suppose that currently, report generation is happening in Excel format. If a requirement comes for PDF format, then that will require a change in Candidate class.

As per SOLID principles, the first rule states single responsibility, which says that a class should take a single responsibility, and there should be one reason to change that class.

Our Candidate class violates this principle. We should always assign a single responsibility to a class, as mentioned in the below code:

Here, we have created a separate class for report generation functionality. The above class won’t have an impact on the Candidate class if we need to modify report generation techniques in the future.

3. Comments Can Save Review Comments

No matter how simple you try to make the code, others will definitely take time to understand it. Remember the time when you looked back at your own code after a few months and struggled to understand it at first glance?

You can save other’s time and yours by adding comments in your code. Short and simple comments make your code self-explanatory. They serve the purpose of documentation for your work.

Try to make this a habit, and it will help you to maintain your codebase without much pain.

4. Code Formatting Is Important

Clean code is not just about functionality but also how it looks. A simple code written with poor formatting will still be difficult to understand.

I won’t go over the fuss between spacing vs. indentation, but you should use whichever you feel good about. Formatting not only improves the readability of your code but also makes it easier for enhancement or modification.

There are several static code analysis tools like StyleCop, FxCop, Resharper, SonarQube, and many others. You can choose one as per your programming language. These tools not only help you with formatting but also provide other insights that can make clean coding easier for you.

5. Refactor the Old Devil

Every programmer tries to give their best while writing their code. Sometimes, it’s possible that you have written the code in the best, most efficient way possible at that time. But when the same code is looked at after a couple of months or years, then it might not be considered the same way.

One of the valid reasons for this could be the limitation of the programming language. If someone wrote the most optimized and efficient code using Java in 2015, then after several new releases of the language, there might be better techniques or in-built functions to perform a set of operations more efficiently. This makes your old code inefficient and requires refactoring.

The reason most software dies over time is because of the developer's negligence to refactor the code for optimization. For instance, take a look at below C# code:

In the old approach, we first checked whether the object was null or not, and then we checked if a particular value inside that object was available. Only after that did we assign that value to a variable to avoid the exception.

In the new approach, we’re using the new C# feature, the safe navigation operator(?.), which was introduced in version 6.0. This lets us directly query the object properties, and in the case where a property has null, then instead of throwing a null reference exception, it returns the null value.

With just a new feature, we were able to convert seven lines of code into a single line. This is one example of how we can simplify and optimize our code with new features in the programming language.

Final Takeaway

In summation, by keeping the below-mentioned qualities in mind, any programmer can write clean code.

  1. Give meaningful names to objects. Keep in mind you are writing code that will be maintained by others.
  2. A class should have a single responsibility. There should not be more than one reason to modify a class.
  3. Comments are important for your code. They save a lot of time for developers.
  4. Code formatting is an important aspect of clean code as it improves the code readability.
  5. Refactor the old code from time to time. It makes your application faster and more efficient.

All programmers should keep these points in mind while writing code. Clean code takes a little more time, but it saves the overall time of testing, debugging, and maintenance.

Thanks for reading!