<< >> Code Complete read two notes of the power of variable names

Notes 1. The variable named

1) intelligibility

Wang Wen-defined variable you want to know, do not look at other variables to see this code will know what it means, it means that

Good variable life: currentDate,heartRate

Bad variable name: newButton,peopleCnt



2) Readability

Variables not only understandable, but also easy to read, and if the variable like this: numberOfShitsInChinaMansFootballNationalTeam, too long, will be mad

The study found that the length of the variable names in the length of 8 to 20 characters each is easy to debug, so the above variables can be changed numShitsInTeam, to make a balance between readability and understandability


2. The impact on the scope of the variable life

Short variable names is not necessarily bad, like for i in healthInfos:, i represents a temporary data, restrict the scope only in this for loop

Studies have shown that: the longer the name applied to or global variables rarely used, and the shorter name applies to local variables or loop variable.

However, short of variable life often cause some trouble, therefore, as a defensive programming strategy, you should try to avoid using short variable names


3. Calculation of the value of the variable name qualifier

Variable code often appears to indicate that the result of the calculation, total, average, maximum, minimum and so on, use the total, average, max, minand other qualifiers, so please remember to add the qualifier last name.

The advantage of this approach is that:


1) the protruding main meaning

For revenueAverage, the reader first thing you see is the revenue, you know, it means that the income is; if averageRevenue, reader first sight average, just know that this is the average, the average of what it is? do not know! This is because in averageand revenueinside, revenueare subject, represent the main meaning averageonly qualifier, so the body on the front, more conducive to understanding


2) to increase consistency

If after there is a similar variable revenueTotal, revenueMax, revenueMin, , expenseTotal, expenseMaxit will be very very neat


4. The name of a particular type of data

1) for loop variable naming

For python for loop, do not use i, infosuch as a temporary variable does not represent a specific meaning, as this reader can not guess the name of the variable according to the significance of variables

Good example:for health_info in health_infos

Bad example:for i in health_infos

Health_infos which represents a user health data storage array, for health_info in health_infosreaders tell at a glance health_info on behalf of a user of a health data


2) a state variable named

Do not use a simple flag state variable named


CHECKLIST

1) variable name completely and accurately express the meaning of the variables represent it?

2) variable name reflects the real-world problems instead of programming languages ​​do?

3) the variable name is too long?

4) If there is calculated qualifier, it was placed after the variable name yet?

5) variable names for loop can represent the specific meaning of it?

6) naming able to distinguish between local data, data type and global data?

It is consistent 7) abbreviations of all the words the way?

Guess you like

Origin www.cnblogs.com/zzliu/p/11965655.html