Indentation

Verze:

02. 07. 2025

Zodpovědná osoba:

Dominik Šlechta

Poslední aktualizace:

03. 07. 2025, kocandajan488@gmail.com

This article lays out why tab-based indentation is the standard. It covers the benefits (like flexibility, consistency, and speed) and explains how using tabs (not spaces) keeps code clean and uniform across different editors. You'll also find best practices to avoid formatting issues in team projects.

Rule

Every code SHOULD be indented by a tab (the spacing of a tab does not play any role). This guideline ensures that the code is visually consistent across various development environments. The rationale behind using tabs over spaces for indentation is that a tab can be configured to display as any length the developer prefers (e.g., equivalent to 4 spaces, 8 spaces, etc.), ensuring flexibility and personalization in code appearance. This approach avoids the issues that arise with space-based indentation, where the physical length of indentation cannot be adjusted, potentially leading to inconsistency in visual code structure among different editors or IDEs.

Reason

Using tabs for indentation brings several advantages:

  • Flexibility: Developers can configure the width of the tab character in their editor, which allows them to view the code according to their visual preference without altering the actual code. This ensures that the code remains consistent no matter the editor or display settings.

  • Consistency: A tab is always a single character, ensuring consistent indentation across the codebase, regardless of the editor used. This avoids potential issues that arise when using spaces, where the visual appearance of indentation may differ based on editor configurations.

  • Efficiency: Inserting a single tab character is faster and more consistent than using multiple space characters, reducing potential errors and enhancing coding speed.

Example (With tabs):

function exampleFunction() {
\tif (condition) {
\t\t// Action 1
\t} else {
\t\t// Action 2
\t}
}

Example (With spaces):

function exampleFunction() {
    if (condition) {
        // Action 1
    } else {
        // Action 2
    }
}

Best Practices

  • Code Reviews: Focus on the logical structure of the code rather than the physical appearance of indentation. Editors and IDEs can be configured to display tabs as the developer prefers, ensuring the review process centers on code quality, not stylistic differences.

  • Editor Configuration: Ensure all team members configure their editors or IDEs to insert a tab character when the tab key is pressed, instead of spaces. This helps maintain consistency across the entire codebase.

  • Avoid Mixing Tabs and Spaces: Do not mix tabs and spaces for indentation. This can result in inconsistent code formatting, which may lead to confusion or errors.