Less Struggles with Clean Code

Sarah
Pilar 2020
Published in
4 min readOct 8, 2020
source: https://vnqthai.com/post/clean-code/

One thing that I constantly struggle with throughout the last two years of being a computer science student is understanding other people’s code. As our team has to continue our previous development, I tried to use every amount of my free time to understand how the existing code works. Despite how difficult it is, we were thankful that our seniors, Post-RPL group, wrote such delicate and clean codes that we were able to get a grip of it quickly without much effort.

So, what is clean code?

From some experts’ perspective in the Clean Code book by Robert C. Martin, we can say that a clean code should be:

  • Elegant and efficient; it should be able to do one one thing well. (Bjarne Stroustrup)
  • Simple and direct. (Grady Booch)
  • Can be read and enhanced by other people. (“Big” Dave Thomas)
  • Written by someone who cares! Someone who takes the time to keep the code simple and in an orderly manner. (Michael Feathers)
  • No duplication, expressive (use meaning full names), use tiny abstractions. (Ron Jeffries)
  • The code makes it look like the language was made for the problem. (Ward Cunningham)

And… how do we write a clean code?

There are several ways that can help you write a program with clean code.

Naming Convention

The easiest implementation of clean code (in my opinion) is to make sure that the names of our class, variables, and functions reflect the context of themselves. This will help us, and also other developers, to understand what the variable or function is supposed to do.

An example from our backend code:

We named the class as ‘BatchSerializer’ because its purpose is to display fields from the batch models that are rendered from the API. Imagine if the class is named ‘Class1’ or ‘Class2’; future developers will most likely be confused and this might slow down the development progress.

Functions

Functions or methods should be small. It is supposed to do one thing only and it should do it right. Methods should also return something, so it is not supposed to return null. Another addition, the ideal number for a function’s argument is zero, following one and two. More than three arguments require a special justification.

Comments

We should only comment on our code when we fail to express our intention in the code. If the code is easily understood, there is no need to write comments. Comments are not justifications for bad codes. If we think that we wrote an unorganized and incomprehensible code, what we have to do is to improve the code. Hence, comments are needed only when the part of your code requires extra explanation.

Exception Handling

We should create plans to handle any kind of errors. However, we must separate the business logic for try-catch blocks from the actual function. If an error occurs, we have to mention where and how it happened.

Don’t Repeat Yourself (DRY)

One of the hardest temptations to resist is to copy and paste. It definitely is the easy way out. However, did you know that having a duplicated code can introduce bugs in more places? Based on what I’ve learned from my past Advanced Programming course, we can avoid these duplications by abstracting out things that are common.

Layout Formatting

A code should be pleasant to read. With the help of the linter tool, we can apply the code layout rules based on its language. It should be structured and clear so our teammates can be aware of our code easily. For instance, use proper indentation, and then structure your class with variables placed on top of functions. If you write in Java, the class name should be capitalized and variable names are in camelCase. There should not be unused variables to avoid confusion.

To sum up, clean code helps our future selves and others understand the code. Thanks to the DRY principle, safe from bugs is also one of the benefit of implementing clean code. Clean code also helps future developers who want to enhance the code as the code is ready for changes.

I hope this helps and thank you for reading!

— Tsamara E.

Source:

  • IT Project 2020 Guideline

--

--