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­ciples 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­vant­ages 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­ciples 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­ately un­der­stood by any ex­per­i­enced developer. The following char­ac­ter­ist­ics of clean code are what make it easy to read:

  • The entire ap­plic­a­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­ately 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 modi­fi­able code has the following char­ac­ter­ist­ics:

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

The ad­vant­ages 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­ciples

But how does one actually write clean code? Writing clean code is about taking certain guiding prin­ciples 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­munit­ies of de­velopers. What is con­sidered clean to one group of de­velopers might be messy to another. Therefore, how clean a piece of code is will always be somewhat sub­ject­ive. In the following, you will find some es­tab­lished clean code prin­ciples that most de­velopers find useful.

Write code as simply as possible: KISS

KISS (Keep it simple, stupid) is one of the oldest prin­ciples 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­ne­ces­sar­ily 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­lated 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­ne­ces­sary re­pe­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­ne­ces­sary re­pe­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­ar­ately, they can be combined together in a single function. This turns the WET code with re­pe­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­ity 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­arch­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­ic­ally and in­di­vidu­ally. Clean code is, thus, always produced when the un­der­ly­ing problem has been solved in the most efficient way possible.

Read­ab­il­ity over con­cise­ness

Code needs to work and be un­der­stood by the machine executing it. However, other de­velopers also need to be able to un­der­stand the code, es­pe­cially if you are working on a project with multiple people. That is why the read­ab­il­ity 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­velopers 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­plan­at­ory. The following variable is not un­der­stand­able without some back­ground knowledge and an ex­plan­a­tion:

int d;

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

int elapsedTimeinDays;
Go to Main Menu