Make Your Code, “SQUEAKY” CLEAN

Akbar Novial
bisaGo2020
Published in
5 min readOct 22, 2020
source: https://www.deviantart.com/stepandy/art/Clean-Vs-Dirty-115931761

Clean Code is one of the most important things to heed in Software Development. As this is very essential for programmers, let’s just jump right into it.

What is Clean Code and Why Should We Implement it?

Clean Code is code that can be understood easily and by everyone on the team and is easy to change. When other programmers read your code, they should be able to easily understand it, which would also help in maintaining your code.

How Can We Write Clean Code?

General Rules

According to Robert C. Martin, there are some general rules that can lead to clean code, which are:

  1. Follow standard conventions.
  2. Keep it simple stupid (KISS). Simpler is always better. Reduce complexity as much as possible.
  3. Boy scout rule. Leave the campground cleaner than you found it.
  4. Always find the root cause. Always look for the root cause of a problem.

The PPL 2020 Programming Guideline also states that here are 6 points that can help improve the quality of your code, likely:

1. Good Naming

This really helps as it can remove the confusion from other developers when reading your code as the names have explicitly told them what they are. For example, we have a variable

int a = 20;

When another developer sees this, they would be confused if they don't know the context. We can, however, improve it by using specific names, such as

int amountOfBooks = 20;

Remember, besides variables, good naming also applies to methods, classes, etc.

Class Names, usually have a rule that they should use nouns or noun phrases and should not be a verb, e.g. Customer, Account, etc.

Method Names, however, should use verbs or verb phrases like redirectPage, deletePage, etc.

2. Efficient Comments

One of the rules of comments in Clean Code is that you should “Always try to explain yourself in code”. What does this mean? If you have to write comments to prove your point, that means that the code you’ve written is dirty. A programmer should write a code so clean that any expressive code comments become unnecessary. When someone reads your code, they shouldn't have to read your comments to understand what the code does. If you have to write comments, keep in mind that good comments should be short and descriptive.

3. Simple and Effective Writing Functions (Methods)

A rule of thumb when writing functions/methods are that each function should only do one thing. This will help in the long run as you can identify the specific locations when you need to change or fix something.

4. Exception Handling

When writing code, you should always prompt to returning the exceptions rather than returning the error codes. It’s also important to make sure that the exceptions you throw are clear and concise. This shows that you and your team are prepared and have expected all sorts of situations that could arise from the use of your code. Not to mention, embracing exceptions will keep your code maintainable, extensible, and readable.

5. Don’t Repeat Yourself

The Don’t Repeat Yourself Principle, also known as DRY stress that as a programmer, you should not repeat any of the code you make. Instead, you should reuse it. Duplication is Waste. If you were to work on maintaining the software that does duplication, you will for sure be in for a ride, because there can be loads of additional and unnecessary code, which would also lead to the decrease in quality of your code.

Our team also tries our best to implement the DRY Principle, as shown below.

We created a method to redirect all of the buttons in the Tentang Disabilitas Page to their respective pages.

6. Layout Formatting

Have you ever heard of the phrase, “Don’t Judge a Book by its cover?” Sadly, judging also happens to code, especially the ones that have terrible formatting as it would immediately disinterest someone from reading the code. Because of this, one should always try to apply the proper formatting rules. Proper formatting usually consists of proper indentation, structures of classes, the naming of variables, and many more. You may think, “That sounds like such a pain…”, but don’t worry, because Linter is here to help! Lint or Linter is a tool that specializes in improving your code. It does so by analyzing your code for any bugs, programming errors, stylistic errors, and suspicious constructs, which would then be flagged to identify easier.

We’ve also used Linter ourselves for our project, it can be seen as there is a stage for it in the Git Pipelines.

An example of dirty code that I’ve encountered during the development of BisaGo is as so, the use of unnecessary imports that can be seen in the first line, which would also be considered as a code smell.

But alas, the time has come for us to part ways once again. In conclusion, as a programmer, always strive for making clean code. Because it can not only help yourself but also other people around you. Let’s get rid of the dirty codes make ourselves a better programmer.

References:

--

--