Hello, I’m Liang Tang.

Today is EasyC++ topic 3, you can click to jump to github repository ~

Welcome to star, welcome to PR

Variable specification

The declaration of a variable in C++ consists of the variable type + the variable name.

There are several rules for C++ naming:

  • The name contains only letters, digits, and underscores (_)
  • The first character of a name cannot be a number
  • Case sensitivity
  • C++ keywords cannot be used (such as class, void, etc.)
  • User-defined identifiers cannot contain two underscores in a row, and cannot start with an underscore followed by a capital letter. Identifiers defined outside the function cannot start with an underscore
  • C++ has no restrictions on name length, but some platforms do

For beginners, it can be confusing to know what a reasonable and complete coding specification is because there is not enough code to write or read. For this, we can refer to Google’s C++ coding specification.

The general

The general rule is to use descriptive names whenever possible and not skunk on variable length, since it’s more important to make code easy to understand than easy to type.

Let’s look at a few positive and negative examples, starting with the positive:

int price_count_reader;    / / no abbreviations
int num_errors;            // "num" is a common expression
int num_dns_connections;   // Everyone knows what "DNS" is
Copy the code

The advantages of these three variable names have also been written in the comments. There are no ambiguous abbreviations, and the variable names are full of meaning. Here are some examples of how not to:

int n;                     // It makes no sense.
int nerr;                  // Ambiguous abbreviations.
int n_comp_conns;          // Ambiguous abbreviations.
int wgc_connections;       // Only your team knows what that means.
int pc_reader;             // There are too many possible explanations for "PC".
int cstmr_id;              // Cut some letters.
Copy the code

The main reason is that there are so many abbreviations that the readability is almost nil. Not only is it unreadable, but the author who wrote the code quickly forgets its original meaning.

The file name

The file name should be all lowercase and can contain underscores as well as characters -, preferably underscores.

For example: my_class.cc, myclass.cc, etc. This is only the Google standard, using the. CPP suffix is also ok, as long as it is consistent.

Type name

Type names are humped and do not contain underscores, such as MyExcitingClass, HelloWorldClass, etc.

Type naming includes classes, structs, typedefs, enums, and type template parameters, all of which use the same convention. That is, the beginning of a capital letter, the beginning of each word is also capital, that is, the big hump.

Such as:

// Class and structure
class UrlTable {.class UrlTableTester {.struct UrlTableProperties {.// Type definition
typedef hash_map<UrlTableProperties *, string> PropertiesMap;

/ / using an alias
using PropertiesMap = hash_map<UrlTableProperties *, string>;

/ / the enumeration
enum UrlTableErrors {.Copy the code

Variable naming

Variables in C++ do not follow the small hump as in Java, but are all lowercase, with underlined lines between words.

Such as:

string table_name;  // Ok - underline.
string tablename;   // Ok - all lowercase.

string tableName;  // error - mixed case
Copy the code

But if it is a data member of a class (a member variable), an extra underscore is required at the end of the variable:

class TableInfo {.private:
  string table_name_;  // Ok - underline after.
  string tablename_;   / / good.
  static Pool<TableInfo>* pool_;  / / good.
};
Copy the code

Structures, however, are still named like normal variables.

struct UrlTableProperties {
  string name;
  int num_entries;
  static Pool<UrlTableProperties>* pool;
};
Copy the code

C++ variable naming is similar to Python and different from Java, which may be a little awkward for those of you who have learned Java.

Constant named

Variables declared as constExpr and const, or other variables that are not defined but remain constant throughout the program, are named starting with k, and are case-mixed, as in: constExpr

const int kDaysInAWeek = 7;
Copy the code

The function name

Function naming also follows the big camel’s back nomenclature, that is, capitalizing the first letter of each word.

For special abbreviations, it is common to treat them as ordinary words and not to use all caps. For example:

void GetUrl(a); / / right
void GetURL(a); / / error
Copy the code

The namespace

All lowercase names

A macro named

Macros are named in all caps and separated by an underscore, as in:

#define ROUND(x) ..
Copy the code

However, using macros in your code is not recommended.

The enumeration name

Consistent with a constant or macro, that is, kEnumName or ENUM_NAME. Personally, I prefer the latter.

Resources: Google C++ documentation and related web blogs