General Principles
The code in this project uses several different coding styles, depending on the age and history of the code. Please attempt to match the style of surrounding code as much as possible.In new components, prefer the patterns described in the C++ Core Guidelines and the modern C++/WinRT language projections.
C++ Core Guidelines
For new code, follow the C++ Core Guidelines. Key principles include:Resource Management
RAII and Smart Pointers
RAII and Smart Pointers
Use Resource Acquisition Is Initialization (RAII) for resource management:This ensures resources are properly cleaned up and prevents memory leaks.
Type Safety
- Use strong types instead of primitive types where appropriate
- Prefer
constexprfor compile-time constants - Use
constfor variables that don’t change - Avoid C-style casts; use
static_cast,dynamic_cast, etc.
Modern C++ Features
Use Modern Language Features
Use Modern Language Features
Leverage modern C++ features for clearer, safer code:
C++/WinRT Guidelines
When working with Windows Runtime APIs, follow the C++/WinRT best practices:Async Operations
Event Handlers
Proper Event Handler Management
Proper Event Handler Management
String Handling
Code Organization
File Structure
- Keep header files (.h) focused on declarations
- Implement complex logic in source files (.cpp)
- Use forward declarations when possible to reduce dependencies
- Group related functionality into namespaces
Naming Conventions
Naming Standards
Naming Standards
Match the existing code style in the file you’re modifying. For new code:
Code Quality
Keep Changes Small
- Focus on one logical change per commit
- Refactor separately from feature changes when possible
- Break large features into smaller, reviewable chunks
Error Handling
Comments
When and How to Comment
When and How to Comment
Write code that is self-documenting, but add comments when:
- Explaining why something is done (not what)
- Documenting complex algorithms
- Noting non-obvious behavior or edge cases
- Referencing external requirements or bugs
Testing Standards
Your code should be structured for testability:Unit Test Structure
Test Coverage
Include corresponding tests whenever possible. Code should be structured so that it can be unit tested independently of the UI.
- Write unit tests for business logic
- Add integration tests for component interactions
- Use manual tests for UI-heavy scenarios
- Test edge cases and error conditions
Performance Considerations
- Avoid unnecessary allocations in hot paths
- Use move semantics for expensive objects
- Prefer passing by const reference for read-only parameters
- Profile before optimizing