A code formatter is a development tool that automatically rewrites the layout of source code (indentation, line breaks, spacing, quotes and bracket placement) to match a defined style, without changing the behavior of the program. Among development tools in programming, it is also called a code beautifier or auto-formatter, and well-known examples include Prettier, Black, gofmt, rustfmt and clang-format.
The purpose is to make style a solved, automatic question. When every file is formatted by the same tool, code reviews focus on logic instead of whitespace, diffs stay small and readable, and a new team member produces code that looks like everyone else’s from the first commit.
How a code formatter rewrites source code
Modern formatters do not simply adjust whitespace line by line. They parse the source into a syntax tree, discard the original layout and then print the tree again according to their rules. This approach, sometimes called pretty-printing, guarantees that the output depends only on the code structure, not on how the author happened to type it.
The hardest part is line breaking. When an expression exceeds the configured line width, the formatter must decide where to split it. Tools such as Prettier and rustfmt use algorithms derived from Philip Wadler’s pretty-printer design, which try the flat layout first and fall back to progressively broken layouts until the code fits.
The table below shows the stages that a typical tree-based formatter runs on every file.
| Stage | What happens | Why it matters |
|---|---|---|
| Parsing | Source text becomes a syntax tree | Invalid syntax stops formatting safely |
| Comment attachment | Comments are linked to nearby nodes | Prevents comments from moving or vanishing |
| Layout planning | Rules choose groups and break points | Produces consistent line breaks |
| Printing | The tree is emitted as text | Same input always yields same output |
| Verification | Some tools reparse the output | Confirms the code meaning is unchanged |
Opinionated and configurable formatters
Formatters differ mainly in how many options they expose. Opinionated tools offer very few settings on purpose, so that teams stop debating style; configurable tools let a team reproduce an existing house style in detail. Both kinds share the same core property, idempotence: running the formatter twice yields the same result as running it once.
| Formatter | Languages | Configurability |
|---|---|---|
| Prettier | JavaScript, TypeScript, CSS, HTML, Markdown | Low, a handful of options |
| Black | Python | Very low, mainly line length |
| gofmt | Go | None, a single official style |
| rustfmt | Rust | Moderate, stable and unstable options |
| clang-format | C, C++, Objective-C, Java | High, dozens of style keys |
| ktlint and ktfmt | Kotlin | Low to moderate |
Go’s gofmt is often cited as proof of the opinionated approach: because the language ships with one canonical format, virtually all public Go code looks the same, and style discussions in Go reviews are rare.
Code formatter versus linter and editor settings
A code formatter is frequently confused with a linter, but the two tools answer different questions. A formatter decides how code looks, whereas a linter detects what code does wrong, such as unused variables, suspicious comparisons or insecure calls. Many teams run both, and modern linters increasingly drop pure style rules so they do not conflict with the formatter.
| Tool | Main question | Changes code | Typical output |
|---|---|---|---|
| Code formatter | Is the layout consistent | Yes, layout only | Rewritten files |
| Linter | Is the code likely wrong or risky | Sometimes, with autofix | Warnings and errors |
| EditorConfig | Which basic whitespace settings apply | No, guides the editor | Indent and line-ending hints |
| IDE auto-indent | Where the cursor should start | Only while typing | Indented new lines |
Editor features such as auto-indent help while typing but do not guarantee consistency across a whole repository. Only a formatter run on every file produces a reproducible result that does not depend on which editor a developer uses.
Integrating a formatter into the workflow
A formatter delivers full value only when it runs automatically at several points, so that unformatted code never reaches the main branch. The usual setup combines editor integration for instant feedback with enforcement before and after each commit.
| Integration point | Mechanism | Benefit |
|---|---|---|
| Editor on save | Plugin in Visual Studio Code or IntelliJ IDEA | Immediate feedback while writing |
| Pre-commit hook | Git hooks run the formatter on staged files | Blocks unformatted commits locally |
| Continuous integration | A check mode fails the build on differences | Enforces style for every pull request |
| One-time migration | Formatter runs over the whole codebase | Removes legacy style differences at once |
Teams should pin the formatter version in the project configuration, for example in the npm lockfile or a requirements file. Different versions may format the same code slightly differently, which causes noisy diffs whenever two developers use different installations.
Limitations and common mistakes
The most common problem appears when a formatter is introduced into an existing project. A single commit that reformats thousands of lines pollutes the blame history, making it harder to see who last changed a line for a real reason. Git can ignore such commits through a dedicated ignore-revisions file, and the migration should be a separate commit with no logic changes.
- Formatting mixed with features: reviewers cannot see the real change inside a large layout diff.
- Conflicting tools: a linter style rule and the formatter keep rewriting each other’s output.
- Manual alignment: hand-aligned tables of constants are collapsed unless an ignore comment protects them.
- Unformatted generated code: generated files should be excluded or formatted consistently to avoid churn.
- Merge conflicts after migration: long-lived branches must be reformatted before merging.
A formatter also cannot improve naming, structure or readability of logic. It removes arguments about spacing, but code review is still needed for everything that a syntax tree cannot judge.
A non-technical view
A code formatter is like an automatic tidy-up tool for program text. Programmers write in slightly different ways: some add more spaces, some break lines in other places. The formatter rearranges everything into one neat, agreed style.
It never changes what the program does, only how the text looks. The result is that a whole team’s code reads as if one person wrote it, which makes it easier to read, review and fix.
A concrete example
A web agency maintains a TypeScript application of about 180,000 lines written by eight developers over five years. Code reviews regularly stall on comments about quotes, trailing commas and indentation, and many pull requests contain hundreds of lines of accidental whitespace changes.
The team adopts Prettier with its default settings, runs it once over the entire repository in a dedicated commit, and lists that commit in the Git ignore-revisions file. They add a pre-commit hook and a continuous integration check that fails on unformatted files. Within a month, the average pull request diff shrinks by roughly a third, and style comments almost disappear from code reviews, leaving reviewers free to focus on logic and tests.