How to choose a good variable name!

  A good variable name can completely and accurately express what it represents.

  Whether a good variable name can be named is essential for the readability and maintainability of a program.

  The naming of variables is so important, but it is not valued by many beginners, with some random mentality to name variables. If you form an incorrect habit, it will make later learning more difficult.

  This time I will sort out some useful variable naming methods for your reference:

 

Basic skills

 1. The variable name should be clearly stated in words as far as possible. It is required to be unambiguous and complete. You can quickly understand the nature of things through the name. And the name should not be too long or too short.

x, x1, x2;    // Bad name, cannot understand the meaning of variable 
date;       // Bad name, the date here includes all dates, the meaning is not accurate 
todaydate; // The current date, you can quickly understand the meaning

  2.  Constrain the length of variable names. Too short a name will increase the difficulty of understanding or semantic confusion, and too long will make the code lines redundant. Therefore, the proper constraint length will make the program interpretation and maintenance more efficient. After all, the quality of the variable name has a greater impact on others' interpretation of the code.

      Research shows that when the variable name is 10-16 characters in length, the effort to debug the program is minimal,

numberOfPeopleOnTheOlympicTeam;     // too long 

n, bp, nst, max;                       // too short 

teamPointsMax, pointsRecord;        // just

  3. After calculating the qualifier, when the variable is named, you will encounter variables that represent the calculation result: total, average, maximum, etc., then you should put  Total  ,  Sum  ,  Max  ,  Min,  etc. after the name you want to modify, and form This habit avoids  the ambiguity of totalRevenue   and   revenueTotal in the program   .

revenueTotal     ,     espenseTotal     ,    revenueAverage   ,    expenceAverage 

totalRevenue, totalEspense, averageRevenue, averageExpense    // It is easy to see that the previous line is more in line with coding habits

  4. Accurate use of match words, and obeying the naming rules for match words helps to maintain consistency and improve readability. Here are some commonly used match words:

open/close        begin/end      show/hide      create/destroy    
lock/unlock       first/last     min/max        start/stop     
get/put           uo/down        old/new        source/target

  5. Keep in mind the typical Boolean variable name, and give the Boolean variable a name that implies "true / false" meaning. For example  done error  ,  found   , success,  etc.

  6. Distinguish between variable names and subprogram names. Variable names start with lowercase letters and subprograms start with uppercase letters. For example  variableName  vs  RoutineName  .

  7. To distinguish the names of classes and objects, there are several standard solutions:

       * Distinguish between types and variables through the beginning of capital letters-  Widget  and  widget  ,  LongerWidget  and  longerWidget  .

       * Differentiate types and variables by adding "t-" prefix ---  t-Widget  and  Widget  ,  t-LongerWidget    and    LongerWidget  .

       * Differentiate types and variables by adding "a" prefix to variables -------  Widget  and  aWidget  ,  LongerWidget  and  aLongerWidget  .

       * Distinguish types and variables by using clearer names for variables -------  Widget  and  employeeWidget  ,  LongerWidget  and  fullEmployeeWidget  .

  8. To identify the global variable, you can add "g-" before the name of the global variable, then when you read this variable, you will clearly know that it is a global variable, such as  g-RuningTotal  ,  g-ColorPointer  .

  Nine. Identify member variables, you need to be able to identify the variable as a data member of the class, you can add "m-" in front of the member name; for example,  m-ClassVariable  ,  m-Velocity  .

  10. Identify the named constant, usually add "c-" before the constant name, and all capitals in C, such as  c-ReceaMax  ,  RECESMAX  .

  11. Use formatted naming to improve readability, commonly used the following two-word method:

      * Use capitalization to separate words, such as  PointerTotal InvalidFirst  .

      * Use separators to separate words, such as  gymnastics-pointertotal pointer-total  .

      These two methods have their own advantages, but they must not be mixed, as it will make the code difficult to read.

  12. Give some tips on variable name abbreviation, which can effectively shorten the variable name and improve the code quality

      * Use standard abbreviations (common abbreviations in the dictionary or people familiar with)

      * Remove function words and, or, the, etc.

      * Remove useless suffixes, ing, ed, etc.

      * Keep the most noticeable pronunciation of each syllable

      * Uniformly truncate the first, second or third letter of each word (choose the most suitable one)

  13. Finally, give some naming conventions that should be avoided

      * Avoid using misleading names and abbreviations

      * Avoid using names with similar meanings

      * Avoid using numbers in names

      * Avoid misspelling words in names

      * Do not use names that have nothing to do with the meaning of variables

 

  Remember, the name is more important to the reader of the code than the author!

                                                                                                                                                                                                                                                                        --参考资料《代码大全》第二版

Guess you like

Origin www.cnblogs.com/Teclado/p/12752606.html