
Formatter
A formatter is a program that automatically brings program text into a uniform form according to fixed rules – such as indentation, line breaks, and spacing. It does not change the content of the program, only its appearance.
Programs are written by humans as text. This text not only has to be correct, it should also be readable. How far a line is indented, where a line break sits, whether there is a space after a comma: all of this is usually irrelevant to the computer, but not to the reader. A formatter is a helper program that automatically produces exactly this external form. It reads in the text and rewrites it according to fixed rules, without changing its meaning. The result looks the same throughout an entire project, no matter who typed the line.
Why teams no longer argue about indentation
In a software project, dozens of people often work on the same files. Everyone has their own habits: one person indents with two spaces, another with four. Without uniform rules, a patchwork emerges. The text becomes harder to read, and readability is no luxury when it comes to programs. Anyone looking for a bug needs to be able to grasp the structure of the text at a glance.
On top of that comes a practical problem. Developers manage their files in a version control system, that is, a system that records every change. There you can see, line by line, what has changed. If someone incidentally adjusts the indentation of a hundred lines, the actually important changes get lost in the noise. A formatter prevents this because everyone involved produces the same form.
Perhaps the greatest benefit is saved time. Discussions about brackets and spaces are notorious in the industry and completely unproductive. Once a team settles on a formatter, the question is decided. You don’t have to like the rules, you just no longer have to negotiate them.
From text to tree and back
A formatter does not work by searching and replacing. It understands the structure of the program. To do this, it first breaks the text down into its smallest components: names, numbers, brackets, operators. From these building blocks, it then constructs a tree-like representation showing what belongs to what. You can imagine this like a sentence diagram from grammar class, in which main and subordinate clauses hang above one another.
From this structure, the tool rewrites the text completely. A number of rules apply here: How many spaces per level? How long may a line be at most, typically around 80 or 100 characters? Where does it wrap when it no longer fits? Because the formatter knows the meaning, it cannot break anything. It leaves comments and text in quotation marks untouched.
It is important to distinguish this from a linter. A linter also examines code, but reports weaknesses in content: a variable that is never used, a dangerous construct, a likely bug. A formatter never judges the content. It only arranges the appearance. Many teams use both together, because the tasks complement each other.
Formatters in editors, pipelines, and AI tools
Anyone who programs usually encounters formatters when saving. Modern editors call them automatically as soon as a file is saved. Well-known examples are Prettier for web development, Black for the Python language, and gofmt for the Go language. gofmt in particular is considered exemplary because it allows almost no settings and therefore no special preferences either.
In companies, the check is additionally automated. Before new code is merged, a server checks whether the formatting is correct. If it deviates, the change is rejected. Such automated processes are called pipelines, because one check runs after another.
Formatters also play a role in the context of AI. Language models generate program text whose form can vary. A downstream formatter smooths out the result, so that generated and hand-written code can no longer be distinguished. The term also appears with a different meaning: in some AI libraries, a formatter is a component that inserts data into a fixed template for the request to a model. The context usually makes clear which variant is meant.