Does the size of a variable name affect its weight?

10

There is a difference in size between this:

string packet1234 = "123";

And that?

string packert1234556 = "123";
    
asked by anonymous 16.02.2015 / 21:56

1 answer

13

No, definitely not. In fact after compiling the code the name of the variable until disappears. A variable is just a well-known design pattern for easy access to a memory address. In compiled languages the name becomes direct addresses. It is a convenience of memory access used in so-called high-level languages. They are used to facilitate understanding of the code by the programmer. That is why their names should be significant.

Only in fully interpreted languages, which are very rare, could make some measurable difference. But this last word is key. Because you could measure it but it would not really make a difference. And this in interpreted languages that do not have any type of optimization. I put this more to the point of curiosity, it does not affect the C # that is compiled in any way.

So in any language I know the size of the variable name will not affect performance or memory consumption significantly and in most cases not even very small. The choice of name should already be made with legibility even if there was some cost for large names.

There is even a memory cost in C # in certain situations where the variable name is not a common local variable, since C # has several metadata. Then a larger name would bring a greater theoretical consumption. But it is such a tiny difference and only in its definition and not in consumption that it would be ridiculous to think that this changes anything. It would be like complaining that a drop of rain fell when you are inside a pool. I put it to give the most correct answer possible but it gives even fear there is the interpretation that there is an extra cost that should be minimally considered. There are so many other factors that affect the program that thinking about it is absurd.

And if it does make any sense, it's better to program in Assembly. It's good that you're a beast at this so you do not make code worse than a good compiler in an optimized language would. I still doubt it would make sense.

    
16.02.2015 / 22:00