Original English:The world's two worst variable names
As a programmer, "Naming" is a very important part of their work. Phil karlton said: "In the computer science field, there are two major challenges: how to invalidate cache and how to name a variety of things ." Although difficult, naming is unavoidable every time you write code. Whether it's a program variable name, a database table name, or a column name in the table, or a file name in the file system, or your project name or product name, it's not easy to name these items.
Bad naming methods are everywhere. You will find that some variable names are too short to provide sufficient descriptive information. Maybe people who have this problem have been TRS-80 Basic programmers, in this basic language, no matter how long variable name you start, only the first two letters of the name is valid, so at that time, programmers had to put a notebook on the keyboard side to record the short variable names and their corresponding meanings so that they could not be confused.
Sometimes, you may find that the original letter in the variable name is omitted directly to shorten the variable length. This method is used to replace the common "truncation method", that is, simply truncates letters to shorten the length. For example, you can replace $ cstmr with $ Cust ). however, for the words customers (customer) and costumers (clothing supplier), the original sound omitting method will cause confusion (customers and costumers use the original sound omitting method, and the result is cstmr ). What's worse, $ cstmr lacks Original letters, making typing more awkward and difficult to pronounce from the perspective of pronunciation.
Tsr80 computer, supports only four variable names
There is also a special naming method for humans. Sometimes the author of the program just creates some interesting names for the sake of quiet. I have seen someone name a loop variable $ crap (crap is a slight curse in English, similar to Damm, shit ), one of my colleagues told me that when he sorted out a piece of code, he saw a function named the_lone_ranger_rides_again (). Although such naming methods are very special, they do not fall into the category of what I call "bad.
Although I am very clear that everyone is justified in naming conventions, I am confident to announce that the worst name in history is:$ Data!
Yes! It must be $ data! This name is completely a loop definition, which is actually a nonsense. It's as if you threw everything in your house into a suitcase, and then pasted a piece on it, with the words "things ".
Correct variable naming should clearly specify the Data Type of the variable. Therefore, considering the data type during naming is a good way to improve the naming quality. I once saw the $ data name when reading a code record of a database table, which is like the following:
$data = read_record(); print "ID = ", $data["CUSTOMER_ID"];
If you ask, "What is $ data ?", Then you want to change its name. Changing the name to $ record is a good start. It is better to change it to $ custormer_record.
Fuzzy naming is rather bad, followed by a variable name that looks almost the same and cannot be distinguished. Therefore, the second worst variable name in history is:$ Data2.
In general, any variable names that rely solely on numerical numbers should be reconstructed. I will give you an example and you will understand:
$total = $price * $qty; $total2 = $total - $discount; $total2 += $total2 * $taxrate; $total3 = $purchase_order_value + $available_credit; if ( $total2 < $total3 ) { print "You can't afford this order."; }
You can find that reading this code is as painful as reading oracle. Obviously, the purpose of this program is to calculate the total cost of the order-$ total. If the logic of the program is correct, the variable name $ total is also appropriate. However, some people modified this program and added the Discount calculation and tax rate calculation functions. Then, he was lazy in variable naming and started $ total2 directly. What's more hateful is that, other people calculate the amount of the user's available account in this program, and then name it $ total3!
The following code is really unfortunate:
if ( $total2 < $total3 )
If you don't look back at the previous code, it's totally impossible to know the meaning of the Code. So you have to read back to see what the variable actually means.
If you see a variable like $ total2, you should change the name to a more specific one. It takes 5 minutes to make these variable names more reasonable. Software refactoring at this level is the simplest, the least cost, and the safest, especially when the variable you want to modify is a local variable.
Let's use the simplest "search-Replace" feature to solve our previous problems
$order_total = $price * $qty; $payable_total = $order_total - $discount; $payable_total += $payable_total * $taxrate; $available_funds = $purchase_order_value + $available_credit; if ( $payable_total < $available_funds ) { print "You can't afford this order."; }
After modification, the only change is the variable name, and the code becomes easy to understand. Now there is no ambiguity for each _ total. Let's take a look at what we found: the locations of the two comparison variables in the original if statement are reversed. Effective naming methods allow us to quickly discover errors.
We usually think that using numbers as the end of a variable is a bad naming method, but there is an exception. If the object described by the variable itself ends with a number, it is best to end the variable name with a number. For example, if we want to define an ha-1 hash object, we should simply name it $ sha1, which is good. You do not need to make it sha_one, to avoid using numbers in variable names.
After I completed the first version of this article, I created my own naming rules and used the Perl: critic package to detect the two naming problems mentioned above. My plug-in Perl: CRITIC: bangs can detect these two problems: prohibitvaguenames and prohibitnumbernames.
What other bad naming methods drive you crazy? Are you sure you want to correct these mistakes?
From: http://kb.cnblogs.com/page/153129/