How Important is Clean Code?

Some Introduction

Rarely do big projects are done individually. They usually are developed by a team of developers which have their own assigned tasks in contributing to the project. Not every developer codes in the same way. One might like to do one thing, and another might do another thing. Therefore, there needs to be a universal “style” of programming as a guidance for all developers so that each and every developer have the same style of programming.

Clean Code

This is where the style of clean code comes in. Clean code is code that’s easy to read, understand, and maintain. Clean code helps developers produce a high quality result of the product, especially in the long run. It also makes our code obvious to other developers. Clean code is also minimal, simple, and no extra stuff. The less code we write, the better it is for the system. Even Jeff Atwood (founder of Stack Exchange) said that the best solution no code at all.

If there’s clean, then surely there’s also dirty right? In simple terms, dirty code is the opposite of clean code is. It is anything that’s a pain to modify, work with, or continue from. Duplicates, global states and variables, pointless and unneeded things, tightly-coupled dependencies, etc. These are usually called code smells.

Clean Code simple and minimal

You don’t want to read something long and complex, it’s just time wasting and inefficient. Less code just means you got less things to think about. The more simple and less code you have, the easier it is to maintain it. Less maintenance means cheaper maintenance. Keep your code simple and minimal.

Clean Code is self-explaining

We don’t have to write comments for every single line. Comments should only be used to describe or explain complex things. The worst thing that can happen is what if you had to make changes to the code? You’d also have to make changes to each comments you made for the changed code. It’s quite bothersome right? You can simply just name your variables and method describing what it does and not write any comment at all. It helps other developers read, understand, and also maintain your code much more easier.

Look at the code below? Do you think those comments are needed?

// Method to add two integers
def add(int1, int2):
return int1 + int2 // return the result of addition

No? Right? The code itself is already self-explaining, therefore comments are not needed. Now imagine if you didn’t need the method anymore but want to add a method for multiplication.

// Method to multiply two integers
def mul(int1, int2):
return int1 * int2 // return the result of multiplication

The comment changing doesn’t look that much. But remember, this is a simple method. What if your method is not just one or two lines?

Clean Code is free of duplication

Have you ever heard of DRY? “Don’t Repeat Yourself”, quite explanatory right? Clean code should not have any any duplicates or repeated codes. If you see patterns in your code, there’s a high chance it’s dirty code. DRYing codes generally improves maintainability. But remember, something too much is not always a good thing. So there is a limit to DRYing.

Look at the flutter snipped code below.

Column(
children: [
Text(
'Text 1',
color: Colors.red,
style: TextStyle(fontWeight: FontWeight.bold),
),
Text(
'Text 2',
color: Colors.red,
style: TextStyle(fontWeight: FontWeight.bold),
),
],
)

Do you see a pattern going on here? What is the difference of both Text widgets? Only their labels right? One easy solution might be to create a method that returns the Text widget like below.

Widget redBoldedText(String label) {
return Text(
label,
color: Colors.red,
TextStyle(fontWeight: FontWeight.bold)
);
}

Now you can just change your previous code and just call the method with the label as the argument.

Column(
children: [
redBoldedText("Text 1"),
redBoldedText("Text 2"),
],
)

See how easy it is to read and understand? It is small chunk of code only for example purposes. But what if you had multiple lines of duplicated code?

Clean Code passes all tests

How do we know that our code do not introduce bugs else where? Tests allow future developers of code have confidence that the code they are developing do not introduce new bugs to existing code. Tests also help developers understand other’s code since tests is like storytelling about the code you make. However you see it, there is no excuse to not use testing for your code.

Clean Code is free from tightly-coupled dependencies

Remember the single responsibility principle? It basically states that a module has one and only one responsibility. This ensures that your code is not tightly-coupled to other pieces of code. This helps maintain your code easier.

Imagine if you had multiple modules or files doing the same thing. And all of a sudden you had to make changes to one of them, you might need to change those other modules too. This is called shotgun surgery.

Refactoring

We’ve also been fixing some of dirty code above. There’s actually a special term for it, and it’s called refactoring. Refactoring is the process of transforming a mess into clean code and simple design.

When to refactor?

The best time to do refactoring is the third time you’ve done a specific task. This is called the Rule of Three:

  1. When you’re doing something for the first time, just get it done.
  2. When you’re doing it for the second time, have some thoughts on having to repeat the same thing twice.
  3. For the third time, it’s best if you try refactoring.

Another good time to start refactoring is if you’ve just added a new feature. Not only does it help yourself read, understand, and maintain your code, it also helps other people too.

Refactoring while fixing bugs also helps developers discover other bugs quicker. We can also do this while also reviewing other’s code. Refactoring together with other developers will make fixing bugs, minor problems, or discovering major problems much more easier.

What to look for?

Basically look for code smells and anti-patterns and start from there. Look for bloaters (Long methods, large classes, long parameter list), object orientation abusers (temporary fields), change preventers (shotgun surgery), dispensables (comments, duplicate code, dead code), and excessive coupling.

Refactoring Techniques

There are many techniques used to fix code smell, but here’s a list of the main points of refactoring:

  1. Composing methods, which is usually used to fix long methods.
  2. Moving features between objects, which is used when you want to move functionality between classes, create new classes, or hide implementation details from public access.
  3. Organizing data, which helps in data handling.
  4. Simplifying conditional expressions, which help with conditional expressions.
  5. Simplifying method calls, which help understand method calls and simplifies the interface for interaction between classes.
  6. Dealing with generalization, which mainly deal with abstraction.

Examples

Here are some examples of clean code in my project. I’ve also cut out some of the implementation to make it shorter.

What I’ve done here is that the children widgets in the build method are extracted into methods that return a widget. Also, I made sure that each variable, method, and class names are self explaining. This makes it more easier for other developers to read and understand the code.

Conclusion

Clean code is an important style of code to always try to implement for every developer. Of course, every developers try to make an excellent code from the beginning. But who knows, maybe some bugs or problems might introduce themselves later in the future. Just imagine if the code that the bug or problem happens to be unreadable and hard to understand. It makes it several times harder to fix rather than a well thought out clean code. Therefore, clean code is a must implement for every developer.

References:

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
SALMAN AHMAD NURHOIRIZA

SALMAN AHMAD NURHOIRIZA

Computer Science Student at Universitas Indonesia