Image source: https://incorainc.com/wp-content/uploads/2020/03/1-1.png

Sometimes when we write a piece of code, it may be tempting to only write a code that works and not care about its readability. At first glance, spending more time on writing a good clean code may seem time and effort consuming, but in reality, clean code can actually offer a lot more than meets the eye.

Back when I was just a beginner to programming, my only objective was that my code could do the things that I needed it to do. I didn’t care about being organized with my code and had a bad habit of writing my code sloppily. Because of this, my friends and project partners would often be confused when they had to read my code. Sometimes, even I would also struggle to read my own code. This would also cause me to consume a lot of time to debug any errors. After a while, I learned about clean code. By using clean code, I actually saved a lot more time and effort in the long run.

OK, so what is clean code?

Characteristics of clean code

Classes and functions in clean code should be made as small and compact as possible. Smaller classes and functions are intended to make them have a more defined and clear purpose. They should only do one thing and do it well. Bigger classes and functions tend to do a lot of things, when they can actually be broken up into smaller pieces.

Another characteristic of clean code is that there are little to no comments. Our code should be clear enough that it can explain itself without the comments. When you add comments to explain code that is already crystal clear, they really don’t add any extra value. Comments should only be used in necessary situations, such as noting how important a function is or clarifying why a certain value is chosen as the default value.

Some clean code principles

KISS (Keep It Simple Stupid)

A misconception that some people have about code simplicity is that shorter is simpler. This is something that some people use to justify using single alphabet variables such as x, y, z. In reality, it doesn’t work like that. The main reason we need simplicity is to maintain readability. If your code is short but extremely hard to read, what’s the use of it.

DRY (Don’t Repeat Yourself)

How to keep your code clean

Use Meaningful Names

Meaningful name piece of code from DocSer

The name of the variable below is concise and clearly states what it is. It is a styled form label, hence the name StyledFormLabel.

Create Single Responsibility Functions

Function that creates a logout button

The function above only does one thing, which is to create a logout button. It can’t be split up into a smaller function, which is why you can tell that it is a single responsibility function.

Be consistent

Consistent naming convention

Other than using standard naming conventions, in order to keep your code consistency, use one word for names with similar functions. For example, get and fetch have the same meaning, so just pick either one for your names.

Reference

https://medium.com/clarityhub/low-coupling-high-cohesion-3610e35ac4a6

https://sis.binus.ac.id/2014/04/12/clean-code/

https://www.freecodecamp.org/news/clean-coding-for-beginners/

A computer science student.