-
Notifications
You must be signed in to change notification settings - Fork 0
C Coding Standards
Adam Szablya edited this page Jun 2, 2024
·
1 revision
Metrolla has adopted the google style guide:
Google's C++ coding standards are documented in the "Google C++ Style Guide". This guide outlines the best practices and rules that Google engineers follow when writing C++ code. Here is a summary of the key points:
- Clarity: Write clear and understandable code.
- Consistency: Follow the established coding conventions to maintain consistency across the codebase.
- Simplicity: Prefer simple solutions and avoid overly complex or clever code.
-
File Names: Use
lower_case_with_underscoresfor file names. -
File Extensions: Use
.hfor header files and.ccfor implementation files. - Line Length: Limit lines to 80 characters.
- Indentation: Use 2 spaces per indentation level.
-
Comment Style:
- Use
//for single-line comments. - Use
/* ... */for multi-line comments.
- Use
-
Documentation Comments: Use Doxygen-style comments (
///or/** ... */) for documenting APIs. - Function Comments: Place comments before function definitions to describe what the function does and its parameters.
-
Variable Names: Use
camelCasefor variable names. -
Class Names: Use
CamelCasefor class names. -
Constant Names: Use
kCamelCasefor constant names. -
Namespaces: Use
lower_case_with_underscoresfor namespace names.
-
Include Guards: Use
#ifndef,#define, and#endifto prevent multiple inclusions. -
Header File Order: Include headers in the following order:
- Related header.
- C system headers.
- C++ standard library headers.
- Other libraries' headers.
- Your project's headers.
- Forward Declarations: Use forward declarations to reduce dependencies where possible.
-
Structs vs. Classes: Use
structfor simple data structures andclassfor more complex abstractions. -
Access Control: Prefer
privatedata members and provide access through public methods. - Inheritance: Favor composition over inheritance. Use inheritance only when there is a clear "is-a" relationship.
-
Function Names: Use
camelCasefor function names. - Parameter Order: Prefer input parameters before output parameters.
- Default Arguments: Avoid using default arguments in functions.
-
Namespace Usage: Avoid using the
usingdirective in header files. Use explicit namespace prefixes. - Local Variables: Minimize the scope of local variables. Declare variables as close to their first use as possible.
-
Smart Pointers: Use
std::unique_ptrandstd::shared_ptrfor managing dynamic memory. - RAII: Follow the RAII (Resource Acquisition Is Initialization) principle to manage resources.
- Thread Safety: Write thread-safe code when necessary. Use synchronization primitives like mutexes and condition variables.
- Avoid Data Races: Ensure that shared data is properly synchronized to avoid data races.
- Exceptions: Avoid using exceptions for normal control flow. Prefer error codes or status objects.
-
Assertions: Use
assertto document assumptions and catch programming errors during development.
- Unit Tests: Write unit tests for all new code using the Google Test framework.
- Test Coverage: Aim for high test coverage, but prioritize meaningful tests over achieving 100% coverage.
-
Braces:
- Opening braces are placed on the same line as the control statement (
if,while, etc.). - Always use braces for control statements, even if the body is a single line.
- Opening braces are placed on the same line as the control statement (
-
Whitespace:
- Use a single blank line to separate logical sections of code.
- Avoid trailing whitespace at the end of lines.
-
Parentheses:
- Use parentheses to make the order of operations explicit.
Here is an example that illustrates some of these standards:
// my_class.h
#ifndef MY_PROJECT_MY_CLASS_H_
#define MY_PROJECT_MY_CLASS_H_
#include <string>
#include <vector>
namespace my_project {
class MyClass {
public:
MyClass();
explicit MyClass(const std::string& name);
void DoSomething();
private:
std::string name_;
std::vector<int> data_;
};
} // namespace my_project
#endif // MY_PROJECT_MY_CLASS_H_// my_class.cc
#include "my_project/my_class.h"
#include <iostream>
namespace my_project {
MyClass::MyClass() : name_("default") {}
MyClass::MyClass(const std::string& name) : name_(name) {}
void MyClass::DoSomething() {
std::cout << "Name: " << name_ << std::endl;
}
} // namespace my_projectFor the most detailed and up-to-date information, refer to the full Google C++ Style Guide.