Clean Code: Understanding Others

Dinda Inas
5 min readMay 4, 2021

You Need to Understand Me

Imagine you’re working together on a project, with each individual doing different tasks, yet you have to integrate them eventually.

How will you connect each of the tasks after it was done?

It’s like a puzzle — with unfit pairs. Even though each task seemed to be working fine separately, it may need some adjusting to be able to fit with the other pieces.

www.dreamstime.com/cartoon-two-businessmen-holding-trying-to-connect-unmatching-pieces-jigsaw-puzzle-cartoon-stick-man-drawing-conceptual-image124860622

But how can you adjust it — or at least, know which part that needs adjustment? How do you know where did it go wrong?

To know which part went wrong, or part that makes it not connect, of course, you need to read them.

Ehmm, okay… what’s so hard about reading?

Well, it’s not hard to read. But it may be hard to understand.

Because you are not the one who made it! It may be written differently than how you usually write your code. And that makes things harder to understand.

See how it can be chaotic now?

If you don't understand the code written by others in your project, it will be really difficult to adjust things.

Well… I still can ask them to explain it…

When developing a project, it’s really fast-paced. Time is a really precious resource. So explaining your code each time your team member doesn’t understand it, is really time-consuming. Moreover, if the one who made the code already left the project, then who will you ask?

So that’s why, kids, we need to code cleanly.

To make others (even yourself!) understand it easily later.

Clean Code

Clean code (or code that is called clean) is code that can be understood easily — by everyone. It should then be readable, changeable, extendable, and maintainable by other developers, with it being understandable.

For it to be ‘universally’ clean, there are some rules toward the making of functions, data structures, comments, etc.

Here are some examples and how they are used in my current project.

Small Functions

Functions should be small and only do one thing. This way, it’s more reusable and easy to debug.

Here is the example from my project.

function that get csrf token

This one was made by my friend (thank you!). At first, I thought it was such a waste to make a new file just for a single function. But later on, I realized that this function is used by many other files. Imagine if it wasn’t made in a single separated file. It would be tiring to write it over and over every time it’s needed.

So then, I learn to make my function small too.

function that get session

Thus, I can reuse it more easily.

Other than that, it also uses descriptive names. So it’s easy to understand its functionality right away.

Be Consistent

It is necessary to be consistent in writing codes for your project. Even though the ones who write the codes are different persons, they should be consistent in writing similar things.

ListJadwalKuliah function uses isLoading state

On ListJadwalKuliah function, it uses a state to indicate whether it’s still loading or not. The state is named isLoading.

using isLoading state in DetailKelas function

Then, in DetailKelas function, it also uses the same state name for the same purpose. This way, it is easier to understand what the state does. You may think that it is still acceptable if the state named as ‘loadingComplete’. Yes, it does. But then, you will need some spare time to check whether the state is doing the same thing. Thus, it is quicker to just name it consistently.

Clean Comments

While writing codes, it is better to make your code so clear you don’t need to comment on it. You can explain your code by using an obvious function name or attributes.

But if you still need to give some explanation, you still can comment on it. Here is an example from my friend’s code.

useful comment

This way, it is easier for other developers (including me!) to understand which library was the code based on.

Use Explanatory Variables

It’s mentioned earlier, using explanatory variables could help you to explain your code without needing to comment on it.

Here is an example.

variable of converted time

It’s another code of my friend’s. It uses variables to assign values of the converted int objects to time objects.

Just by seeing the variables’ names, I understand its function or what it stores. It makes it easier for me (other developers) to understand it quicker.

From the examples above, you can see how most of them are from my team’s code. That’s all already explain how I understand their code! More specifically, I understand their code because it’s clean.😊

So these are some examples of the many examples of how clean code helped me working on my project.

Now it’s time for it to help yours too!

--

--

Dinda Inas
0 Followers

undergraduate computer science student