Indentation refers to the technique of adding an initial space (indented) at the beginning of lines of code, which helps visually delimit blocks and control structures.
To indent spaces or tabs are added at the beginning of lines of code to highlight the hierarchy of control structures, such as loops, conditions, and functions.
Proper indentation makes the code easier to understand and maintain, as it helps quickly identify code blocks and the relationships between them. Additionally, it facilitates error detection and improves collaboration in programming projects.
Some people prefer tabs, some people prefer three spaces, some people prefer four. I won’t enter that debate (you wouldn’t imagine how radical some become about it).
Personally, I don’t care. What is important is that you are consistent with the style you choose within the same project, and aligned with the style guidelines of the company or organization you collaborate with.
Non-significant Indentation
Non-significant indentation is used in programming languages like C#, Java, and C++. In these languages, the indentation of the code has no semantic meaning and only has visual effects to improve readability.
In these languages, the structure of the code is defined using braces {}
or other specific keywords like end
to delimit code blocks.
public static void CalculateDiscount(decimal price)
{
// <---- that indentation is purely visual
if (price > 100)
{
decimal discount = price * 0.1m;
decimal finalPrice = price - discount;
Console.WriteLine("A discount of 10% is applied");
}
else
{
Console.WriteLine("No discount is applied");
}
}
In this case, the indentation in these languages is optional, and it is merely a style convention used to improve the readability of the source code.
In fact, most IDEs provide auto-formatting tools, which allow you to apply indentation automatically to the code, without having to do anything other than press a button or a key combination.
Significant Indentation
On the other hand, significant indentation is used in languages like Python or CoffeScript. In these, code blocks are defined by the indentation itself, rather than using braces or other keywords to delimit them.
def calculate_discount(price):
# here the indentation defines the block
if price > 100:
discount = price * 0.1
final_price = price - discount
print("A discount of 10% is applied")
else:
print("No discount is applied")
In these languages, indentation is semantic, meaning it has significance. Therefore, it is mandatory and necessary, and using it incorrectly leads to syntax or runtime errors.
Advantages and Disadvantages
As is almost always the case, there is no better option than another (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 promotes good coding organization practices.
Additionally, the code becomes more concise, as it does not rely on so many delimiters that unnecessarily occupy space. Both factors contribute to facilitating understanding and maintenance of the code. - Non-significant indentation, as in C# or Java, offers greater flexibility in the structure of the code and adapts to different coding styles. This can be beneficial in specific scenarios and may be preferred by some programmers.
Furthermore, with modern IDEs and auto-indentation features, you can stop worrying about it. You only have to press a key to correctly format the text.
But above all, it is much easier to copy and rearrange code. With blocks delimited by keywords, you only have to copy or move the code, whereas otherwise, you have to adjust tabs.
In any case, the choice between non-significant indentation and significant indentation depends on the programming language and, to a large extent, personal preferences and what we have become accustomed to.