The most important thing to consider when naming a variable is that it completely and accurately describes what the variable represents.
Both currentdate and todaysdate are good names because they both completely and accurately describe the "current date"
This concept. In fact, these two names all use very straightforward words. Programmers sometimes overlook these
And they are often the most explicit. CDs and C are poorly named because they are too short and do not have
Descriptive. Current is bad too, because it doesn't tell you what it is. Date looks good, but after the most
After the scrutiny of it is also a bad name, because the date mentioned here is not all the date can be, but only specifically the current day
And date itself does not express this meaning. X, X1 and X2 are always bad names-Traditionally, X represents a
Unknown quantity; If you don't want your variable to represent an unknown amount, consider taking a better name.
The name should be as clear as possible. Like X, I, these names are broadly available for a variety of purposes, and they
There is not enough information as it should be, so it is often a mistake in naming. Some may retort that the
I used as a circular subscript is the most normal, it must be written indexoftheloop this kind of smelly and long name to count
All right? Steve McConnell that if the loop is only a few lines, and it's just a single-layer loop,
I is also feasible. But just imagine, if you have been accustomed to using I as a cyclic subscript,
In the future you may need to put this loop in another loop to execute, that is, loop nesting,
Then the inner and outer layers of the loop with I for subscript is definitely not. If the compiler reminds you that the variable
I repeat the definition, that is lucky, if the compiler is silent, and you forgot to change,
Uh, did you hear the sound of the insects flying? Because the code is often modified, expanded, or copied to other programs,
So many experienced programmers simply don't use names like I do. If the loop is not just a few lines,
It is easy for code readers to forget the meaning of I, so it's best to give a more meaningful name to the loop subscript.
One of the common causes of the cycle to become longer is the nested use of loops. If you use multiple nested loops, you should give the loop variable a longer name to improve readability:
for (int teamindex = 0; Teamindex < teamcount; teamindex++)
{
For
(int eventindex = 0; Eventindex < Eventcount[teamindex]; eventindex++)
{
Score[teamindex][eventindex] = 0;
}
}
It is prudent to name the cyclic subscript variable to avoid the common subscript string (index Cross-talk) Problem:
I was written when I was trying to use J, but I wrote J when I wanted to. It also makes data access clearer:
Score[teamindex][eventindex] More information than score[i][j].
If you must use I, J, K, then do not use them for any occasion other than the circular subscript of a simple cycle-
-This tradition has been too popular, and if it goes against that principle, the use of these variables for other purposes can be misleading.
The simplest way to avoid such a problem is to come up with a name that is more descriptive than I, J, K, or more.
The optimal length of the variable name should be between X and Maximumnumberofpointsinmodernolympics
Between. Too short a name does not convey enough information. The problem with names like X1 and X2 is that
Even if you know what x means, you can't tell the relationship between X1 and X2. Too long a name is hard to write, and it also makes the program view
The structure of the sensation becomes blurred. When the study found that the average length of a variable name was 10 to 16 characters,
The effort required to debug the program is minimal. The average name of a program with a length of 8 to 20 characters is also almost as easy to debug. This principle does not mean that
You should try to control the length of the variable name from 9 to 15 or 10 to 16 characters. It's emphasized that if you look at
When you write your own code and find a lot of shorter names, you need to check carefully to make sure the names are clear enough.
Code Daquan Review 2nd