Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does how you name a variable have any impact on the memory usage of an application?

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?

like image 730
Kriem Avatar asked Mar 13 '09 00:03

Kriem


People also ask

Do variable names take up memory?

And yes, it uses memory, but it's the compiler's memory, not anything at runtime for your program.

Does variable name length affect performance?

Variable name will have absolutely no influence at runtime, and totally negligible at compile time.

Does variable name matter?

Compiled-to-bytecode languages tend to not have reflection at all, so the variable names don't matter. Some languages may have a limit: though not imposing it as a rule for the source code, internally the variable name may be truncated to say 32 ascii characters.

Does variable name length affect performance Python?

To wrap it up, variable name length can affect a Python program's speed as larger files require longer processing times, as shown in the Unix time benchmark.


1 Answers

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.

like image 93
Reed Copsey Avatar answered Sep 30 '22 13:09

Reed Copsey