Skip to content
What Formatters Solve
step 1/5

Reading — step 1 of 5

Read

~1 min readWhy Formatters

What Formatters Solve

A formatter rewrites source code in a canonical style. Same code, same formatting, every time.

input:    function f(x,y){return x+y}
output:   function f(x, y) {
            return x + y;
          }

Why?

  • Style consistency across a team: nobody argues over braces.
  • Readability: standard layout = familiar reading.
  • Diff clarity: a function rename doesn't reformat the file.
  • Speed: developers don't waste cycles aligning code.

Real formatters:

  • gofmt (Go): canonical, no options. Universal.
  • rustfmt (Rust): canonical with limited options.
  • prettier (JS/TS/CSS/MD): opinionated, multi-language.
  • black (Python): opinionated.
  • clang-format (C/C++): highly configurable.

Two design philosophies:

  1. No options (gofmt, black): one true style. Eliminates bikeshedding.
  2. Many options (clang-format): match team's existing style.

Trade-off: gofmt achieved universal Go style; clang-format achieved pluralism.

Wadler's "A Prettier Printer" (2003): elegant algebra for pretty-printing structured data. Foundation of prettier, and many functional pretty printers.

We'll build a Wadler-style pretty printer + apply it to a small language. The formatter takes an AST and renders it as text within a width budget.

Compare to a linter:

  • Linter: finds problems, suggests/applies fixes for specific issues.
  • Formatter: rewrites layout (whitespace + line breaks) only.
  • Both run in pre-commit / CI.

Discussion

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

Sign in to post a comment or reply.

Loading…