Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How long is too long for a variable name?

I suppose, I'm looking for both answers that are both technical and opinionated in nature.

How long is too long for a variable name?

I always try to make my variables as short as they can be while maintaining the proper level of meaning. 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?

For instance, I just used MBReadPerSecondAverageAfterLastFlushToLog, which is as close to supercalifragilisticexpialidocious as I hope I ever come.

Thanks,

Matt

p.s. Sorry if you now have a Mary Poppins song in your head.

like image 569
mbrownnyc Avatar asked Aug 12 '11 17:08

mbrownnyc


1 Answers

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.

No, the length of a variable name has absolutely nothing to do with performance.

Edit:

For some reason I thought you were talking about C++. 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.

like image 158
Seth Carnegie Avatar answered Nov 08 '22 16:11

Seth Carnegie