Skip to content
Indentation Tracking
step 1/5

Reading — step 1 of 5

Read

~1 min readWhy Formatters

Indentation Tracking

Most code is hierarchically indented. The formatter tracks current indentation level.

python

Indentation step: 2 or 4 spaces (or tabs). Configurable.

Continued lines (when an expression wraps):

result = some_long_function(
    argument1,
    argument2,
    argument3,
)

Continued indent ≠ block indent. Often 4 spaces vs 2 for blocks. Or aligned with the opening paren:

result = some_long_function(argument1,
                            argument2,
                            argument3)

Alignment-based indent has a cost: editing the function name shifts ALL aligned content. Diff-noisy.

Hanging indent (more diff-friendly):

result = some_long_function(
    argument1,
    argument2,
)

Most modern formatters prefer hanging.

Multi-level expressions:

result = a + b * (
    c + d * (
        e + f
    )
)

Each nesting adds an indent level.

For Python (indent-significant):

  • Indent must be consistent within a block.
  • Mixing tabs and spaces is forbidden.

For most other languages, indent is purely cosmetic — the formatter chooses.

Indent settings to consider:

  • Tab vs spaces: many wars. Black/prettier/gofmt use spaces (or tabs in case of Go).
  • Width: 2 (Java/JS), 4 (Python/C++).
  • Continuation indent: usually = base indent or 2x.

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…