- This rule is not applied to the name that shared with other systems.
- For e.g., FIFO/HW/Reg Module names
- Don't abuse abbreviation for variable/function/class name
- DON'T: the head words
- For e.g. barycentricPoint
- DON'T: short words (In general, <= 8 characters)
- For e.g.
Mode
->md
,Module
->Mdu
,Core
->Cor
,Port
->Prt
, etc.
- For e.g.
- DON'T: ambigous abbreviations
- DO: Abbr. CAN be used where there is clear context WITHOUT misunderstanding
- e.g.
Context ctx;
std::function<void()> func;
- Common abbreviations: https://github.com/kisvegabor/abbreviations-in-code
- e.g.
- DON'T: the head words
- PLEASE FIX TYPOs!
- Macro
- All capital, underscore as separator
HAS_DEFAULT_CONSTRUCTORS(MyClass);
- All capital, underscore as separator
- Type name
- starts with capital letter, and capital letter for each new word.
ThisIsAType
- Variable name
- Local varaible as
localVariable
- Class member variable as
mFieldName
- Struct member variable as
FieldName
- Static member variable as
sFieldName
const static
orconst local variable as
kValue`
- Local varaible as
- Function name
- Regular functions have mixed case; accessors and mutators may be named like variables.
Ordinarily, functions should start with a capital letter and have a capital letter for each new word. - Prefer verb-object phrase
- For e.g.
AddTriangle
- For e.g.
- Regular functions have mixed case; accessors and mutators may be named like variables.
- Enumeration
enum
- The name of
enum
is following type naming rule. - Field names in
enum
like the macros and should have a prefix to identify theenum
. - e.g.
enum PrimitiveTopology{ PRIMTOPO_TRIANGLE_LIST = 0; };
- The name of
enum class
- The name of
enum class
and fields follows the naming conversion of structure.
- The name of
struct MyStruct {
int Value;
};
template <typename T>
class MyClass {
public:
void DoSomething() {
int localVariable;
// ...
}
private:
T mSomeField;
static int sField;
static int const kMaxValue;
};
// Rule: `cond` should be a bool
if (cond) { return false; }
if (cond) {
return false;
}
if (cond)
{
return false;
}
if (cond)
{
// ... do something ...
}
// Rule: Braces are requied even only one statement in the branch.
if (cond)
{
return localVariable;
}
// Rule: Convert to bool explicitly, except pointer.
if (iValue != 0)
{
// ... do something ...
}
// Rule: Pass-thru is only available for shared logic
switch (v) {
case A:
// ... do something ...
break; // don't pass through
// Pass-thru is only allowed for following case
case B:
case C:
case D:
// ... do something for B, C and D ...
break;
// Rule: Default is required
default:
// ... do something or validation ...
break;
}
class SmallStruct {
void* p;
int v;
};
class BigClass {
int arr[20];
}
// Rule: For small structure (<= 2 * sizeof(void*)), passing by value
// Rule: For big class, passing by const reference
void Foo(SmallStruct smallValue, BigClass const& bigValue)
{
// Rule: var
int apple = 5;
// ... do something ...
}
Don't
struct Value {
int v;
#if defined(DEBUG)
DebugInfo dbg; // !!! Danger !!!
#endif
};
Do
struct Value {
int v;
std::unqiue_ptr<DebugInfo> dbg;
};