Clean Your Dirty Code

Christian Denata
3 min readMay 3, 2021

Dirty Codes

There are lots of programmers out there. But do all of them can write good codes? Well, most of them can write a working code however, those codes may not be easy to maintain.

  • Have you spent hours just to understand what your code does, even though it’s just a week ago?
  • Have trouble finding variables and functions you need when editing or improving your code?
  • There are similar codes that you implement just by copy-paste?

These are the symptoms of having dirty code. Every programmer can make these mistakes, and it’s not great to maintain the code for the long run.

Clean Code

The best practices to make codes easier to understand and make life easier when you have to maintain your code or maybe someone else.

The characteristics:

  • Comfortable to read
  • Easy to understand
  • Focused and unpolluted by other code
  • No duplications

Meaningful Names

Code should be created with the mind, other coders should be able to understand the code. This can be done by assigning names that fit what that component should be. It might take some time to think about the names, but it will save lots more time in the future. How do find out when the names are not meaningful enough?

  • The names not telling much information about them.
var a; // What is the meaning of the value?
function check(){…} // What is the function doing?
class A // What objects this class will make?
  • There is a need to use a comment to explain the component.
var t; # The value of the starting time
function check() {
// Check if user authenticated
...
}

These bad codes could be fixed, Eg:

var time_start;
function check_authentication(){}

Functions

When creating functions, they have to be small and simple. This can be done by implement a function to only done one thing it is meant to. By doing so, will make the functions reusable, preventing code duplication, and easier to maintain.

function combination(int total_num, int selected_num){
upper = 1;
for (num=(total_num); num > 1; num--):
upper = upper * num
lower = 1;
for (num=(total_num - selected_num); num > 1; num--):
lower= lower * num
for (num=selected_num; num > 1 num--):
lower= lower * num
return upper / lower
}

The example above shown 3 duplications of the permutation formulas inside the combination formula. When you clean the code, it can be like this:

function permutation(int number){
result = 1
for (num=(total_num); num > 1; num--):
result = result * num
return result
}
function combination(int total_num, int selected_num){
upper = permutation(total_num);
lower = permutation(total_num - selected_num) * permutation(selected_num)
return upper / lower
}

Practically the same code, but a lot easier to read and to understand what the code does.

Comments

Only use to compensate for the failure of expressing what the code does. If there is a need to write any comment, try to read your code and rewrite it without the need to give a comment.

Error Handling

In the process of programming, no matter what happens things could go wrong. That is when error handling comes to play, to understand what could be the problem with the code and prevent them when it is in production. These errors could be check by making tests to check if the code could handle unexpected inputs. Any errors that thrown need to be provided contexts and where it is failing to ease the maintain it.

--

--