+5 votes
185 views
in Web development by (242k points)
reopened
Clean code: principles, benefits and examples

1 Answer

+3 votes
by (1.6m points)
edited
 
Best answer

Clean code: what is it?
Clean code: general principles
As simple as possible: KISS
Avoid repeating for no reason: DRY
Eliminate the unnecessary: ​​YAGNI
Legible rather than concise

image

Clean code: principles, benefits and examples

The term clean code is attributed to software engineer Robert Cecil Martin, who used it in his book Clean Code: Refactoring, Patterns, Testing and Techniques for Clean Code to refer to clean code . However, its principles are much older and do not come from the field of programming. We explain what the clean code consists of , what its advantages are and how to develop it..

Index
  1. Clean code: what is it?
  2. Clean code: general principles
    1. As simple as possible: KISS
    2. Avoid repeating for no reason: DRY
    3. Eliminate the unnecessary: ​​YAGNI
    4. Legible rather than concise

Clean code: what is it?

The code clean is not a strict set of rules, but a set of principles that help produce intuitive and easy to modify code. In this context, intuitive means that any professional developer can understand it immediately. An easily adaptable code has the following characteristics:

  • The sequence of execution of the entire program follows a logic and it has a simple structure .
  • The relationship between the different parts of the code is clearly visible .
  • The task or function of each class, function, method, and variable is understandable at first glance .

A code is considered easy to modify when it is flexible and extensible, which also helps to correct any errors it may have. Therefore, the clean code is very easy to maintain and has the following properties:

  • Classes and methods are reduced and, if possible, have a single clear task.
  • Classes and methods are predictable , work as expected, and are publicly accessible via well-documented APIs (interfaces).
  • The code has been  unit tested .

The advantages of this type of programming are obvious: the clean code becomes independent of the developer who created it . In principle, any programmer can work with it, which avoids problems such as those associated with legacy code. The maintenance of the software is also simplified , because the bugs are easier to find and fix.

Clean code: general principles

How to write clean code? Creating clean code involves keeping certain fundamental principles in mind during software development . It is not about following specific instructions that indicate how to program in certain situations, but rather about self-reflection on your own work. For this reason, its meaning arouses controversy in the developer community: what some consider? Clean ?, can be? Dirty? for others, so the cleanliness of the code ends up being somewhat subjective. Here are some very popular clean code principles that almost all developers find useful..

As simple as possible: KISS

KISS (from English k eep i t s imple , s bushy or? Keep it simple, stupid?) Is one of the oldest clean code principles, already in use by the US military in the 1960s. KISS reminds developers that the code should be as simple as possible , avoiding any unnecessary complexity. In programming, there is never a single way to solve a problem. Tasks can always be expressed in different languages ​​and formulated with different commands. Therefore, programmers who follow the KISS principle should always wonder if they could come up with a simpler solution to a particular problem.

Avoid repeating for no reason: DRY

DRY (from English d on? t r epeat and ourself , or? Don't repeat yourself?) is a KISS realization. According to the DRY principle, each function must have a unique and therefore unambiguous representation within the general clean code system.

Note

The opposite of DRY is WET ( w e e njoy t yping or? We had a good typing?). The code is WET when it contains unnecessary duplications..

With the following example, you will see more clearly the DRY principle of the clean code: suppose that the username and password appear twice in the code to be used in different actions. Instead of programming them separately, both processes can be grouped into one function . In this way, the WET (? Wet?) Code, with its redundancies, will become DRY (? Dry?) Code.

WET code:

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

DRY code:

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

Eliminate the unnecessary: ​​YAGNI

The YAGNI principle ( y ou a ren? T g onna n eed i t o? You won't need it?) Of clean code is based on the following idea: a developer should only add functions to code when strictly necessary . YAGNI is closely related to agile software development methods. According to this principle, instead of starting to program from a general concept, the software architecture is developed step by step to be able to react to each problem dynamically. In other words, clean code is created when the underlying problems are solved as efficiently as possible .

Legible rather than concise

Not only must the code work and be interpreted by the machine that runs it, it must also be understandable to other developers , especially if working on collaborative projects. Therefore, in the realm of software development, the readability of the code is always more important than its conciseness: there is no point in writing concise code if other developers do not understand it. A good example of creating human-readable code would be naming variables.

The variable names should always be understandable. For example, it is not possible to understand the following variable without an explanation or more information:

  int d;  

However, under the following name, the same variable is self-explanatory:

  int elapsedTimeinDays;  

...