Skip to content

Coding Standards Naming

Adam Martin edited this page Feb 13, 2021 · 2 revisions

Naming

Naming is such a subjective topic and there is always cases where breaking the "rules" is more important to making code legible or easier to understand, that the following is meant to be used as more guidance than rule.

Variables

Give as descriptive a name as possible, within reason. Do not worry about saving horizontal space as it is far more important to make your code immediately understandable by a new reader. Do not use abbreviations that are ambiguous or unfamiliar to readers outside your project, and do not abbreviate by deleting letters within a word. To avoid confusion with a function/method or class/struct name the should be snake_case.

Avoid the usage of global variables. Also try to avoid the usage of preprocessor constants, instead use static const or even better constexpr for constants as they have both type and scope, making them safer to use.

When you define a pointer or a reference, try to keep the * or & or && next to the type and not the name.

Variable names should be:

  • All lowercase
  • snake_case
string table_name;  // OK - uses underscore.
string tablename;   // OK - all lowercase.

Good examples

int price_count_reader;    // No abbreviation.
int num_errors;            // "num" is a widespread convention.
int num_dns_connections;   // Most people know what "DNS" stands for.
int i,j,k,n,x,y,z;         // Typical loop counter variables. Try to no abuse of it 

Bad examples

int nerr;                  // Ambiguous abbreviation.
int n_comp_conns;          // Ambiguous abbreviation.
int wgc_connections;       // Only your group knows what this stands for.
int pc_reader;             // Lots of things can be abbreviated "pc".
int cstmr_id;              // Deletes internal letters.

Classes and Structs

Classes and structs should be named using UpperCamelCase and should be a noun. Avoid using terms such as list and manager as they are to overused or ambiguous. There is a good time and place just make sure it is that time and place.

Classes should try to do 1 job only. If it looks like a class is doing multiple things it maybe best to create 2 distinct classes.

Structs should be used for primarily for data and minimal methods.

Class/Struct names should be:

  • UpperCamelcase
  • Noun

Method names should be

  • UpperCamelCase
  • Verb or action
class Foo {
public:
    int GetTotalFoos();
    bool IsActive();
};

Functions

Function naming is similar to methods, except they should also describe what they are working on since they are not tied to a class/struct.

Function names should be

  • UpperCamelCase
  • Verb or action
int ParseString(std::string);
bool ContainsAdminFlag(Falgs[]);

Typedefs and Using

Typedefs and using are used to rename concepts into something more human readable/understandable such as a long set of templated parameters becoming an easy to read name.

The naming should follow similar to class/struct naming rules

// typedefs
typedef hash_map<UrlTableProperties *, string> PropertiesMap;
typedef uint8_t  Byte;

Enums and Constants

Enums and constants provide a way to ensure constraints and consistency amongst a group of programmers or over a period of time. By creating a constant to describe the update frequency instead of using a magic number will give it more importance and make it so it only needs to be updated in one location.

Enums and Constants names should be:

  • All capitals
  • CAPITAL_SNAKE_CASE
enum class Colors {
    RED, 
    BLUE, 
    GREEN
};
const float PI = 3.1416....;
constexpr float DESTRUCTION_RATIO = 0.1f;