Chapter XI of the power of variable names

Selection Considerations good variable name

A good variable name is readable, easy to remember and get just the right.

The most important Naming Considerations

The most important consideration is the name to fully and accurately describe the things that variable represents. In general, it is the best description of variable names to variables.

Problem-oriented

A friendly name usually reflects the problem, not the solution.

The most appropriate name length

When the average length in the 10-16 character variable names when debugging effort required is minimal cost. The average length of the name in the 8-20-character program is also easier to debug.

The impact on the scope of variable names

Longer name applies to variable or global variables rarely used, and the shorter name is applied to local variables or loop variable. For global namespace names added qualifier.

Calculating a value variable name qualifier

Many programs have represented the results of variables: total, average, maximum, and so on. If you want to use like Total, Sum, Average, Max, Min, Record, String, Pointer qualifier to modify such a name, remember the name of the last qualifier added.

Common antithesis word variable name

By applying naming conventions to improve the consistency of the use of the word antithesis, the antithesis commonly used words:

  • begin/end
  • first/last
  • locked/unlocked
  • min / max
  • next/previous
  • old/new
  • opened/closed
  • visible/invisible
  • source/target
  • up/down

Specific types of data designated as

This section describes the loop variable, state variable, temporary variables, Boolean variables, enumerated types, and named constants relevant considerations.

  • Is named cycle index;
  • Named for the state variables;
  • Named temporary variables;
  • Named Boolean variables;
  • Enumerated type named;
  • A constant name;

The power of naming

This section describes why, when and how to create your own variable naming standards.

Why should the rules

Naming rules can bring the following benefits:

  • It requires you to play by the rules more;
  • Help to transfer knowledge between projects;
  • Help you learn more quickly the code in the new project;
  • Help reduce the proliferation of names;
  • To make up for the shortcomings of programming languages;
  • To emphasize the relationship between the dependent variable.

When using naming conventions

  • When multiple programmers to develop a project;
  • When you plan a program to be transferred to another programmer to modify and maintain the time;
  • When you evaluate a program you wrote in the organization of other programmers times;
  • When you write the program size is too large, so that while you can not understand the big picture in mind, but must divide and conquer time;
  • When you write a program life cycle long enough, you may grow to again start work on the program when it shelved after a few weeks or months;
  • When you do not, there are some common terms in a project, and want to use a standard data coding stage or the abbreviation of time.

Formal procedures

Typically, the degree of formality you need depends on the number of staff working for the same program, the size of the program, and the program is expected to declare the period.

Informal naming convention

Most of the projects are similar to that used in this section are talking about a relatively informal naming convention.

Guidelines for language-independent naming

  • Distinguish between variable names and subroutine names: naming conventions used in this book required variable and object names begin with a lowercase letter, subroutine names begin with a capital letter.
  • Area classification and the object:
    • By a capital letter to distinguish types and variables;
    • Differentiate the type of variables and all caps;
    • By adding a type "t_" prefix to distinguish the types and variables;
    • By adding to the variable "a" and the prefix to distinguish the type of variable;
    • By using variable names clearer distinction between types and variables.
      • Identify global variables: g_
      • Identifies the member variables: m_
      • Identifies the type declaration: t_
      • Logo named constants: c_
      • Identify the elements enumerated type: e_

Guidelines relating to the language naming

C naming rules

  • and c ch is a character variable;
  • i and j are integer indexes;
  • n represents the number of something;
  • p is a pointer;
  • s is a string;
  • All caps preprocessor macro;
  • Variable names and subroutine names all lower case;
  • Underscore as a separator.

Mixed-Language Programming Notes

In mixed-language programming environment can be made to optimize the naming rules to improve the overall consistency and readability.

Standard prefix

  • About user-defined type abbreviation
  • Semantic prefix
  • The advantages of the standard prefix
    • Standard prefix can describe more accurately the meaning of some of the more obscure names;
    • Standardized prefix is ​​the name of a more compact;
    • Standard prefix can help you the most accurate type of judgment.

Create a short name with readability

Abbreviations general guidelines

Here are a few guidelines to create an abbreviation:

  • Use standard abbreviations;
  • Remove all non-leading vowel;
  • Remove function words;
  • Use each word in the first or the first few characters, the mother;
  • Uniformly after the first, second or third letter of each word of the cut;
  • Retains the first and last word of each letter;
  • Using the name of each important word, does not exceed a maximum of three more;
  • Removing unwanted suffix;
  • Ensure that each syllable pronunciation of the most interesting;
  • Make sure not to change the meaning of the variables;
  • Repeated use of the above techniques until you put the length of each variable name is reduced to 8-20 characters.

Comments on the abbreviation

  • Do not delete the way a character from each word to an abbreviation;
  • Abbreviations to be consistent;
  • You can read to create a name;
  • Avoid using easily read wrong or the wrong combination of characters;
  • Use the dictionary to resolve naming conflicts;
  • A description of all abbreviations in a project-level "standard abbreviations" document;
  • Remember, the code name for the reader's meaning is more important than the author.

The name should be avoided

Here's what variables should avoid giving out guiding principles:

  • Avoid using misleading names or initials;
  • Avoid using names with similar meaning;
  • Avoid using variables have different meanings but similar names;
  • Avoid using similar sounding names;
  • Avoid using numbers in the name;
  • Avoid misspelled word in the name;
  • Do not relying on case to distinguish variable names;
  • Avoid using a variety of natural language;
  • Avoid using names of standard types, variables and subroutines;
  • Do not use the variable name meaning nothing to do;
  • Avoid confusing contains characters in the name.

Checklist: variable name

General Considerations named

  • [] Name completely and accurately express the meaning of the variables represents yet?
  • [] Name reflects the real-world problems instead of programming languages ​​do?
  • [] Name long enough, so that you do not need to be agonizing over it?
  • [] If you have calculated qualifiers, it was on the last name of it?
  • [] Count by name or Index instead Num yet?

Specific types of data designated as

  • [] Cycle under the subject name make sense?
  • [] All the "temporary" variables are re-ordered in a more meaningful name yet?
  • [] When the value of the Boolean variables true, the variable names can accurately express the meaning of it?
  • [] Contains an enumerated type name can represent their classes prefixes or suffixes yet?
  • [] Is named, according to a long abstract entity it represents rather than its figures represent named it?

Naming Rules

  • [] Can distinguish between local data rules, data types and global data?
  • [] Rules can distinguish the type name, named constants, enumerated types and variable name?
  • [] Rules can identify the compiler does not enforce read-only parameter detection of the input parameters language subroutine do?
  • [] Rule as compatible as possible with the standard rules of the language it?
  • [] Name to be formatted for readability it?

Short name

  • [] Code uses a long name?
  • [] If only to avoid a character abbreviation and the name of the province?
  • [] All the words of the abbreviations are consistent with it?
  • [] Name can be read out of it?
  • [] Avoid using easily misread or read the wrong name?
  • [] In the abbreviation of control in making short name says it?

Points

  • Good variable names are a key element to improve the readability of the program;
  • Name to be as specific as possible;
  • Naming convention should be able to distinguish between local data, and a global class data transactions;
  • Regardless of the type of projects to do, you should use some variable naming conventions;
  • Few modern programming language to use abbreviations;
  • The number of times to write the code to read far more than.

Guess you like

Origin www.cnblogs.com/liam-ji/p/11517387.html