Two worst variable names in history

Source: Internet
Author: User

Two worst variable names in history

As a programmer, "Naming" is a very important part of their work. Phil Karlton said: "There are two major challenges in the computer science field: How to verify cache and how to name everything ." Although it is difficult, it is unavoidable to name a thing 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 customers) and costumers clothing providers, 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.

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 change as $ 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 cake.

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 note on it, with the words: "things"

Correct variable naming should clearly specify the Data Type of the variable. Therefore, data type 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:

 
 
  1. $data = read_record();

  2. print "ID = ", $data["CUSTOMER_ID"];

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. 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.

In general, any variable names that rely solely on numerical numbers should be reconstructed. I will give you an example and you will understand:

 
 
  1. $total = $price * $qty;

  2. $total2 = $total - $discount;

  3. $total2 += $total2 * $taxrate;

  4. $total3 = $purchase_order_value + $available_credit;

  5. if ( $total2 < $total3 ) {

  6. print "You can't afford this order.";

  7. }

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:

 
 
  1. if ( $total2 < $total3 )

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. 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.

Let's use the simplest "search-Replace" feature to solve our previous problems

 
 
  1. $order_total = $price * $qty;

  2. $payable_total = $order_total - $discount;

  3. $payable_total += $payable_total * $taxrate;

  4. $available_funds = $purchase_order_value + $available_credit;

  5. if ( $payable_total < $available_funds ) {

  6. print "You can't afford this order.";

  7. }

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 a SHA-1 hash object, we should simply name it $ sha1, which is good. You do not need to convert it into $ 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?

Original article: http://petdance.com/2012/04/the-worlds-two-worst-variable-names/
Http://blog.jobbole.com/18304/.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.