guide-category-code

Code tools: formatting, minifying and validating

Formatting makes code readable for people; minifying makes it smaller for machines. Knowing which one you need, and what each changes, keeps you out of trouble.

Format for reading, minify for shipping

Formatters add consistent whitespace so structure is visible: indentation shows nesting, line breaks separate items. Minifiers remove whitespace and comments that the computer does not need. Neither should change meaning. The tools in this category are deliberately conservative: when a transformation could change behaviour, it is either skipped or offered as an option that is off by default.

  • JSON formatter and validator: parses with the browser's own JSON parser, so if it formats, the JSON is valid.
  • HTML minifier: collapses whitespace, removes comments you do not mark for keeping, and copies pre, textarea, script and style contents exactly.
  • CSS formatter and minifier: respects strings and comments and keeps the spaces calc() requires.
  • SQL formatter: lays out clauses and conditions and recases keywords without touching strings or identifiers.

Formatting is not validation

Only the JSON tool validates in the strict sense, because JSON's grammar is small and the browser includes a complete parser. The CSS and SQL tools understand enough structure to lay code out and to catch problems such as unbalanced braces or unclosed strings, but they do not know every property or every database's grammar. Always run formatted SQL against a test database and check formatted CSS in the browser.

How much does minifying save?

Less than you might think when your server already compresses responses with gzip or Brotli, because repeated whitespace compresses very well. Minification still helps with large, deeply indented files and with assets that are inlined into HTML. Measure: compare compressed sizes before and after rather than raw byte counts.

Keep a readable source

Treat minified output as a build artefact. Keep the formatted original in version control, minify a copy as part of deployment, and never hand-edit minified files. If you need comments to survive minification, such as licence headers, mark them with /*! in CSS or <!--! in HTML.

Further reading

Start with how to format and validate JSON, which covers the syntax rules behind the most common errors.