Chapter 11 the power of variable names
Power of variable names
11.1 considerations in choosing good names
Considerations for selecting a variable name
The most important naming considerationi most important naming considerations
To change the name, you must describe exactly what the variable represents.
Problem orientation problem-oriented
A well-remembered name usually reflects problems rather than solutions. A good name usually expresses "what" rather than "how )"
. For example, bitflag is more computer-specific than printerready for a bit field used to indicate the printer status. Traces of calcval calculation in Financial Software
It is more obvious than sum. Therefore, use printerready and sum to name the variables.
Optimum name length the most appropriate Name Length
When the average length of the variable name is 10-16 characters, debugProgramThe minimum effort required is required. Programs with an average length of 8 to 20 characters are almost equally easy to tune.
Try.
The effect of the effect of scope on variable names variable name on scope
· Restrict the name in the global namespace.
C #, C ++, you can use the namespace keyword to divide the global namespace.
Computed-value qualifiers in variable names variable name
Many programs use the following variables to indicate the computing result: total, sum, average, Max, Min, record, String, pointer, and so on to modify a name,
Remember to add the qualifier to the end of the name.
Common opposites in variable names
Correct use of battle words. By applying naming rules, we can improve the consistency of the word usage and improve the readability of the tool. The following are common confrontation words:
Begin/end
First/last
Locked/unlocked
Min/MAX
Next/previous
Old/new
Opened/closed
Visible/invisible
Source/Target
Source/destination
Up/down
11.2 naming specific types of data
Name data of a specific type
Naming loop indexes is the name of the loop subscript
If there are more than a few rows in a loop, the reader will easily forget the meaning of I, so you 'd better change the subscript of the loop to a more meaningful name.
One of the reasons that leads to a longer cycle is the nested usage of the loop. If you use multiple nested loops, you should assign a longer name to the loop variable to improve readability.
.
Naming status variables: Name the status variable
Get a better name than flag for the status variable
Naming temporary variables is named as a temporary variable
Alert for "temporary" Variables
Naming Boolean variables: Name a Boolean variable
1. Remember the typical Boolean variable name
The following are some particularly useful Boolean variable names:
Done; error; found; succes (OK) and so on
2. Give the Boolean variable a name that implies the meaning of "true/false.
For example, names such as status and sourcefile are bad Boolean variable names, because they do not have explicit true or false. Error or statusok should be used;
Sourcefileavailable or sourcefilefound.
Some programmers like to add is before the Boolean variable name they write. In this way, the variable name becomes a problem: isdone? Iserror? Isfound? Use true or false
The answer to this question is the value of the variable.
One of the advantages of this method is that it cannot be used for ambiguous names: isstatus? This is meaningless.
One drawback is that the readability of simple logical expressions is reduced: the readability of IF (isfound) is slightly worse than that of IF (found ).
3. Use a positive Boolean variable name
Negative names such as notfound are hard to read, especially if they are reversed: If not notfound
Naming enumerated types is an enumeration type name
When using the enumeration type, you can use the group prefix, such as color _, planet _, or _ month to explicitly indicate that all members of this type belong to the same group.
In someProgramming LanguageEnumeration members are always prefixed with enumeration names, such as color. color_red or planet. planet_earth. So repeat the meaning of the above prefix
So You Can prefix The Name Of The Enumeration type and simplify the name to color. Red and planet. Earth.
Naming constants is a constant name
When a constant is named, it should be named based on the meaning indicated by the constant, rather than the value of the constant.
11.3 The Power of naming conventions naming rules
11.4 informal naming conventions informal naming rules
· Guidelines for a language-independent convention
Guidelines for language-independent naming rules
1. differentiate variable names and subroutine names
2. Partition classification and objects
3. Mark global variables
4. Identify member variables
5. Label type declaration
6. Mark the named variable
7. Variables of Enumeration type
8. There is no guarantee that the read-only parameters are marked in the read-only language of the input parameters.
9. format the name to improve readability
· Guidelines for language-specific conventions
Guidelines for language-related naming rules
· Mixed-language programming considerations
Considerations for Mixed Language Programming
· Sample naming conventions
Naming rule example
11.5 standardized prefixes
Standard prefix
· User-defined type abbreviations
The abbreviation of UDT can identify the data type of the named object or variable. The abbreviation of UDT can be used to represent objects such as forms, screen areas, and fonts.
· Semantic prefixes semantic prefix
The Semantic prefix goes further than that of UDT. It describes how variables or objects are used.
· Advantages of standardized prefixes standard prefix
The standard prefix can more accurately describe names with vague meanings.
Standardized prefixes are more compact names.
When your compiler cannot check the abstract data type you use, the standard prefix can help you accurately determine the type: pareformat = docreformat is likely not
Yes, because Pa and Doc are different UDT.
11.6 creating short names that are readable to create readable short names
11.7 kinds of names to avoid names should be avoided
Avoid confusing names or abbreviations
Avoid using names with similar meanings
Avoid using variables with different meanings but with similar names
Avoid using names with similar pronunciation
Avoid using Chinese Characters in names
Avoid spelling words in names
Avoid using words that are often misspelled in English
Do not distinguish variable names by case.
Avoid using multiple natural languages
Avoid using standard type, variable and subroutine names
Do not use names that are completely irrelevant to the meaning of variables.
Avoid confusing characters in the name
Key Points of Key Points
· A Good variable name is a key element to improve program readability. Special considerations are required for special types of variables, such as cyclic subscript and state variables.
· The name should be as detailed as possible. The names that are too vague or too generic and can be used for multiple purposes are usually not good.
· naming rules should be able to distinguish local data, class data, and global data. They should also be able to distinguish between type names, named constants, enumerated type names, and variable names.
· regardless of the type of project, you should adopt a variable naming rule. The type of rules you use depends on the size of your program and the number of project members.
· modern programming languages seldom need abbreviations. If you really want to use the abbreviation, use the project abbreviation dictionary or standard prefix to help you understand the abbreviation.
· Code is read much more than written. Make sure that your name is more focused on reading convenience than writing convenience.