Clean your code beyond code clean!

Priagung Satria
3 min readMay 2, 2021

What is clean code?

https://unsplash.com/photos/ieic5Tq8YMk

It is a state where the code looks flawless, it has Simple and direct lines, Looks neat, efficient, easy to read, easy to extend or enhance, and looks like well-written prose. There are few general principles that are used such as, DRY(Don’t Repeat Yourself), Law of Demeter, Conventions, SOLID Principle, and also Design Pattern.

Meaningful names

Code is hard to understand, so don’t make it harder for others to understand your intention.
There are some best practice to name a variable as follows:

some variable that are intention revealing, pronounceable, and searchable.
  • Use Intention Revealing names
    It is a lot easier to create a variable by what it holds, its intention than a mere temporary name (x, y, z, etc).
  • Use Pronounceable Names
    It is hard to remember an abbreviation for a variable, it is also harder for others to read and sometimes there will be a miscommunication or worse a typo on the variable itself.
  • Use Searchable names
    it is a lot quicker to search a variable using the search function. Therefore it would be best to use these as much as possible especially on larger projects.
  • Picking one word perconcept for a project
    Creating a word for a concept on a large project that includes a lot of developers is a must, this can prevent a multiple variable that has the same intention for example (fetch,get,etc) that has the same purpose.
using toInfo for fetching a user (admin,student,lecturer)

Functions

a function to check the token of the user.

Functions are small, create to do one thing, have a descriptive name for its purpose, have fewer arguments as possible, and also should not have a side effect.

Classes

Classes are also similar in principle to a function, which is small, has a single responsibility and lastly open for extension but closed for modification.

Tests

a test created to check if a user is successfully created.

The unit tests that will be often repeated to check for any vulnerabilities.

  • Fast
    Tests are supposed to run quick as it will be repeated often.
  • Independent
    Tests are independent to each other and should not be a chain, or pre-requisite to another test.
  • Repeatable
    Tests should produce the same result and repeatable in any environment.
  • Self-Validating
    Tests should only have a boolean value which is true or false, and we as the developer should know which test that fails.
  • Timely
    Tests should be written before creating the code to make it pass.

Linter

This topic is an extension as it is commonly used nowadays, Linter is a tool that analyzes source code to flag programming error and typos.
This tool also evolved from doing a simple check to create a standardized code there are few benefit of using linter such as improves code review discussion, as there will be no typos created the discussion will focus on the code not the writing and makes code to look like written by the same person, as the linter will be standardized.

What have we apply it on

These knowledge of clean code will always be our pillar on creating code for our on-going project.
For example the general principles of dry, naming conventions are always prioritized, And based on the last batch linter, we also still maintain it by using the provided linter while doing the logbook project.

This is all from me,
I hope this article will be helpful to anyone

Thank you for reading :)

--

--