How long can variable names be in c




















However, as some "sub-routines" increase in complexity, I'm finding the need to create longer variable names to continue to have them be meaningful. Does the length of a variable have an impact on performance? You are correct in naming variables as short as you can while retaining enough meaning to be able to describe what the variable does by just looking at its name.

If you are or C or Delphi or another compiled language the above is correct barring debug information which won't appear in a release executable. For dynamic languages such as Lua or Python or Ruby, the length of a variable name could very well affect runtime performance depending on how variable name lookups are performed. If variable names are hashed and then the hash is used to index a table of values to get the value of the variable, then natrually the more data the hash function has to process, the longer it will take.

That said, do not sacrifice meaningful variable names for short ones just because you think they'll be faster. The speed increase will usually be extremely negligible, and definitely not worth sacrificing the maintainability of your program for.

Make it as long as it takes to make it clear. But, in your example, it looks like you may have some other issues. The name of the Variable indicates to me that you have a function that has more than one responsiblity.

Isn't this what objects are for? I think the main problem here that you forgot that variables are not some sort of global unique identifiers. They have a context, which helps explaining them. By reading them you normally know that context: namespace, class name, method name, the meaning of the previous lines, method name by giving value to the variable or in your example some basic scientific knowledge like MBps is a unit of data transfer speed, speed is always measured by average, etc To make it easier to understand if you want a beer than you just say to your girlfrend that Give me a beer please!

This might be premature optimization. First you write the code, after that should you look for bottlenecks These might be ReadSpeadAfterFlushToLog if the first 2 variables are about the same thing and you should have a DataTransferSpeed class, which does the unit conversion for you when needed.

You must also, of course, be able to assure that future maintainers of your program will also be able to use this name length. There is a dilemma in defining the name of a variable. You can make it really clear what the variable means by using a sentence about it. However, although this may improve the readability of the name, it can actually harm the readability of the code. When using long names, the code rapidly tramps across towards the right hand margin, causing cramps and wraps:.

There is, somewhere between the meaningless abbreviation and the sentence-name, a balance where the meaning of the name is clear, yet it is easy and not unwieldy to use.

Careful thought, functional prefixes see 5. Thus the example above could be shortened to:. As an approximation, there are breakpoints in legibility about every 5 characters.

It is seldom easy to devise names less than 5 characters which have any real meaning. Active 7 years, 6 months ago. Viewed 5k times. Add a comment. Active Oldest Votes. Alin m. Alin Spehro Pefhany Spehro Pefhany k 12 12 gold badges silver badges bronze badges. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. A long can be used to contain an extended range of values.

It is not guaranteed that a short uses less memory than an int , nor is it guaranteed that a long takes up more memory than an int. Typically a short is 2 bytes, an int is 4 bytes, and a long either 4 or 8 bytes. Modern C compilers also provide long long which is typically an 8 byte integer.

In all of the types described above, one bit is used to indicate the sign positive or negative of a value. If you decide that a variable will never hold a negative value, you may use the unsigned modifier to use that one bit for storing other data, effectively doubling the range of values while mandating that those values be positive. The unsigned specifier also may be used without a trailing int , in which case the size defaults to that of an int. There is also a signed modifier which is the opposite, but it is not necessary, except for certain uses of char , and seldom used since all types except char are signed by default.

The long modifier can also be used with double to create a long double type. This floating-point type may but is not required to have greater precision than the double type. When the const qualifier is used, the declared variable must be initialized at declaration. It is then not allowed to be changed. While the idea of a variable that never changes may not seem useful, there are good reasons to use const.

For one thing, many compilers can perform some small optimizations on data when it knows that data will never change. Note that a Standard conforming compiler must issue a warning if an attempt is made to change a const variable - but after doing so the compiler is free to ignore the const qualifier.

When you write C programs, you may be tempted to write code that will depend on certain numbers. For example, you may be writing a program for a grocery store. This complex program has thousands upon thousands of lines of code. The programmer decides to represent the cost of a can of corn, currently 99 cents, as a literal throughout the code.

Now, assume the cost of a can of corn changes to 89 cents. The programmer must now go in and manually change each entry of 99 cents to While this is not that big of a problem, considering the "global find-replace" function of many text editors, consider another problem: the cost of a can of green beans is also initially 99 cents. To reliably change the price, you have to look at every occurrence of the number C possesses certain functionality to avoid this.

This functionality is approximately equivalent, though one method can be useful in one circumstance, over another. The const keyword helps eradicate magic numbers. By declaring a variable const corn at the beginning of a block, a programmer can simply change that const and not have to worry about setting the value elsewhere. There is also another method for avoiding magic numbers.

It is much more flexible than const , and also much more problematic in many ways. It also involves the preprocessor, as opposed to the compiler. When you write programs, you can create what is known as a macro , so when the computer is reading your code, it will replace all instances of a word with the specified expression. For some purposes, define can be harmfully used, and it is usually preferable to use const if define is unnecessary.

It is possible, for instance, to define , say, a macro DOG as the number 3, but if you try to print the macro, thinking that DOG represents a string that you can show on the screen, the program will have an error. It disregards the structure of your program, replacing the text everywhere in effect, disregarding scope , which could be advantageous in some circumstances, but can be the source of problematic bugs. You will see further instances of the define directive later in the text.

It is good convention to write define d words in all capitals, so a programmer will know that this is not a variable that you have declared but a define d macro. It is not necessary to end a preprocessor directive such as define with a semicolon; in fact, some compilers may warn you about unnecessary tokens in your code if you do.

In the Basic Concepts section, the concept of scope was introduced. It is important to revisit the distinction between local types and global types, and how to declare variables of each. To declare a local variable, you place the declaration at the beginning i. To declare a global variable, declare the variable outside of any block. If a variable is global, it can be read, and written, from anywhere in your program.

Global variables are not considered good programming practice, and should be avoided whenever possible.



0コメント

  • 1000 / 1000