So named in Python variable is to give yourself digging

It follows a string of code that I believe many people have written, but probably no one except you can read:

for range in range(n):
    for j in range(m):
        for k in range(l):
            temp_value = X [i] [j] [k] * 12.5 
            new_array [i] [j] [k] = temp_value + 150

 

In order to facilitate the drawing, using a simple no actual meaning of variables and values ​​in any program can be used to see who wants to find a half-day or up and down in order to know what is the meaning of variables, others to modify and debug great trouble.

So if your code is not just for yourself, just try not to be so written, more use of descriptive variable names and named constants write code, to develop a good habit of naming variables.

Take a look at the standard variable how to write the code above:

PIXEL_NORMALIZATION_FACTOR = 12.5
PIXEL_OFFSET_FACTOR = 150

for row_index in range(row_count):
    for column_index in range(column_count):
        for color_channel_index in range(color_channel_count):
            normalized_pixel_value = (
                original_pixel_array[row_index][column_index][color_channel_index]
                * PIXEL_NORMALIZATION_FACTOR
            )
            transformed_pixel_array[row_index][column_index][color_channel_index] = (
                normalized_pixel_value + PIXEL_OFFSET_FACTOR
            )

 

Although the code amount increases, but others see that you understand, no matter how often look soon be able to understand, and the first piece of code, you can look at after a while understand is doing it?

Good programmers will abide by the code variable naming rules:

  • Variable names should be able to describe information variable represents.
  • Give priority to understandability of code, rather than the speed of writing code.
  • A standard naming convention to make a global decision rather than multiple local decision.

So named is not the standard for two main reasons:

  • Useless / confusion / blurred variable name
  • Unnamed "magic" constant

In short, do not like this named variables:

X, y, xs, x1, x2, tp, tn, clf, reg, xi, yi

To correct also very simple, following three simple rules to follow:

  • Variable / function name: lower case letters, intermediate divided underlined
  • Constant: capital letters
  • Class categories: the first letter capitalized, the rest lowercase

In detail, there are so many considerations:

  • ###  Xand y:

X and y you may have seen hundreds of times, know their function, but for other developers to read the code, it may not be obvious. So, we might use to describe these variables is what the name represents, for example: house_features and house_prices.

  • value

What value is it specific value? It may be a velocity_mph, customers_served, efficiency, revenue_total. Different value plus a final detailed prefix.

  • ### temp

Although you can use a temporary variable temp said, but it is best to give it a meaningful name. For example, is the need to convert the value of the unit, write like this:

# Do not do 
the TEMP = get_house_price_in_usd (house_sqft, house_room_count) 
final_value = the TEMP * usd_to_aud_conversion_rate 

# to do so (small series finishing a set of Python data and PDF, need to learn Python learning materials can be added to the group: 631 441 315, anyway idle is idle it, it is better to learn something ~ ~ ) 
house_price_in_usd = get_house_price_in_usd (house_sqft, 
                                            house_room_count) 
house_price_in_aud = house_price_in_usd * usd_to_aud_conversion_rate

 

  • ### Abbreviations:

If you use abbreviations variable, ahead of the best meaning of the abbreviation declare variable names, such as usd, aud, mph, kwh, sqftthese. In agreement with the other members of the common abbreviation for the team and write it down. Then, in the code review to ensure the implementation of these standards in writing.

  • ### machine learning to avoid certain abbreviations:

For example, try not to use  tptnfpfn, these abbreviations variables, but the use of the full variable instead: true_positives, true_negatives, false_positives, and false_negatives.

Leading to the direct cause of the error variable name?

Here are two reasons for this error is an important reason to write the variable name:

  • We want to keep variable names short
  • The code is converted directly to formula

According to the above formula, under normal circumstances it is likely to be directly written as:

temp = m1 * x1 + m2 *(x2 ** 2)
final = temp + b

 

It is written in code that looks like a machine, although the computer will eventually run it, but the number of people looking for more, better to write the code for people to understand:

house_price = price_per_room * rooms + price_per_floor_squared *(floor** 2)
house_price = house_price + expected_mean_house_price

 

Do not use the magic number

Constant magic number is not a variable name, typically used for unit conversion. But such a conversion code is wrong:

final_value = unreverted_value * 1.61 
final_quantity = quantity / 60 
value_with_offset = value + 150

 

Nobody understood what units represented 1.61,60, naturally see the variables do not understand the meaning of the conversion, so it is best to convert this value to the variable name, for example:

USD_TO_AUD_CONVERSION_RATE = 1.61 
price_in_aud = price_in_usd * USD_TO_AUD_CONVERSION_RATE

REVOLUTIONS_PER_MINUTE = 60 
revolution_count = minutes_elapsed * REVOLUTIONS_PER_MINUTE

 

These are some of the details need to pay attention during the variable name in the code.

Guess you like

Origin www.cnblogs.com/qingdeng123/p/11329525.html