Google Style Guide

https://google.github.io/styleguide/cppguide.html

I really like this style guide. Very standard in industry.

Pascal:

HelloWorld // <--- pascal case

Camel:

helloWorld // <--- camel case

Include What You Use

If a source or header file refers to a symbol defined elsewhere, the file should directly include a header file which properly intends to provide a declaration or definition of that symbol. It should not include header files for any other reason.

Do not rely on transitive inclusions. This allows people to remove no-longer-needed #include statements from their headers without breaking clients. This also applies to related headers - foo.cc should include bar.h if it uses a symbol from it even if foo.h includes bar.h.

Naming

Filenames

Filenames should be all lowercase and can include underscores (_) or dashes (-).

  • prefer ā€_ā€

Examples of acceptable file names:

  • my_useful_class.cc
  • my-useful-class.cc
  • myusefulclass.cc
  • myusefulclass_test.cc // _unittest and _regtest are deprecated.

What about folder names?

Seems like they donā€™t have a convention for folder names. I think using the same convention as the file names makes sense.

Personal Preference

Since the classes themselves donā€™t have underscores in between, I also donā€™t have underscores in between for the filenames.

Type Names (Classes, Structs)

Type names follow PascalCase. Ex: MyExcitingClass,Ā MyExcitingEnum.

// classes and structs
class UrlTable { ...
class UrlTableTester { ...
struct UrlTableProperties { ...
 
// typedefs
typedef hash_map<UrlTableProperties *, std::string> PropertiesMap;
 
// using aliases
using PropertiesMap = hash_map<UrlTableProperties *, std::string>;
 
// enums
enum class UrlTableError { ...

Variable Names

Variables (including function parameters) and data members areĀ snake_case.

  • data members of classes (but not structs) additionally have trailing underscores _

Non-Class Member Variables

std::string table_name;  // OK - snake_case.
std::string tableName;   // Bad - mixed case.

Class Data Members

Data members of classes (but not structs) additionally have trailing underscores _.

class TableInfo {
  ...
 private:
  std::string table_name_;  // OK - underscore at end.
  static Pool<TableInfo>* pool_;  // OK.
};

Struct Data Members

Data members of structs, both static and non-static, are named like ordinary nonmember variables. They do not have the trailing underscores that data members in classes have.

struct UrlTableProperties {
  std::string name;
  int num_entries;
  static Pool<UrlTableProperties>* pool;

Constant Names

Variables declaredĀ constexprĀ orĀ const, and whose value is fixed for the duration of the program, are named with a leading ā€œkā€ followed by mixed case. Underscores can be used as separators in the rare cases where capitalization cannot be used for separation. For example:

const int kDaysInAWeek = 7;
const int kAndroid8_0_0 = 24;  // Android 8.0.0

Method / Function Names

Functions are named with PascalCase.

  • accessors and mutators may be named like variables.

Regular functions have mixed case;

AddTableEntry()
DeleteUrl()
OpenFileOrDie()

Method names

class MyClass {
 public:
  void Analyze(const std::string &text);
  void Analyze(const char *text, size_t textlen);
};

Enum Names

Enumerators (for both scoped and unscoped enums) should be named likeĀ constants, not likeĀ macros. That is, useĀ kEnumNameĀ notĀ ENUM_NAME.

enum class UrlTableError {
  kOk = 0,
  kOutOfMemory,
  kMalformedInput,
};