Clarity formatter

The Clarity formatter is a tool designed to automatically format your Clarity smart contract code according to standardized style rules. Using consistent formatting improves code readability and maintainability, making it easier for developers to understand, collaborate on, and learn from existing Clarity projects.


Formatting philosophy

The formatter applies an opinionated standard designed to make Clarity code more readable:

  • Line length - Lines wrap at 80 characters by default
  • Indentation - Uses 2 spaces for consistency
  • Structure - Enforces consistent patterns for functions, let bindings, and control flow

These defaults are configurable to match your preferences.

Integration points

The formatter is available in two primary development tools:

  1. 1Clarity VS Code Extension - Format directly within your editor
  2. 2Clarinet CLI - Format via command line, including entire projects

Comparison table

AspectManual FormattingClarity Formatter
ConsistencyVaries by developerUniform across codebase
SpeedTime-consumingInstant
Error-proneYesNo
Team coordinationRequires style guideAutomatic enforcement

Best practices

  • Format on save - Enable automatic formatting in VS Code
  • Pre-commit hooks - Ensure all code is formatted before commits
  • Team adoption - Use consistent settings across your team

Common misconceptions

Common misconception

The formatter changes code behavior or logic.

Reality: The formatter only changes whitespace and layout. It never alters the functionality of your code.

Further reading

Formatting rules in detail

The formatter handles many Clarity constructs with specific rules:

Usage examples

VS Code integration

// settings.json
"[clarity]": {
"editor.formatOnSave": true,
},

CLI usage

Terminal
$
clarinet format --in-place

Format with custom settings:

Terminal
$
clarinet format -i 4 -l 120 --in-place

Ignoring sections

Prevent formatting of specific code blocks:

;; @format-ignore
(define-constant something (list
1 2 3 ;; Preserves custom spacing
4 5 ))