views:

184

answers:

8

Lets say i have a variable that contains the number of search engine names in a file, what would you name it?

  • number_of_seach_engine_names
  • search_engine_name_count
  • num_search_engines
  • engines
  • engine_names
  • other name?

The first name describes what the variable contains precisely, but isn't it too long?, any advice for choosing variable names? especially how to shorten a name that is too long or what kind of abbreviations to use?

+2  A: 

It depends on the scope of the variable. A local variable in a short function is usually not worth a 'perfect name', just call it engine_count or something like that. Usually the meaning will be easy to spot, if not a comment might be better than a two-line variable name.

Variables of wider scope – i.e. global variables (if they are really necessary!), member variables – deserve IMHO a name that is almost self documentary. Of course looking up the original declaration is not difficult and most IDE do it automatically, but the identifier of the variable should not be meaningless (i.e. number or count).

Of course, all this depends a lot on your personal coding style and the conventions at your work place.

Alexander Gessler
+10  A: 

How about numEngineNames?

Choosing variable names is more art than science. You want something that doesn't take an epoch to type, but long enough to be expressive. It's a subjective balance.

Ask yourself, if someone were looking at the variable name for the first time, is it reasonably likely that person will understand its purpose?

Shaggy Frog
+5  A: 

A name is too long when there exists a shorter name that equally conveys the purpose of the variable.

I think engineCount would be fine here. The number of engine names is presumably equal to the number of engines.

See JaredPar's post.

Mark Byers
+4  A: 

If it is a local variable in a function, I would probably call it n, or perhaps ne. Most functions only contain two or three variables, so a long name is unnecessary.

anon
I disagree, and it certainly depends on the function, but the variable should still be descriptive enough to convey what it means. If it's just a three line function and n is set to engines.Count on the first line, then the meaning is obvious. But as the function grows, usage of n could be further and further away from how it was initialized, the name has less meaning. In general, I think 1 and 2 character names should be reserved purely for short-duration throwaway variables, such as loop variables and lambda parameters.
Anthony Pegram
@Anthony As the function grows, it should be refactored into shorter functions.
anon
I'm not talking about functions stretching for hundreds of lines or even dozens. But how difficult is it to give the name meaning? Think of the poor sap that's going to have to look at that code somewhere down the line, and consider that the poor sap may be you.
Anthony Pegram
@Anthony It's not difficult to think of along name, but I find code that uses short names for variables (not functions) much easier to read.
anon
+1 I agree with you
M.H
e is enough. :)
hendry
A: 

Depends on the lifetime or intended purposes. However, that includes developmental lifetime. I recently named a vector "stuff" because it was for use in an experiment only.

If a variable is only declared or defined right next to it's use, then I wouldn't bother with an expressive name. If not, I'd go for a med-length name. I can never understand why people would use names, say, above 10 characters. Something like enames would be my choice for your given question.

DeadMG
A: 

Depends on the context, if its is a local variable, as eg

int num = text.scan(SEARCH_ENGINE_NAME).size();

the more explicit the right-hand of the expression the shorter the name I'd pick. The rational is that we are in a limited scope of maybe 4-5 lines and can thus assume that the reader will be able to make the connection between the short name and the right-hand-side expression. If however, it is the field of a class, I'd rather be as verbose as possible.

Adrian
A: 

See similar question

The primary technical imperative is to reduce complexity. Variables should be named to reduce complexity. Sometimes this results in shorter names, sometimes longer names. It usually corresponds to how difficult it is for a maintainer to understand the complexity of the code.

On one end of the spectrums, you have for loop iterators and indexes. These can have names like i or j, because they are just that common and simple. Giving them longer names would only cause more confusion.

If a variable is used frequently but represents something more complex, then you have to give it a clear name so that the user doesn't have to relearn what it means every time they use it.

On the other end of the spectrum are variables that are used very rarely. You still want to reduce confusion here, but giving it a short name is less important, because the penalty for relearning the purpose of the variable is not paid very often.

Ben Gartner
A: 

When thinking about your code, try to look at it from the perspective of someone else. This will help not only with picking names, but with keeping your code readable as a whole.

Having really long variable names will muddle up your code's readability, so you want to avoid those. But on the other end of the spectrum, you want to avoid ultra-short names or acronyms like "n" or "ne." Short, cryptic names like these will cause someone trying to read your code to tear their hair out. Usually one to two letter variables are used for small tasks like being incremented in a for loop, for example.

So what you're left with is a balance between these two extremes. "Num" is a commonly used abbreviation, and any semi-experienced programmer will know what you mean immediately. So something like "numEngines" or "numEngineNames" would work well. In addition to this, you can also put a comment in your code next to the variable the very first time it's used. This will let the reader know exactly what you're doing and helps to avoid any possible confusion.

Sean O'Hollaren