Philosophy
PowerToys follows a pragmatic approach to coding style:- When modifying existing code: Follow the existing style as closely as possible
- When writing new code: Follow Modern C++ and C# best practices
- When refactoring: Apply modern patterns and reference language guidelines
C++ Style
Core Guidelines
Follow the C++ Core Guidelines for modern C++ development. Key principles:- Use RAII (Resource Acquisition Is Initialization)
- Prefer
std::unique_ptrandstd::shared_ptrover raw pointers - Use
constandconstexprwhere 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
- Tools > Options > Text Editor > C/C++ > Code Style > Formatting
- Check “Automatically format on paste”
- Check “Automatically format completed statement on ;“
Using ClangFormat from Command Line
Format modified files automatically:- Run from “Native Tools Command Prompt for VS”
- Or ensure
clang-format.exeis in%PATH% - Script uses Git to find modified files
ClangFormat Configuration
Configuration file:src/.clang-format
Key formatting rules:
C++ Naming Conventions
| Element | Convention | Example |
|---|---|---|
| Classes | PascalCase | ColorPicker, WindowManager |
| Functions | camelCase or snake_case | getWindowRect(), init_logger() |
| Variables | camelCase or snake_case | currentWindow, window_rect |
| Constants | UPPER_CASE | MAX_BUFFER_SIZE |
| Namespaces | lowercase | fancyzones, powerrenameext |
| Member variables | m_ prefix (optional) | m_windowHandle, m_settings |
C++ Best Practices
Modern C++ Features
Prefer smart pointers:String Handling
Error Handling
Include Order
C# Style
Naming Conventions
| Element | Convention | Example |
|---|---|---|
| Classes | PascalCase | ColorPicker, SettingsViewModel |
| Interfaces | IPascalCase | IModule, ISettings |
| Methods | PascalCase | GetWindowRect(), ProcessInput() |
| Properties | PascalCase | WindowTitle, IsEnabled |
| Fields (private) | _camelCase | _windowHandle, _settings |
| Parameters | camelCase | windowHandle, inputText |
| Local variables | camelCase | currentWindow, result |
| Constants | PascalCase | MaxBufferSize, DefaultTimeout |
| Enums | PascalCase | WindowState.Maximized |
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
XAML Style
XamlStyler
PowerToys uses XamlStyler for XAML formatting.Install XamlStyler
Visual Studio ExtensionApply XAML Formatting
In Visual Studio:- Format on save (if enabled in XamlStyler options)
- Manual format: Right-click in XAML editor > Format Document
XAML Conventions
Element ordering:x:Namein PascalCase:MainGrid,SettingsPanelx:Uidfor localization:ModuleName_SettingName
Resource Strings and Localization
Resource String Naming
Pattern:<ModuleName>_<Context>_<Element>
Examples:
Using Resource Strings
In XAML:Code Comments
When to Comment
Good comments explain WHY: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
-
Format your code:
-
Build successfully:
-
Run tests:
- Test Explorer: Run all tests
- Verify no regressions
-
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:- 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: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
- Built-in Roslyn analyzers
- Configuration in
.editorconfig
Build Properties
Key build properties:Directory.Build.props- Shared properties for all projectsDirectory.Build.targets- Shared targetsDirectory.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