Language: EN


What is code indentation

Indentation refers to the technique of adding initial spaces (indented) at the beginning of code lines, which helps visually delimit blocks and control structures.

To indent, spaces or tabs are added at the beginning of code lines to highlight the hierarchy of control structures, such as loops, conditions, and functions.

Some people prefer tabs, some people prefer three spaces, some people prefer four. I’m not going to get into that debate, you wouldn’t imagine how radical some people get about it.

Personally, I don’t care. What is important is that you be consistent with the style you choose within the same project, and aligned with the style guidelines of the company or organization you collaborate with.

Proper indentation allows code to be easier to understand and maintain, as it helps to quickly identify code blocks and the relationships between them. It also facilitates error detection and improves collaboration on programming projects.

Non-significant indentation

Non-significant indentation is used in programming languages such as C#, Java, and C++. In these languages, code indentation has no semantic meaning and only has visual effects to improve readability.

In these languages, the code structure is defined by using braces {} or other specific keywords like end to delimit code blocks.

public static void CalcularDescuento(decimal precio)
	// <---- that indentation is merely visual
	if (precio > 100)
		decimal descuento = precio * 0.1m;
		decimal precioFinal = precio - descuento;
		Console.WriteLine("Se aplica un descuento del 10%");
		Console.WriteLine("No se aplica descuento");

In this case, the indentation in these languages is optional and is only a style convention used to improve the readability of the source code.

In fact, most IDEs provide autoformatting tools, which allow indentation to be applied automatically to the code, without having to do anything else but press a button or a key combination.

Significant indentation

On the other hand, significant indentation is used in languages such as Python or CoffeScript. In these languages, code blocks are defined by their own indentation, rather than using braces or other keywords to delimit them.

def calcular_descuento(precio):
	# here the indentation defines the block
    if precio > 100:
        descuento = precio * 0.1
        precio_final = precio - descuento
        print("Se aplica un descuento del 10%")
        print("No se aplica descuento")

In these languages, indentation is semantic, meaning it has significance. Therefore, it is mandatory and necessary, and using it incorrectly leads to syntax or execution errors.

Advantages and disadvantages

As is almost always the case, there is no better option than the other. Otherwise, both wouldn’t exist. Each has its advantages and disadvantages.

  • Significant indentation, as found in Python, can provide more readable and structured code, as it encourages good code organization practices.

    In addition, the code becomes more concise, as it does not depend on many delimiters that unnecessarily occupy space. Both factors contribute to making the code easier to understand and maintain.

  • Non-significant indentation, as in C# or Java, offers greater flexibility in code structure and adapts to different coding styles. This can be beneficial in specific scenarios and may be preferred by some programmers.

    In addition, with modern IDEs and auto-indentation features, you can forget about it. You just have to press a key to correctly format the text.

    But above all, it is much easier to copy and rearrange the code. With blocks delimited by keywords, you only have to copy or move the code, whereas otherwise you have to adjust tabulations.

In any case, the choice between non-significant indentation and significant indentation depends on the programming language and, to a large extent, on personal preferences and what we have become accustomed to. In any case, there are the two approaches.