Skip to main content
KiCad follows specific code style guidelines to maintain consistency across the codebase. All contributions should adhere to these standards.

Overview

KiCad uses automated formatting tools to enforce many (but not all) style requirements:
  • clang-format - Primary formatting tool (minimum version 10)
  • uncrustify - Legacy formatting configuration (for reference)
The official style guide is available at: https://dev-docs.kicad.org/en/rules-guidelines/code-style/

Automated Formatting

Using clang-format

The clang-format configuration is stored in _clang-format at the repository root.

Format a Single File

Check Formatting (Dry Run)

Format All Modified Files

CI Format Checks

When you create a merge request, the CI pipeline includes a formatting check. This automatic check is not always 100% correct, so keep in mind:
  1. Exceptions exist: Some formatting guidelines have exceptions or only apply to certain situations. clang-format doesn’t know about these nuances.
  2. Sweeping changes: clang-format may suggest changes to areas of a file near your code, even if you didn’t modify that code. Rule 7 of the style guide: When there is flexibility or doubt, follow the existing formatting of the file you are editing.
  3. Limitations: clang-format doesn’t handle:
    • Column alignment formatting (Rule 4.1.2)
    • Preferred lambda format (Rule 4.10)
    • Alphabetizing #include directives (don’t do this for existing files unless making sweeping changes)

Code Style Configuration

clang-format Settings

Key settings from _clang-format:

Basic Style Rules

Indentation and Spacing

  • Indentation: 4 spaces (no tabs)
  • Line length: Maximum 120 characters
  • Spaces in parentheses: Yes - foo( a, b ) not foo(a, b)
  • No space before parentheses: function() not function ()

Naming Conventions

Classes and Types:
Functions:
Variables:
Parameters:

Bracing Style (Allman)

Pointer and Reference Alignment

Header Guards

Use #ifndef include guards:

Include Order

While clang-format suggests alphabetizing includes, do not alphabetize includes in existing files unless you are making sweeping changes to the include list. Typical include order:

C++ Specific Guidelines

Modern C++ (C++20)

KiCad uses C++20. Prefer modern C++ features:

Lambda Functions

clang-format doesn’t support KiCad’s preferred lambda format. Format lambdas manually:

Templates

Constructor Initializer Lists

Comments and Documentation

File Headers

All source files should include a license header:

Doxygen Comments

Use Doxygen-style comments for classes, functions, and important members:

Inline Comments

wxWidgets Specific

wxString Usage

Event Handlers

Best Practices

Column Alignment (Rule 4.1.2)

When appropriate, align similar code for readability:
Note: clang-format doesn’t support this, so format manually.

Error Handling

Boolean Comparisons

Magic Numbers

Avoid magic numbers, use named constants:

User Interface Guidelines

For UI-related code, also follow the User Interface Guidelines:
  • Consistent dialog layouts
  • Proper use of wxWidgets sizers
  • Keyboard shortcuts and mnemonics
  • Accessibility considerations

Pre-Commit Checks

Before committing your changes:
  1. Format your code:
  2. Build without warnings:
  3. Run tests:
  4. Check for style violations:

When to Deviate

Rule 7: When there is flexibility or doubt, follow the existing formatting of the file you are editing rather than rigidly following clang-format. If you’re modifying an existing file:
  • Match the existing style in that file
  • Don’t reformat code you didn’t change
  • Focus on making your changes consistent with the surroundings

Additional Resources

Next Steps

Now that you understand the code style guidelines:
  1. Practice formatting your code with clang-format
  2. Review existing code to see style in practice
  3. Start contributing with a merge request