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

前端 未结 7 1305
灰色年华
灰色年华 2021-02-20 09:05

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

7条回答
  •  北恋
    北恋 (楼主)
    2021-02-20 09:22

    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.

提交回复
热议问题