Skip to main content

Code Formatter

Updated 3 min read

Share this page

Send the link, quote the definition with a link back, or show it as a card on your own site.

https://softwaredictionary.org/terms/code-formatter

In short

A code formatter is a tool that rewrites source code into one consistent layout, fixing indentation, spacing and line breaks without changing what it does.

What is a code formatter?

A code formatter takes source code and rewrites its layout to follow one consistent style: indentation, spaces around operators, where lines break, quote marks, trailing commas. What the program does never changes, only how the text looks. Widely used examples include Prettier for JavaScript, TypeScript, CSS and more, gofmt for Go, Black and Ruff for Python, rustfmt for Rust and clang-format for C and C++.

Most modern formatters don't patch the existing text line by line. They parse the code into a syntax tree, throw the original layout away and print the code again from the tree with fixed rules, choosing line breaks to fit a maximum width. Many are deliberately opinionated, with few settings: gofmt has none for style, which is why almost all Go code looks alike. Developers usually run the formatter on save in the editor, in a pre-commit hook, and in CI in a check mode, such as prettier --check or black --check, that fails when a file isn't formatted.

The main benefit is social: style debates and whitespace comments disappear from code review, and diffs show only real changes. It works like a publisher's house style applied automatically, so every author's chapters come out with the same fonts and margins. Teams adopting a formatter on an existing project usually reformat everything in one commit and list it in a .git-blame-ignore-revs file, so blame views can skip it and still show who really wrote each line.

A formatter is often confused with a linter. A linter analyzes code for likely bugs and questionable patterns, such as an unused variable or a missing await, and reports them; a formatter only changes layout and never judges the logic. Teams commonly run both, with the linter's style rules turned off so the two don't fight, and ESLint deprecated its own formatting rules in 2023 for this reason. Some newer tools, such as Ruff and Biome, ship a linter and a formatter together.

Key takeaways

  • A code formatter rewrites code layout automatically and never changes behavior.
  • Most parse the code and print it again from scratch with fixed rules.
  • Opinionated formatters such as gofmt, Prettier and Black end style debates in code review.
  • They run on save, in pre-commit hooks and as a check in CI.
  • A linter finds likely bugs; a formatter only fixes layout.

Example

The same code before and after Prettier with its default settingsjavascript
// Before: valid code, written in whatever style the author liked
const user={name:'Ada',roles:[ 'admin','dev' ],active:true}
function greet(u){return 'Hello, '+u.name}

// After running `npx prettier --write user.js`
const user = { name: "Ada", roles: ["admin", "dev"], active: true };
function greet(u) {
  return "Hello, " + u.name;
}

Readers ask

What is the difference between a linter and a formatter?

A linter looks for likely bugs and risky patterns and reports them, while a formatter only rewrites layout such as indentation and line breaks. Many projects use both and let the formatter own every layout decision.

Can a code formatter break my code?

It is designed not to: a formatter only changes layout details such as whitespace, quotes and parentheses, and some, including Black, check that the reformatted code still parses to an equivalent syntax tree. Bugs are rare, and running the tests after reformatting a large codebase is a cheap safety net.

Which code formatter should I use?

Usually the one your language community has settled on: gofmt for Go, rustfmt for Rust, Prettier or Biome for JavaScript and TypeScript, and Black or Ruff for Python. Agreeing on one formatter as a team matters more than which one you pick.

See also

Sources

Spotted a mistake or something missing on this page?Suggest an edit

More

Settings