Skip to main content

Philosophy

PowerToys follows a pragmatic approach to coding style:
  1. When modifying existing code: Follow the existing style as closely as possible
  2. When writing new code: Follow Modern C++ and C# best practices
  3. When refactoring: Apply modern patterns and reference language guidelines
Consistency within a file or module is more important than strict adherence to global rules. Make the code readable and maintainable.

C++ Style

Core Guidelines

Follow the C++ Core Guidelines for modern C++ development. Key principles:
  • Use RAII (Resource Acquisition Is Initialization)
  • Prefer std::unique_ptr and std::shared_ptr over raw pointers
  • Use const and constexpr where applicable
  • Avoid manual memory management
  • Use standard library algorithms and containers

Code Formatting

PowerToys uses ClangFormat for automatic C++ code formatting.

Using ClangFormat in Visual Studio

Automatic formatting:
  • Format document: Ctrl+K, Ctrl+D
  • Format selection: Ctrl+K, Ctrl+F
Enable automatic formatting:
  1. Tools > Options > Text Editor > C/C++ > Code Style > Formatting
  2. Check “Automatically format on paste”
  3. Check “Automatically format completed statement on ;“

Using ClangFormat from Command Line

Format modified files automatically:
Requirements:
  • Run from “Native Tools Command Prompt for VS”
  • Or ensure clang-format.exe is in %PATH%
  • Script uses Git to find modified files
Manual formatting:

ClangFormat Configuration

Configuration file: src/.clang-format Key formatting rules:

C++ Naming Conventions

C++ Best Practices

Modern C++ Features

Prefer smart pointers:
Use auto for type deduction:
Range-based for loops:

String Handling

Error Handling

Include Order

C# Style

Naming Conventions

C# Best Practices

Nullable Reference Types

PowerToys enables nullable reference types:

Properties vs Fields

LINQ and Modern C#

Async/Await

IDisposable Pattern

C# Code Formatting

Visual Studio’s default C# formatting is generally acceptable. Key settings:
  • Indentation: 4 spaces (no tabs)
  • Brace style: Allman (braces on new line)
  • Line length: Aim for 120 characters or less
Example:

XAML Style

XamlStyler

PowerToys uses XamlStyler for XAML formatting.

Install XamlStyler

Visual Studio Extension

Apply XAML Formatting

In Visual Studio:
  • Format on save (if enabled in XamlStyler options)
  • Manual format: Right-click in XAML editor > Format Document
From command line:

XAML Conventions

Element ordering:
Use resource strings:
Naming:
  • x:Name in PascalCase: MainGrid, SettingsPanel
  • x:Uid for localization: ModuleName_SettingName

Resource Strings and Localization

Resource String Naming

Pattern: <ModuleName>_<Context>_<Element> Examples:

Using Resource Strings

In XAML:
In C#:
Never submit PRs that modify localized strings in languages other than English. Localization is handled by Microsoft’s internal localization team.

Code Comments

When to Comment

Good comments explain WHY:
Poor comments explain WHAT (code already shows this):

XML Documentation (C#)

Doxygen Comments (C++)

Pull Request Guidelines

PR Checklist

Before submitting a PR:
  • Code follows existing style in modified files
  • New code follows modern C++/C# practices
  • Code is formatted (ClangFormat for C++, XamlStyler for XAML)
  • Comments explain complex logic
  • Resource strings used for user-facing text
  • No hardcoded strings in UI
  • Code builds without warnings
  • Tests pass locally

Code Review Expectations

Reviewers will check for:
  • Code readability and maintainability
  • Proper error handling
  • Memory management (C++ leaks, C# disposable patterns)
  • Thread safety for async code
  • Security considerations (input validation, injection attacks)
  • Performance implications
  • Consistency with existing patterns

Before Opening PR

  1. Format your code:
  2. Build successfully:
  3. Run tests:
    • Test Explorer: Run all tests
    • Verify no regressions
  4. Review changes:
    • Remove debug code
    • Remove commented-out code
    • Check for unintended changes

Project-Specific Guidelines

Settings Integration

Settings JSON structure:

Logging Standards

C++ logging:
C# logging:
Logging principles:
  • Log errors and warnings always
  • Log info for important operations
  • Use debug/trace sparingly (disabled by default)
  • Include context in log messages
  • Don’t log sensitive information (passwords, tokens, PII)

Telemetry

C++ telemetry:
C# telemetry:
All telemetry respects user privacy settings. Users can disable telemetry in PowerToys Settings.

Code Quality Tools

Static Analysis

PowerToys uses Visual Studio’s code analysis: For C++:
  • Configuration: CppRuleSet.ruleset
  • Properties: Cpp.Build.props
For C#:
  • Built-in Roslyn analyzers
  • Configuration in .editorconfig

Build Properties

Key build properties:
  • Directory.Build.props - Shared properties for all projects
  • Directory.Build.targets - Shared targets
  • Directory.Packages.props - Central package version management

Additional Resources

External Style Guides

PowerToys Documentation

Next Steps

Building

Build PowerToys from source

Debugging

Debug PowerToys effectively

Testing

Write tests for your code

Creating New Utility

Create a new PowerToys utility