Clean Code: A newbie journey learning clean code

Tsaqif Al Bari
Electronic Logbook
Published in
3 min readApr 4, 2021
Photo by Sarah Dorweiler on Unsplash

What is Clean Code?

Clean Code is a code that is easy to read and maintain. There are many ways to achieve this, but most of them follow the same principle. Here are some of it.

Meaningful Names

When writing your variables, function, object class, give them meaningful names. What does that variables keep, what does that function does, what does that object represent, and so on.

Simple Functions

Taken from mathematics, a function is a mapping that maps one element of a domain, to another, and so does our function should behave. A code function should do a simple task, a given input gives out an output. Even though our functions are a lot more complex than mathematics function, such as having the ability to retrieve values from global variable for additional inputs, we should minimize these because it adds hidden variables which makes our function harder to read and harder to debug. If a function that doesn’t read or write on any global variables and only use it’s given input to produce an output, we call them a pure function. It is the basics in Functional Programming that gives it an edge against Object-Oriented Programming. Learn from your rival, OOP.

Simple Execution Flow

In Object-Oriented Programming, we have objects that can interact with other objects to achieve a goal. This is a powerful tool that gives modularity and reusability to Object-Oriented Programming, but there’s a limit to it. To much objects switch calls with each other for an execution makes code harder to understand and higher dependencies with each other, one object fails or changed may break the whole execution easily. Keep it simple and have the flow in one direction.

Simple Writing

The reason why we have programming language is not just to communicate with machines, but also to communicate with others like us. Set up a rule to follow when writing codes, such as line breaks to separate functions, camel case for local variable names and upper case for global variable, 120 max character for one line, and so on. This rules help us to write a consistent code and makes it easier to read.

Test for leaks and breaks

Test your code daily, on every changes made, on every pull request, even on your break time. Testing your codes help us to maintain our code during development, and reviewing code daily also help us to improve our clean code. One way to make this easier is to make automated test-case.

At first, it is hard to implement Clean Code. Writing meaningful names are a hassle when you are trying to find out if your new algorithm will work or not. Object need to call 4 layers of other objects and why would you simplified it if it works. But when I got my PPL project which was to improve a project, I realize the benefit of Clean Code made by the previous team. Their code is easy to read since it is neatly written. If I have a hard time trying to understand a particular function, they have a test case for it. With that test case, I could try to modify the function to fail the test intentionally so I can have a better understanding of it. Each feature has it own MVC architecture making its flow simple and easy to understand and improve on.

--

--

Tsaqif Al Bari
Electronic Logbook

Computer Science Student in University of Indonesia. Likes to code and drink chocolate milk.