1 constant
Constant values are easier to understand, track, and analyze, so constants should be used instead of variables as much as possible. When defining values
As the default option.
Rule 1.1 uses const constants to replace macros
Note: macros are simple text replacements that are completed during the preprocessing phase. When an error is reported, corresponding values are directly reported.
Value, rather than macro name. The macro has no type check and is not secure. The macro has no scope.
Example:
# Define max_msisdn_len (20) // bad example
Const int max_msisdn_len = 20; // A Good Example
Rule 1.2 a group of related Integer constants should be defined as Enumeration
Note: enumeration is used based on:
Enumeration is safer than # define or const int, because the compiler checks whether the parameter value is within the enumerated value range
To avoid errors.
Example:
// A good example:
Enum dayofweek {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };
Enum color {black, blue, white, red, purple };
Bool colorizecalendar (dayofweek today, color todayscolor );
Colorizecalendar (Blue, Sunday); // compilation error, blue and Sunday Location Error
// Bad example:
Const int Sunday = 0;
Const int Monday = 1;
Const int black = 0;
Const int Blue = 1;
Bool colorizecalendar (INT today, int todayscolor );
Colorizecalendar (Blue, Sunday); // No error is reported
When the enumerated value needs to correspond to a specific value, a value must be displayed during the declaration. Otherwise, you do not need to assign values explicitly to avoid repeated values,
Reduces maintenance (adding or deleting members) workload.
Example:
// A good example: the device ID value defined in The s protocol, used to identify the device type
Enum tdevicetype
{
Dev_unknown =-1,
Dev_dsmp = 0,
Dev_ismg = 1,
Dev_wapportal = 2
};
The program is used internally only for classification and should not be explicitly assigned.
Example:
// Good example: enumeration definition used in the program to identify the session state
Enum tsessionstate
{
Session_state_init,
Session_state_closed,
Session_state_waiting_for_rsp
};
Avoid repeated enumeration values as much as possible. If the enumerated values must be repeated, use the defined enumeration. For example:
Typedef Enum
{
Rtcp_sr= 200,
Rtcp_min_type = rtcp_sr, // must be lowest known type
Rtcp_r-= 201,
Rtcp_sdes = 202,
Rtcp_bytes = 203,
Rtcp_app = 204,
Rtcp_rtpfb = 205,
Rtcp_psfb= 206,
Rtcp_xr= 207,
Rtcp_rsi= 208,
Rtcp_pubports = 209,
Rtcp_max_type = rtcp_pubports // must be highest known type
} Rtcp_type_t;
A constant unrelated to Rule 1.3, which must be defined separately even if the values are the same.
Note: A constant is only used to indicate a specific function. That is, a constant cannot be used for multiple purposes.
Example:
// Good example: Protocol A and Protocol B. The length of the mobile phone number (msisdn) is 20.
Unsigned const int a_max_msisdn_len = 20;
Unsigned const int B _max_msisdn_len = 20;
// Or use different namespaces:
Namespace alib
{
Unsigned const int max_msisdn_len = 20;
}
Namespace blib
{
Unsigned const int max_msisdn_len = 20;
}
We recommend that you use const 1.1 as much as possible.
Note: add the keyword const before declared variables or parameters to indicate that the variable value cannot be tampered. Add const to the class member function
The qualifier indicates that the function does not modify the state of the class member variable.
Common const scenarios:
Function parameter: when a reference is passed, if the function does not modify the input parameter, the parameter should be declared as Const.
Member functions: access functions (such as get functions); do not modify functions of any data member; do not call non-const functions, no returned number
Non-const pointer or referenced function of a data member.
Data Member: if the data member does not change after the object is constructed, it can be defined as Const.