- Write clean, readable, and maintainable code
- Follow the DRY (Don't Repeat Yourself) principle
- Keep functions small and focused
- Use meaningful names for variables, functions, and classes
- Use camelCase for variable names
- Make names descriptive and meaningful
- Boolean variables should ask a question:
isReady
,hasData
int maxValue;
std::string userName;
bool isValid;
- Use camelCase for function names
- Verbs for functions that perform actions
- Nouns for functions that return values
void calculateTotal();
std::string getUserName();
bool isValidInput();
- Use PascalCase for class names
- Nouns or noun phrases
class Calculator;
class DataProcessor;
- Use UPPER_SNAKE_CASE for constants
const int MAX_BUFFER_SIZE = 1024;
- Use header guards
- Group includes logically
- Forward declare when possible
#ifndef MODULE_NAME_H
#define MODULE_NAME_H
// Standard library includes
#include <string>
#include <vector>
// Project includes
#include "project_header.h"
class ClassName {
// ...
};
#endif // MODULE_NAME_H
- Order members: public, protected, private
- Group similar functions together
- Document class purpose and member functions
- Use 4 spaces for indentation
- No tabs
- Maximum 80 characters per line
- Break long lines logically
- Opening brace on the same line
- Closing brace on its own line
if (condition) {
// code
}
- One space after keywords
- One space around operators
- No space after function names
if (x == y) {
foo(x);
}
- Write comments that explain why, not what
- Keep comments up to date
- Use complete sentences
- Document parameters and return values
- Explain side effects
- Use Doxygen style
/**
* @brief Calculate the sum of two numbers
* @param a First operand
* @param b Second operand
* @return Sum of a and b
*/
double add(double a, double b);
- Use exceptions for error handling
- Document exceptions in function comments
- Always catch exceptions at appropriate levels
try {
// code that might throw
} catch (const std::exception& e) {
// handle error
}
- Use smart pointers instead of raw pointers
- Follow RAII principles
- Avoid memory leaks
- Use auto when it improves readability
- Use range-based for loops when possible
- Use constexpr for compile-time constants
- Pass large objects by const reference
- Use move semantics when appropriate
- Consider cache coherency
- Write unit tests for all public functions
- Test edge cases
- Keep tests readable and maintainable
- Document public interfaces
- Keep documentation up to date
- Use Doxygen for API documentation
- Write meaningful commit messages
- Keep commits focused and atomic
- Follow conventional commits format
This style guide is a living document and may be updated as needed to reflect best practices and team preferences.