Reading — step 1 of 5
Read
~1 min readWadler's Algebra
Alignment vs Hanging Indent
Two indentation styles when wrapping:
Aligned (column-based):
result = some_function(arg1,
arg2,
arg3)
After (: indent to column 23. All wrapped lines align there.
Hanging (block-based):
result = some_function(
arg1,
arg2,
arg3,
)
Open ( at end of line; body indented one block; close ) on its own line.
Trade-offs:
- Aligned: visually clean. But editing function name shifts alignment → noisy diff.
- Hanging: less aligned but stable across edits.
Modern formatters favor hanging. Black, prettier, rustfmt all use hanging by default.
In Doc algebra:
align(d): indent contents to current column.nest(i, d): indent contents by i more than parent.
Aligned wraps:
python
Hanging wraps:
python
The group decides: try one-line; if not, break + use the nested layout.
Which to use? Per-language style guide:
- Python (PEP 8): allows either; black picks hanging.
- Go: gofmt's choice (different per construct).
- C++: clang-format configurable.
- Lisp: aligned (parens are sacred).
Reformatting from aligned to hanging or vice versa is mechanical; AST-based formatters handle it trivially.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…