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:
- 1Clarity VS Code Extension - Format directly within your editor
- 2Clarinet CLI - Format via command line, including entire projects
Comparison table
Aspect | Manual Formatting | Clarity Formatter |
---|---|---|
Consistency | Varies by developer | Uniform across codebase |
Speed | Time-consuming | Instant |
Error-prone | Yes | No |
Team coordination | Requires style guide | Automatic 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
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
$clarinet format --in-place
Format with custom settings:
$clarinet format -i 4 -l 120 --in-place
Ignoring sections
Prevent formatting of specific code blocks:
;; @format-ignore(define-constant something (list1 2 3 ;; Preserves custom spacing4 5 ))