Which Code Formatter Do You Need? JS, CSS, SQL, Python
Code pasted from a minified bundle, a production log or a colleague's message usually arrives with mangled indentation or no line breaks at all. Formatting fixes readability instantly, but the rules differ by language - and getting them wrong can change what the code does.
- Formatting changes only whitespace - indentation, line breaks, spaces - and never code semantics, so formatted code and formatted-then-minified code behave identically
- Beautifying and minifying are opposite operations: the first adds whitespace for reading, the second removes it to reduce transfer size
- JavaScript's automatic semicolon insertion gives line breaks semantic meaning, so minification that deletes newlines can change program behaviour
- Python uses indentation to define blocks, so a formatter must preserve the indentation hierarchy rather than re-indent freely as it would for C-style languages
- SQL keyword case never affects execution, but identifier quoting is dialect-specific: MySQL uses backticks, Oracle and PostgreSQL use double quotes
- Formatting never fixes syntax errors - code that does not parse will fail immediately with an error
#What formatting changes, and what it does not
Formatting adjusts whitespace only - indentation, line breaks, spaces - and leaves semantics untouched. That guarantee is what makes formatters safe: the result behaves exactly like the input, only more readable.
Two opposite directions are worth keeping straight:
| Direction | Also called | What it does | Where you use it |
|---|---|---|---|
| Beautify | pretty-print | Adds indentation and line breaks | Reading, debugging, code review |
| Minify | compress | Removes all dispensable whitespace | Production deployment, smaller transfers |
Minification permanently reduces readability. A minified bundle is fine in production, but always keep the original - debugging minified code needs source maps or re-formatting, and reformatting cannot restore renamed variables (production minifiers rename them). The minifier here only removes whitespace and does not rename anything.
#JavaScript: line breaks carry meaning
JavaScript's automatic semicolon insertion (ASI) gives line breaks semantic weight, a risk unique to JS. These two functions do completely different things:
class="tk-c">// This one returns undefined
function f() {
return
{ value: 1 }
}class="tk-c">// This one returns the object { value: 1 }
function f() {
return {
value: 1
}
}In the first version the newline after return triggers an inserted semicolon, and the object becomes a discarded statement. This is most dangerous during minification, where removing newlines can join return to the expression that follows and change behaviour.
The safe practice: before minifying, confirm there are no bare line breaks after return, break, continue or throw, and avoid starting a line with (, [, + or - when it continues a previous expression.
Template literals and regex literals are the two constructs most often misparsed by formatters. The JavaScript Formatter handles both explicitly - if the contents of a template literal ever change, check for nested backticks.
#Python: indentation is syntax
Python uses indentation to define blocks, so a formatter must preserve the original hierarchy. This is fundamentally different from C-style languages, where braces determine structure and indentation is cosmetic. In Python, wrong indentation means either an IndentationError or silently different logic.
| Language | Structure defined by | Formatter freedom |
|---|---|---|
| JavaScript / Java / C | {} braces | High; re-indent freely |
| Python | Indentation level | Low; can normalize the space count, never the level |
| CSS | Braces plus semicolons | High |
| SQL | Keywords and semicolons | High, subject to dialect |
When you run Python Formatter, it normalizes inconsistent indentation to 4 spaces - it does not re-infer the hierarchy. If the pasted code had incorrect indentation to begin with, the output preserves that error, and a human needs to confirm the structure.
#CSS: aligning nested media queries
The main value of CSS formatting is aligning nested media queries and selector levels. Minified CSS typically looks like this:
.a{color:red;padding:8px}@media(max-width:640px){.a{padding:4px}}Formatted, the hierarchy becomes visible, and you can see at a glance which rules apply only at a given screen width:
.a {
color: red;
padding: 8px;
}
@media (max-width: 640px) {
.a {
padding: 4px;
}
}Two CSS rules deserve attention when formatting. Spaces inside shorthand values such as margin: 0 auto must not be collapsed, or the declaration becomes invalid. And the string inside a content property must be preserved exactly, even when it contains { or }.
#SQL: the dialect decides the quotes
SQL keyword case never affects execution, but identifier quoting is dialect-specific, which is where SQL formatting goes wrong most often:
| Database | Identifier quoting | Note |
|---|---|---|
| MySQL | Backticks ` col ` | Double quotes work only when ANSI_QUOTES is enabled |
| Oracle | Double quotes "COL" | Double quotes create a case-sensitive identifier |
| PostgreSQL | Double quotes "col" | Unquoted identifiers fold to lowercase |
| SQL Server | Brackets [col] | Double quotes also accepted |
A formatter that normalizes all quoting to one style produces statements that fail on another database. So pick the target dialect in SQL Formatter first, then verify the quotes survived intact.
Note also that string literals are never reformatted, including any part that looks like a keyword or a comment. That is correct behaviour, not a bug.
#Which tool for which job
| What you have | Tool | Watch out for |
|---|---|---|
| JS pulled from a live bundle | JavaScript Formatter | Template literals, regex literals |
| A UI framework stylesheet | CSS Formatter | Media query nesting, shorthand values |
| A statement copied from a slow query log | SQL Formatter | Target database dialect |
| A script a colleague sent you | Python Formatter | Verify the indentation hierarchy by hand |
| Config files or XML from an API | XML Formatter | Attribute quotes, namespace prefixes |
| A page template to shrink | HTML Formatter / Minifier | Whitespace inside <pre> is meaningful |
That last row deserves its own note: whitespace inside HTML <pre> and <textarea> elements is significant, so minifying those regions changes what the page renders. Check your template for these tags before minifying.
#Frequently asked questions
#Can formatting break working code?
Ordinary beautification cannot, because it only touches whitespace. Two exceptions exist. First, indentation-sensitive languages like Python: if the input indentation was already wrong, the formatter preserves the error. Second, minification: deleting line breaks can change semantics at JS return statements. Beautifying is generally safe; minifying deserves care.
#How much smaller does minification make a file?
It depends on the original indentation and blank-line density. A typical range is 20-50% smaller, with indentation spaces accounting for most of it - code that uses 4-space indentation and generous blank lines benefits most. Comments are usually stripped too, so if a licence header must survive, verify the tool's behaviour first.
#Should I review the output by hand?
A quick scan is worthwhile, focusing on three constructs: string literals, regular expressions and template literals. These can contain characters that resemble code syntax, which is where mistakes happen. Also verify SQL quoting and the Python indentation level.
#Why did formatting fail with an error?
A formatter must parse the code first, and parsing fails when the code is not valid. That usually means a real syntax error: unbalanced brackets, an unterminated string, a missing comma. The reported position typically points at or near the problem - fix it manually and retry.
#Does formatting keep comments?
Beautifying keeps them. Minifying usually removes them, since comments do not execute and consume bytes. If a licence header must remain for legal reasons, add it back after minifying, or use a build tool that preserves designated comments.
#Is an online tool enough to standardise a team's style?
No. Online tools are for one-off fragments. For a team, use an editor plugin or a formatter in the build pipeline (Prettier, Black, gofmt) so it runs on save - that way consistency is enforced by tooling rather than by personal habit. Online formatters are best for the "quickly understand unfamiliar code" case.
Tools used in this guide
Beautify & minify JS, supports template strings and regex
Open tool →CSS FormatterBeautify & minify CSS with media query nesting
Open tool →SQL FormatterFormat SQL for MySQL/Oracle dialects
Open tool →Python FormatterPEP8-style Python code beautifier and minifier
Open tool →XML FormatterBeautify & minify XML with attribute normalization
Open tool →HTML FormatterBeautify & minify HTML with smart tag indentation
Open tool →