Chapter 11-variable naming (1)

Source: Internet
Author: User

1.Selected variable name

This seems to be a common problem, but the code complete Parsing is very delicate. Let's use the code to understand it. See the following Java code:

X = x-xx;

Xxx = Aretha + salestax (aretha );

X = x + latefee (x1, x) + xxx;

X = x + Interest (x1, X );

Can you understand this question? How do you feel when such code is modified. The program name does not express its purpose, type, or attribute.

A piece of excellent Java code is as follows:

Balance = balance-lastpayment;

Monthlytotal = newpurchases + salestax (newpurchases );

Balance = balance + latemer( mermerid, balance) + monthlytotal;

Balance = balance + Interest (customerid, balance );

The code below completes the same function is obviously more readable and easier to remember. This example illustrates the importance of variable naming. A good variable name must follow at least one principle to express what it is. Note that it expresses what rather than how. For example, the variable that records employee information can be called inputrec or employeedate. Do you think that name will be better? Of course it is the latter. inputrec does not think about it from the perspective of the problem. It expresses the computer thinking. The person who looks at the program looks at the problem, so the latter name is better.

2.Variable Name Length

Variable names must be able to accurately express their meanings, and the words used must inevitably be longer. However, if the variable name is too long, it is also less readable, which fully demonstrates that everything cannot be extreme. In addition, it is inconvenient to debug the program if the name is too long. How long or how short can it be suitable for a variable name? The average length is 10 to 16, or 8 to 20. This should be inconclusive, as long as you remember the name to clearly express the meaning of the variable, the length does not look too long or too short. For example:

Too long: numberofw.leontheusolympicteam

Numberofseatsinthestadium

Maximumnumberofpointsinmodernolympics

Too short: N, NP, NTM

N, NS, nsisd

M, MP, Max, points

Moderate: numteammembers and teammembercount

Numseatsinstadium, seatcount

Teampointsmax, pointsrecord

3.Variable name represents the attributes of a Variable

I believe that in most cases, variable I is used to control the number of for loops, and there is no need to think about it for a good term, such as count or index, because the survival time of I is only in a for loop. There is no limit on the length. After all, we need to adapt to local conditions. In code complete, long variable names are suitable for less-used global variables, and short variable names are suitable for local variables or cyclic variables.

You can use a global namespace in C ++ or C # To prevent name conflicts. Java contains packages, but only a limited prefix is added to C or ABAP. For example, in a database, the employee class can be named dbemployee, and the employee class in the interface can be named uiemployee.

4.Computation result qualifier

Many programs have limits on the operation results, such as total, sum, average, Max, Min, record, String, pointer, and code complete. We recommend that you put these limits at the end of the name. In my opinion, this still reflects consistent consistency, which is my biggest experience in The Mythical man-month.

The only exception is the word num. Note that numsales and salesnum have different meanings. The former represents the total sales, and the latter num represents the index. Therefore, it is best to replace num with total or index to cancel ambiguity.

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.