views:

289

answers:

7

Declaring a variable name, how much (if at all) impact does the length of its name have to the total memory of the application? Is there a maximum length anyway? Or are we free to elaborate on our variables (and instances) as much as we want?

+4  A: 

It has no impact in a compiled language.

Mitch Wheat
A: 

In compiled languages, almost certainly not; everything becomes a symbol in a symbol table. In interpreted languages, the answer is also no, with a few extremely rare exceptions (in certain older versions of Python there would be a difference, for example).

John Feminella
+6  A: 

It depends on the language, actually.

If you're using C++ or C, it has no impact.

If you're using an interpreted language, you're passing the source code around, so it can have a dramatic impact.

If you're using a compiled language that compiles to an intermediate language, such as Java or any of the .NET languages, then typically the variable names, class names, method names, etc are all part of the IL. Having longer method names will have an impact. However, if you later run through an obfuscator, this goes away, since the obfuscator will rename everything to (typically) very short names. This is why obfuscation often gives performance impacts.

However, I will strongly suggest using long, descriptive variable/method/class names. This makes your code understandable, maintainable, and readable - in the long run, that far outweighs any slight perf. benefit.

Reed Copsey
+1 for considering IL
Daniel LeCheminant
IL is way too important to ignore. Java, C#, VB.Net are the ones you think of, but even python isn't interpreted, it has it's own form of IL...
Reed Copsey
The name may be part of the IL, but in .NET at least, the IL is JIT'ed into machine language, so again, no variable names.
John Saunders
Yes, no variable names when it's finally run, but at runtime, the JIT deals with the long variable names. This increases the memory space required, as well as the processing time to build the variable name mappings internally. +the IL is preserved, and part of the application's runtime memory usage.
Reed Copsey
A: 

MSVC++ truncates variable names to 255 characters. Variable name length has no impact on compiled code size.

Cannonade
A: 

As stated by others, variable names disappear in compiled languages. I believe that local variable names in .Net may be discarded. But generally speaking, even in an interpreted language, the memory consumption of variable names is negligible, especially in light of the advantages of good variable names.

Snarfblam
A: 

Actually in ASP.NET long variable names for controls and master pages do add to the size of the generated HTML. This will add some insignificant extra memory to buffer the output stream, but the effect will be most noticed in the extra few hundred bytes your sending over the network.

tarn
A: 

In Python, the names appear to be collected into a number of simple tables; each name appears exactly once in each code object.The names have no impact on performance.

For statistical purposes, I looked at a 20 line function that was a solution to Project Euler problem 15. This function created a 292-byte code object. It used 7 distinct names in the name table. You'd have to use 41-character variable names to double the size of the byte-code file.

That would be the only impact -- insanely large names might slow down load time for your module.

S.Lott