Keep Things Clean, Even Your Codes

Rhendy rivaldo
Akhirnya
Published in
5 min readMar 11, 2020

A huge meteor is coming right towards Earth. You are assigned by NASA to create a program that calculates how many time left until it finally hits the Earth. You are so close to finish the program, but unfortunately due to panic, you suddenly get sick and went to coma.

Another programmer is assigned to continue your work. He read your codes, and then he was like “What the — What is this?”. He can’t understand a single thing you write. He starts over and guess what? By the time he finish the code, he notice the sky is brighter. The meteor already entered the atmosphere. Hasta la vista, baby.

Overview

The above example illustrates how something as simple as clean code can have a great impact later in the future. But what is clean code anyway? Clean code is a code that is easy to read and understand. Why do we have to make a clean code? Well an example has already provided above (even though its not very realistic — what’s so hard about estimating impact time, with all the physicist they got there?). A program we made may not only be read by ourselves, but also our fellow co-worker for continuous development of the program. Consider developers as an author. An author always write a book that tells clear stories. Developers work similarly, only in this case they are writing codes, and the developer’s team are the readers. Beside this, here’s why clean code is important:

  1. It’s just simply needed — in the recent years, the TDD (check my post about TDD here), has gained a lot of attention between programmers. Clean code is the main factor that can make a program easier to test. Why? Because every little part of the program is tested and if the code structure is messy, then there must be a lot of bugs emerge during the test. To fix them, it would feel like you are starting it all over again.
  2. Prevent misunderstanding — when you are working in team, the use of variables, functions, and classes names is as important as solving the problem. Everyone in the team must follow the rules agreed by every team member in the start.
  3. Time saving — someday when you look back at your code and wonder what that code does, if you have already implemented clean code then you will remember what it does in no time. It’ll all comes back in mind all of a sudden. Similar thing may happen to your successor in the business.
  4. Keep everything simple — a clean code supports Single Responsibility Principle, meaning that every class or function must have a single functionality. Even though the code may have more lines, it becomes more simple since every function only executes one job and the other job needed to be done already defined in the other function.

There are, of course, many more advantages of implementing clean codes. Attracting recruiters is obviously in the list. Everyone likes people that looks neat, even more when their codes looks neat too, right?

Principles of Clean Codes

  1. Single Responsibility Principle (SRP) — as mentioned above, it states that every class or function must have a single functionality. You wouldn’t want to have a class that will execute the whole program in one call, you want to keep it separated to keep it more understandable
  2. Keep It Simple Stupid (KISS) — meaning that most systems should be kept as simple as possible. Unnecessary complexity should be avoided. The question to ask when you’re writing code is more like “can this be written in a simpler way?” than “can I finish this as fast as I can?”
  3. Don’t Repeat Yourself (DRY) — It states that every piece of code must have a single, unambiguous, authoritative representation within a system. Violations of DRY are referred to as Waste Everyone’s Time (WET).
  4. You Ain’t Gonna Need It (YAGNI) — A developer should not add functionality unless deemed necessary. This should be used in conjunction with continuous refactoring, unit testing, and integration.
  5. Give A Reasonable Name (GARN) — Every name of a class, function, or variables should represent what is in it and what is the class or function will do.

Brief Examples of Clean Codes

Since I am working on Python based framework for my Software Project course, I will provide some example of clean codes in Python.

a = datetime.date.today().strftime("%y-%m-%d")

Now, look at this one

current_date = datetime.date.today().strftime("%y-%m-%d")

Just by looking at the variable name, which one tells you better about what is stored in the variable? You tell me.

Another example: Follow the language’s standard conventions.

There are no constant type variables in Python. But there’s a naming convention that states a constant variables (like Pi, gravity, speed of light) usually named with uppercase:

PI = 3.14
G = 9.8
C = 300000

Besides, are you comfortable to write 300000 every time you are calculating something far, far away out there?

Why Developers Won’t Do It

Making a clean codes may provide a lot of benefits, but don’t forget that it was all in the future. Most developers don’t even have enough spirit about it to begin with. Say you are working with some developer from an outsourcing platform who gets paid very few amount of money per hour. He wouldn’t even have the motivation to do a good job. Your product may be deployed, but in no time bugs will start popping that could’ve been avoided from the start.

For starters, most of them may not have the basics of clean codes. Take an example of a student developer. Mainly, their focus is to get a good grade without considering how the code is written. Trying to pass the test cases and meet the task’s requirements is what they usually care the most. If the lecturer don’t highlight the word ‘clean code’ in the task’s description, chances are the students won’t even bother to do a research about it.

And the excuse most ‘lazy’ programmers (like me) use is the lack of time. Writing bad codes takes a lot less time since our only goal is that the feature is implemented. Making a clean codes take a lot more time and not to mention it requires more carefulness in writing it line by line. When developers faced with tight deadlines, bad codes might be their only option. This is where the managers should be the knight in shining armor to offer generous deadlines (or even extending the deadlines) so the developers can take their time to write a clean one.

References

https://medium.com/mindorks/how-to-write-clean-code-lessons-learnt-from-the-clean-code-robert-c-martin-9ffc7aef870c

https://www.pluralsight.com/blog/software-development/7-reasons-clean-code-matters

poatek.com/2017/07/31/clean-code-what-it-is-and-why-its-important/

https://github.com/zedr/clean-code-python

--

--