The concept of clean code can be traced back to Robert Cecil Martin and his book ”Clean Code: A Handbook of Agile Software Crafts­man­ship” where it was presented as a term for cleanly written code. However, the prin­ci­ples of clean code are much older and did not actually originate in software de­vel­op­ment. We will explain what clean code is, what its ad­van­tages are, and how to write clean code yourself.

What is clean code?

Clean code is not a set of strict rules. It is a set of prin­ci­ples for writing code that is easy to un­der­stand and modify. In this case, “un­der­stand­able” means that the code can be im­me­di­ate­ly un­der­stood by any ex­pe­ri­enced developer. The following char­ac­ter­is­tics of clean code are what make it easy to read:

  • The entire ap­pli­ca­tion’s order of execution is logical and clearly struc­tured.
  • The con­nec­tion between different parts of the code is quite obvious.
  • The task or role of each class, function, method, and variable is im­me­di­ate­ly un­der­stand­able.

Code is easy to modify if it can be adapted and extended. This also makes it easier to correct errors in the code. Clean code is thus very easy to maintain. Easily mod­i­fi­able code has the following char­ac­ter­is­tics:

  • Classes and methods are small and only have one single task.
  • Classes and methods are pre­dictable, work as expected, and are publicly available through well-doc­u­ment­ed APIs (in­ter­faces).
  • The code uses unit tests.

The ad­van­tages of this kind of pro­gram­ming are obvious. Clean code does not depend on the original developer. In principle, any pro­gram­mer can work with the code. This prevents problems that may occur when working with legacy code, for example. It is also easier to maintain the code because bugs can be iden­ti­fied and fixed more easily.

Overview of clean code prin­ci­ples

But how does one actually write clean code? Writing clean code is about taking certain guiding prin­ci­ples into con­sid­er­a­tion when pro­gram­ming. It is less about having concrete in­struc­tions on what to program in a specific situation and more about re­flect­ing on one’s own de­vel­op­ment work practices. But what clean code means in concrete terms is a highly debated topic among the com­mu­ni­ties of de­vel­op­ers. What is con­sid­ered clean to one group of de­vel­op­ers might be messy to another. Therefore, how clean a piece of code is will always be somewhat sub­jec­tive. In the following, you will find some es­tab­lished clean code prin­ci­ples that most de­vel­op­ers find useful.

Write code as simply as possible: KISS

KISS (Keep it simple, stupid) is one of the oldest prin­ci­ples of clean code. It was being used by the US military as early as the 1960s. KISS en­cour­ages pro­gram­mers to write their code as simply as possible. You should avoid making your code un­nec­es­sar­i­ly complex. When it comes to pro­gram­ming, there is never a single way to solve a problem. A task can always be executed using different languages and for­mu­lat­ed using different commands. Pro­gram­mers who follow the KISS principle must, therefore, always ask them­selves whether there is an easier way to solve a par­tic­u­lar problem.

Avoid un­nec­es­sary rep­e­ti­tion: DRY

DRY (Don’t repeat yourself) is a more specific version of KISS. According to the DRY principle, functions in clean code should only do one thing within the overall system.

Note

The opposite of DRY is WET (We enjoy typing). Code is WET when there are un­nec­es­sary rep­e­ti­tions in the code.

The following is an example of the DRY clean code principle: The username and password are retrieved twice in the code and then used for different actions. Instead of pro­gram­ming both processes sep­a­rate­ly, they can be combined together in a single function. This turns the WET code with rep­e­ti­tions into DRY code.

WET code:

//Version A
let username = getUserName();
let password= getPassword();
let user = { username, password};
client.post(user).then(/*Version A*/);
//Version B
let username = getUserName();
let password= getPassword();
let user = { username, password};
client.get(user).then(/*Version B*/);

DRY code:

function getUser(){
    return {
        user:getUserName();
        password:getPassword();
    }
}
//Version A
client.post(getUser()).then(/*Version A*/ );
//Version B
client.get(getUser()).then(/*Version B*/);

Delete what is not needed: YAGNI

The clean code principle YAGNI (You aren’t gonna need it) is based on the following idea: a developer should only introduce ad­di­tion­al func­tion­al­i­ty to code when it is necessary. YAGNI is closely tied to agile software de­vel­op­ment methods. According to the YAGNI principle, instead of starting from an over­ar­ch­ing concept during de­vel­op­ment, you should program the software ar­chi­tec­ture in small steps to be able to react to problems dy­nam­i­cal­ly and in­di­vid­u­al­ly. Clean code is, thus, always produced when the un­der­ly­ing problem has been solved in the most efficient way possible.

Read­abil­i­ty over con­cise­ness

Code needs to work and be un­der­stood by the machine executing it. However, other de­vel­op­ers also need to be able to un­der­stand the code, es­pe­cial­ly if you are working on a project with multiple people. That is why the read­abil­i­ty of code is always more important than its con­cise­ness when it comes to software de­vel­op­ment. There is no point in writing concise code if other de­vel­op­ers cannot un­der­stand it. A good example of producing readable clean code can be found in the naming of variables.

A variable name should always be self-ex­plana­to­ry. The following variable is not un­der­stand­able without some back­ground knowledge and an ex­pla­na­tion:

int d;

In contrast, providing the following name for the same variable makes its meaning clear:

int elapsedTimeinDays;
Go to Main Menu