Follow+Star Public Account Number, don’t miss out on exciting content
Editor | strongerHuang
WeChat Public Account | Embedded Column
In software projects, code naming is a very important task. Whether to use uppercase ‘A’ or lowercase ‘a’, Chinese or English… Those who have worked on projects should understand this! Today, I will share some common methods of code naming..Naming ConventionsThe most important consistency rule is naming management. The style of naming allows us to quickly understand the meaning of a name—type, variable, function, constant, macro, etc.—without needing to look up type declarations.Naming rules have a certain degree of arbitrariness, but consistency is more important than personal preference. So regardless of whether you think they are important, rules are rules.
General Naming Rules
1. OverviewFunction naming, variable naming, and file naming should be descriptive; avoid abbreviations.2. ExplanationUse descriptive names as much as possible; don’t worry about space. After all, making the code easier for new readers to understand is more important. Avoid using abbreviations that only the project developers can understand, and don’t shorten words by cutting off a few letters.For example:
Note that some widely known abbreviations are acceptable, such as using ‘i’ for iteration variables and ‘T’ for template parameters.The naming of template parameters should follow the corresponding classification: type template parameters should followtype naming rules, while non-type templates should followvariable naming rules.
File Naming
1. OverviewFile names should be all lowercase, and can include underscores (_) or hyphens (-), according to project conventions. If there are no conventions, then ‘_’ is preferred.2. ExplanationFile names should generally be made clearer; bsp_i2c.h is better than i2c.h.Additionally, source files and header files should ideally appear in pairs, such as bsp_i2c.c and bsp_i2c.h .There are many ways to define them, such as using underscores or hyphens:
- bsp_i2c.c
- bsp-i2c.c
- bspi2c.c
Type Naming
1. OverviewType names should have each word’s first letter capitalized, without underscores: MyExcitingClass, MyExcitingEnum.2. ExplanationAll type naming—classes, structures, type definitions (typedef), enumerations, type template parameters—should use the same convention, starting with a capital letter, with each word’s first letter capitalized, and without underscores.For example:
Variable Naming
1. OverviewVariable (including function parameters) and data member names should be all lowercase, with words separated by underscores. Class member variables should end with an underscore, but structure members do not, e.g., a_local_variable, a_struct_data_member, a_class_data_member_.2. ExplanationGeneral Variable NamingExamples:
Class Data MembersWhether static or non-static, class data members can be named like ordinary variables, but should have an underscore.
Structure VariablesWhether static or non-static, structure data members can be named like ordinary variables, without needing an underscore like classes:
Constant Naming
1. OverviewVariables declared as constexpr or const, or those whose values remain unchanged during program execution, should be named starting with ‘k’, using mixed case. For example:const int kDaysInAWeek = 7;2. ExplanationAll variables with static storage duration (e.g., static variables or global variables, see storage duration) should be named this way. For other storage duration variables, such as automatic variables, this rule is optional. If this rule is not followed, then use the general variable naming rules.
Function Naming
1. OverviewRegular functions use mixed case, while getter and setter functions should match the variable names: MyExcitingFunction(), MyExcitingMethod(), my_exciting_member_variable(), set_my_exciting_member_variable().2. ExplanationGenerally, the first letter of each word in function names is capitalized (i.e., “camel case” or “Pascal case”), with no underscores. For acronyms, it is preferred to treat them as a single word and capitalize the first letter (e.g., write StartRpc() instead of StartRPC()).
(The same naming rules also apply to constants in class scope and namespace scope, as they are exposed as part of the API, and should look like a function, since at this point, the fact that they are actually an object rather than a function is an irrelevant implementation detail)The naming of getter and setter functions is consistent with the variables. Generally, their names correspond to the actual member variables, but this is not strictly required. For example, int count() and void set_count(int count).
Enumeration Naming
1. OverviewEnumeration names should be consistent with constants or macros: kEnumName or ENUM_NAME.2. ExplanationIndividual enumeration values should preferably adopt the naming style of constants. However, the macro naming style is also acceptable. Enumeration names like UrlTableErrors (and AlternateUrlTableErrors) are types, so they should use mixed case.
Macro Naming
1. OverviewYou don’t intend to use macros, right? If you must use them, name them like this: MY_MACRO_THAT_SCARES_SMALL_CHILDREN.2. ExplanationRefer to preprocessor macros; generally, macros should not be used. If you must use them, their naming should be all uppercase like enumeration naming, using underscores:#define ROUND(x) …#define PI_ROUNDED 3.0Declaration:This article’s material is sourced from the internet, and the copyright belongs to the original author. If there are any copyright issues, please contact me for removal.———— END ————
● Column “Embedded Tools”● Column “Embedded Development”● Column “Keil Tutorial”● Selected Tutorials from the Embedded ColumnFollow the public accountReply “Add Group” to join the technical exchange group according to the rules, reply “1024” to see more content.
Click “Read the original text” to see more shares.