Cast Clean Code

Clean thyself, after thy code

Johanes Jason
6 min readApr 11, 2022

“You should name a variable using the same care with which you name a first-born child.” — Robert C. Martin, Clean Code: A Handbook of Agile Software Craftsmanship

As a programmer myself, I often reread the codes I wrote to remember how to use an algorithm or data structure. However, because my code is messy and hard to read, sometimes I have to read it over and over again to finally understand. Sometimes, I also have to read other people’s code. I was a teaching assistant for Data Structure and Algorithm course, and I had to read student code to help them debugging. Imagine if their code is ill-written, I will definitely have a hard time understanding the algorithm they used.

Clean Code illustration

What Is Clean Code?

To put it simply, clean code is code that is easy to understand and easy to maintain. Assume that we have just been hired by a company, and are given the task of continuing the application/program that was made by other developer. We have to understand the code first, then we can continue the application. That’s why we should implement clean code to our program.

Clean Code Approach

  • You are the next person to read the code you are currently write. As I said earlier, we will definitely read our code again at some time, so instead of wasting time re-understanding, we should make some really well-written code so we can understand quickly.
  • Better clear than clever. If you are do some problem-solving, it’s better to write the solution clearly, instead writing a complex algorithm. We must make sure that after we solve the problem, other people can read that solution code easily.
  • When we use agile methodology where we work as a team, it is very possible that we work on a feature together. Therefore, we must establish together in the team that the code written must be clean. That way, it will make it easier for a programmer to continue the work of the previous programmer.
  • The application we made, of course will be maintained by us, so that the application continues to provide new and updated feature to the user. The maintenance itself, requires a lot of money and time. Clean code is the solution to this problem. By using efficient code, we save the use of human resources by maximizing the efficiency of the programmer in understanding the code. With this, we can minimize the costs needed to modify the software.
  • Clean code does not happen by itself. We should implement it ourselves, but how? I’ll explain how to write clean code.

Clean Code Implementation.

  • Separate Concerns
    Each section (function or class) should only deal with one problem. If you are writing some algorithm, make sure to separate them properly. For example, we want to create a todo list. Bad example of implementation is you shove all tasks and algorithm into one function. In clean code, you want to separate the concern, so one function only does one thing.
Bad example
Good example
  • Clean, Reusable, and Maintainable
    Clean means that the code has a good structure, has well named variables or functions, etc. Reusable means that you implement separate concerns, so other people can use that function/class too. For example, if you want to create a list of your friend’s name and age, you can use stringIntoObj function that we made earlier. Maintainable means other people could understand the code you wrote.
  • Never duplicate (Don’t Repeat Yourself aka DRY)
    I think it’s pretty simple. If you write a line of code for an exact same algorithm, create a function instead. Use programming concept such as Object Oriented, Design Pattern, etc.
  • Choose Descriptive Name
    Choose a name that makes other people understand what this variable/function is used for. For example :
Example of naming
  • Function and Classes Should Be Small
    Implement OOP, any data structure or architecture properly. If you named your small function correctly, you can debug easily if there are any errors occur.
  • Function and Classes Should Do One Thing (High Cohesion)
    As I said earlier, implement separate concerns. Every class and function have to do what it’s supposed to do.
  • The Best Comment Is the Comment You Didn’t Write
    If you named your functions, classes, or variables correctly, you wouldn’t need to explain your code with comment. People with understand what is stored by a variable by it’s name, or what a function does by it’s name.

My Clean Code Implementation

Personally, I really like seeing well-organized code, and because of that I really care about how I write my code. Since learning clean code, I’ve always try to implement clean code for all my code. Here are some examples of my clean code implementation in my PPL Project.

  • Separate concern and high cohesion
Separate concern and high cohesion example

As you can see, in the second function I want to add some data after I create a new object called Riwayat. In Riwayat, there is an IP attribute that serves to tell us the ip of the user. Everytime Riwayat updated, we want the IP attribute updated too. So, I call this function called count_new_average_ip() to check user’s current IP. This is a separate concern application, where first function is used to calculate new IP, and the second function to add the datas after Riwayat created.

  • Never duplicate and don’t repeat yourself
calling returnExplore() based on role
Calling ExploreElement component
ExploreElement Component

React JS uses component-based architecture approach. We can use this approach to make components that we will use repeatedly separate themselves, thus forming many smaller components. That way, we don’t need to repeat our code to write the same thing, we just need to create a new component and provide some parameters as differentiating attributes in that component.

  • Choose descriptive name and comments efficiently

I always make sure that people will understand my code. As you can see, I named my variable buttonElement and responseElement, because I get button element from my screen, and response element from my screen too. This way, I don’t have to add unnecessary comments to explain my code.

Clean Code Benefits

Honestly, when I was just learning programming, I didn’t care about clean code. The important thing is that my code is finished. But as time goes by, lots of group projects are running, so I can’t help but have to read other people’s code, and it was disaster because my friends and I didn’t apply any clean code principles.

Writing clean code is important because it allows you to clearly communicate with your team, or you future team member. Being able to return to previously written code and understand what it does is really important, especially in the software development world.

That all from me about clean code, thanks for reading :D

References :

--

--

Johanes Jason
Johanes Jason

Written by Johanes Jason

A normal college student from Faculty of Computer Science, University of Indonesia

No responses yet