Motion Master
Loading...
Searching...
No Matches
Google C++ Style Guide – Key Bullet Points

✅ Naming Conventions

  • Types (classes, structs, typedefs): CamelCase (e.g., MyClass)
  • Functions & Variables: camelCase (e.g., parseMessage, bufferSize)
  • Constants: kCamelCase (e.g., kMaxSize)
  • Namespace names: lowercase_with_underscores
  • Macros & Enums: ALL_CAPS_WITH_UNDERSCORES
  • File names: lowercase_with_underscores.h/.cc

🔒 Private Member Variables

  • Named using camelCase
  • Suffixed with an underscore (_)

✅ Formatting

  • Indentation: 2 spaces, no tabs
  • Line length: 80 characters preferred, up to 100 allowed
  • Braces:
    • Opening brace on the same line (K&R style)
    • Always use braces, even for single-line blocks

✅ Comments

  • Use // for inline comments.
  • Use /// or /** */ for documentation comments.
  • The preference for Doxygen comments is as follows:
    • Use /// for one-line Doxygen comments.
    • Use /** */ for multi-line Doxygen comments.
  • Keep comments up to date and explain **"why"**, not just **"what"**.

✅ Function Design

  • Prefer short functions that do one thing.
  • Use default values for parameters sparingly.
  • Avoid output parameters; return values instead when possible.

✅ Class Design

  • Make data members private.
  • Use = delete for disallowed constructors/assignment.
  • Use explicit for single-argument constructors.
  • Prefer initializer lists in constructors.

✅ Use of Pointers and References

  • Use const T& for read-only inputs.
  • Use raw pointers only for non-owning references.
  • Use std::unique_ptr or std::shared_ptr for ownership.

✅ Memory Management

  • Prefer smart pointers over raw new/delete.
  • Avoid manual memory management unless absolutely necessary.

✅ Header Files

  • Use include guards or #pragma once.
  • Keep headers self-contained and minimal.
  • Prefer forward declarations when possible.

✅ Miscellaneous

  • Avoid using namespace std;.
  • Prefer nullptr over NULL or 0.
  • Avoid using directives in headers.
  • Prefer range-based for loops and auto where appropriate.

🚫 What to Avoid

  • Avoid using namespace std;
    It can introduce name conflicts and make the code harder to maintain.
  • Avoid Magic Numbers
    Use named constants or constexpr for values used throughout the code.
  • Avoid Direct new/delete Usage
    Prefer smart pointers (std::unique_ptr, std::shared_ptr) for automatic memory management.
  • Avoid NULL or 0 for Pointers
    Always use nullptr to initialize or check pointers.
  • Avoid Implicit Type Conversions
    Use explicit for single-argument constructors to prevent accidental conversions.
  • Avoid Non-Const Global or Static Variables
    Minimize the use of global or static variables as they can introduce bugs and make state management harder.
  • Avoid Using Raw Loops When Possible
    Use range-based for loops or standard algorithms for better readability and maintainability.
  • Avoid Non-Descriptive Names
    Always use meaningful and descriptive names for variables, functions, and classes.
  • Avoid Large Functions or Classes
    Keep functions and classes small and focused on a single responsibility.
  • Avoid Overloading Operators Without Good Reason
    Operator overloading can reduce code clarity if misused.
  • Avoid Mixing Style Between Header and Source Files
    Declarations should go in header files, and definitions should go in source files.