Skip to content

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:

Google C++ Style Guide

1. General Principles

  • 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.

2. Source Code Structure

  • File Names: Use lower_case_with_underscores for file names.
  • File Extensions: Use .h for header files and .cc for implementation files.
  • Line Length: Limit lines to 80 characters.
  • Indentation: Use 2 spaces per indentation level.

3. Comments

  • Comment Style:
    • Use // for single-line comments.
    • Use /* ... */ for multi-line comments.
  • 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.

4. Naming Conventions

  • Variable Names: Use camelCase for variable names.
  • Class Names: Use CamelCase for class names.
  • Constant Names: Use kCamelCase for constant names.
  • Namespaces: Use lower_case_with_underscores for namespace names.

5. Header Files

  • Include Guards: Use #ifndef, #define, and #endif to prevent multiple inclusions.
  • Header File Order: Include headers in the following order:
    1. Related header.
    2. C system headers.
    3. C++ standard library headers.
    4. Other libraries' headers.
    5. Your project's headers.
  • Forward Declarations: Use forward declarations to reduce dependencies where possible.

6. Classes

  • Structs vs. Classes: Use struct for simple data structures and class for more complex abstractions.
  • Access Control: Prefer private data members and provide access through public methods.
  • Inheritance: Favor composition over inheritance. Use inheritance only when there is a clear "is-a" relationship.

7. Functions

  • Function Names: Use camelCase for function names.
  • Parameter Order: Prefer input parameters before output parameters.
  • Default Arguments: Avoid using default arguments in functions.

8. Scoping

  • Namespace Usage: Avoid using the using directive 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.

9. Memory Management

  • Smart Pointers: Use std::unique_ptr and std::shared_ptr for managing dynamic memory.
  • RAII: Follow the RAII (Resource Acquisition Is Initialization) principle to manage resources.

10. Concurrency

  • 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.

11. Error Handling

  • Exceptions: Avoid using exceptions for normal control flow. Prefer error codes or status objects.
  • Assertions: Use assert to document assumptions and catch programming errors during development.

12. Testing

  • 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.

13. Style Rules

  • 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.
  • 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.

Example Code

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_project

For the most detailed and up-to-date information, refer to the full Google C++ Style Guide.

Clone this wiki locally