Code Formatting
Conventions and rules for consistently formatting source code to improve readability and maintainability.
Classification
- ComplexityLow
- Impact areaTechnical
- Decision typeDesign
- Organizational maturityIntermediate
Technical context
Principles & goals
Use cases & scenarios
Compromises
- Automatic reformatting can clutter commit history.
- Inconsistent application leads to merge conflicts.
- Poor communication leads to team resistance.
- Enforce automatic formatting on commit or before merge.
- Make the style guide documented and easily discoverable.
- Keep style changes in separate commits from functional changes.
I/O & resources
- Existing source code
- Style guide or coding conventions
- Formatter tool and EditorConfig
- Consistently formatted code
- Automated CI checks
- Documented style guide
Description
Code formatting defines consistent whitespace, indentation, line breaks and naming conventions to make source code readable and maintainable. It standardises style across teams, reduces friction in code reviews and tooling, and enables automated enforcement. Its scope covers conventions, editor settings and CI-based formatters.
✔Benefits
- Improved readability and faster code reviews.
- Fewer style discussions in pull requests.
- Easier automation and tool integration.
✖Limitations
- Cannot cover all stylistic preferences.
- Initial effort to configure and enforce.
- Overly strict rules may frustrate developers.
Trade-offs
Metrics
- Number of style commits
Measure how many commits contain primarily formatting changes.
- PR discussion duration
Average time spent on style discussions in pull requests.
- CI format failure rate
Share of builds failing due to formatting issues.
Examples & implementations
Python project using PEP8 and Black
Using Black for automatic formatting aligned with PEP8 conventions.
Web frontend with Prettier
Prettier as the standard formatter for JavaScript/TypeScript/HTML in CI and editors.
C++ project with clang-format
Automatic reformatting in the build pipeline, consistent codebase across teams.
Implementation steps
Analyse existing codebase and identify deviations.
Select and configure a suitable formatter and EditorConfig.
Integrate formatting into pre-commit hooks and CI pipeline.
Communicate rules, train team, and apply incrementally to codebase.
⚠️ Technical debt & bottlenecks
Technical debt
- Large backlog tasks to reformat legacy modules.
- Inconsistent history due to late massive reformatting.
- Technical debt from unsupported formatter versions.
Known bottlenecks
Misuse examples
- Enforcing project-specific style that is incompatible with tools.
- Completely reformatting entire repositories in a single commit without review.
- Ignoring functional changes because format checks fail.
Typical traps
- Formatter configurations that produce unexpected changes.
- Non-versioned editor settings lead to inconsistencies.
- Enforcing too early without stakeholder alignment.
Required skills
Architectural drivers
Constraints
- • Existing codebase with many style deviations
- • Limited CI resources for extra checks
- • Language- or platform-specific formatter differences