Clean code: what makes programming code clean

The concept of clean code can be traced back to Robert Cecil Martin and his book ”Clean Code: A Handbook of Agile Software Craftsmanship” where it was presented as a term for cleanly written code. However, the principles of clean code are much older and did not actually originate in software development. We will explain what clean code is, what its advantages 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 principles for writing code that is easy to understand and modify. In this case, “understandable” means that the code can be immediately understood by any experienced developer. The following characteristics of clean code are what make it easy to read:

  • The entire application’s order of execution is logical and clearly structured.
  • The connection between different parts of the code is quite obvious.
  • The task or role of each class, function, method, and variable is immediately understandable.

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 modifiable code has the following characteristics:

  • Classes and methods are small and only have one single task.
  • Classes and methods are predictable, work as expected, and are publicly available through well-documented APIs (interfaces).
  • The code uses unit tests.

The advantages of this kind of programming are obvious. Clean code does not depend on the original developer. In principle, any programmer 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 identified and fixed more easily.

Overview of clean code principles

But how does one actually write clean code? Writing clean code is about taking certain guiding principles into consideration when programming. It is less about having concrete instructions on what to program in a specific situation and more about reflecting on one’s own development work practices. But what clean code means in concrete terms is a highly debated topic among the communities of developers. What is considered clean to one group of developers might be messy to another. Therefore, how clean a piece of code is will always be somewhat subjective. In the following, you will find some established clean code principles that most developers find useful.

Write code as simply as possible: KISS

KISS (Keep it simple, stupid) is one of the oldest principles of clean code. It was being used by the US military as early as the 1960s. KISS encourages programmers to write their code as simply as possible. You should avoid making your code unnecessarily complex. When it comes to programming, there is never a single way to solve a problem. A task can always be executed using different languages and formulated using different commands. Programmers who follow the KISS principle must, therefore, always ask themselves whether there is an easier way to solve a particular problem.

Avoid unnecessary repetition: 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 unnecessary repetitions 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 programming both processes separately, they can be combined together in a single function. This turns the WET code with repetitions 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 additional functionality to code when it is necessary. YAGNI is closely tied to agile software development methods. According to the YAGNI principle, instead of starting from an overarching concept during development, you should program the software architecture in small steps to be able to react to problems dynamically and individually. Clean code is, thus, always produced when the underlying problem has been solved in the most efficient way possible.

Readability over conciseness

Code needs to work and be understood by the machine executing it. However, other developers also need to be able to understand the code, especially if you are working on a project with multiple people. That is why the readability of code is always more important than its conciseness when it comes to software development. There is no point in writing concise code if other developers cannot understand it. A good example of producing readable clean code can be found in the naming of variables.

A variable name should always be self-explanatory. The following variable is not understandable without some background knowledge and an explanation:

int d;

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

int elapsedTimeinDays;
Was this article helpful?
Page top