A code formatter for every language you use
Consistent formatting makes code easier to read, review and maintain. It also helps when you receive code in a bad state: minified bundles, one-line JSON, SQL from a log file or a snippet whose indentation was mangled by an email client. This collection brings a code formatter for each common language together in one place, most of them based on the same formatters that professional editors use.
Choosing the right formatter
- Data formats – JSON formatter, XML formatter, YAML formatter and TOML formatter also validate the document while formatting.
- Web front end – HTML formatter, CSS formatter, JavaScript formatter and TypeScript formatter, plus Vue, Angular, React and GraphQL.
- Back end – PHP formatter, Python formatter, Java formatter, C#, C, C++, Ruby, Perl and Lua beautifiers.
- Databases – the SQL formatter lays out clauses, joins and subqueries.
- Docs – the Markdown formatter normalizes lists, headings and tables.
If you want to present code rather than change it, the code syntax highlighter produces colored HTML for blogs and emails.
How to get the best results
- Pick the formatter for the exact language or dialect; a JSX file formats better with the React formatter than with the plain JavaScript one.
- Choose an indentation that matches your project (2 or 4 spaces, or tabs).
- If the formatter reports a syntax error, fix it first; formatters need parseable code.
- Compare before and after with the diff checker when you format code that others will review.
Formatting in teams
Online formatters are ideal for one-off jobs. For projects, add the same tool to your editor and continuous integration (Prettier for web languages, Black for Python, gofmt for Go and so on), so every commit is formatted automatically and style debates disappear from code reviews.
When a formatter saves the day
Formatting is the first step of almost every debugging session with unfamiliar code. A minified stack trace becomes navigable once the bundle is formatted, a failing configuration file often reveals a misplaced bracket as soon as it is indented, and a long SQL query that "sometimes returns duplicates" usually shows its missing join condition when each clause sits on its own line.
Good to know
- Formatting changes whitespace and layout, not behavior. Python is the exception to be careful with, because indentation is part of its syntax.
- Minifiers do the opposite of formatters; you can find them in the minifiers category.
- Code is formatted in your browser, so proprietary code is not uploaded.