3

What factors do I need to consider when choosing names for identifiers such as variables? I am concerned about space issues, i.e. extra memory consumption when choosing longer names.

As an example, take these two variables:

bool noExp = true;
bool willNotExpireEver = true;

Each one will take up memory the size of bool. But what about the variable names? They are after all characters that have to be stored somewhere. Where does the space for them get allocated? Am I wasting pace by choosing longer names?

3
  • edited please check
    – Afnan
    Feb 12, 2012 at 11:27
  • 4
    It boggles my mind how one could get the idea that identifiers have an impact on runtime (it only takes a basic introduction to the concept of compilation and to machine code, possibly with some thinking, to rule it out) and moreover care about it.
    – user7043
    Feb 12, 2012 at 12:51
  • 4
    You have a minimum of two billion bytes of user-addressable virtual address space; are you really concerned about the memory impact of the difference of a single variable? Your application is consuming megabytes of virtual address space; if you need to trim that down then you're going to have to do it megabytes at a time, not two or three bytes at a time. Measure your memory usage and optimize to fix the largest offender. What you're suggesting is the equivalent of losing weight by shortening a couple of hairs on your head. Feb 13, 2012 at 23:04

4 Answers 4

4

Is there appropriate way to declare variables name to avoid memory or space issues.

Variable names are mostly for humans. The C# compiler does not care about your name as long as it obeys language rules. So, to answer your question, there are no memory or space issues that could result just from the name. However, there are problems that could result from using the same name with different scoping but this is not what you are asking about.

bool noExp = true; bool willNotExpireEver = true;

two variable should occupy same space in memory that is size of bool rite?

No. In general each variable occupies different memory location for a period of time in the program's execution life time. This duration depends on where it is declared and when it was disposed of.

now where do variable names go? I mean they are somehow characters after all, where do they get space?

The role of the compiler is to take your friendly English like names and convert them to addresses to be used by the binary code. That is why the English name does not really matter to the compiler.

so is it better to name variables short or it does not matter?

As suggested by other answers, there are some common conversions on how to name variables. This is a matter of taste, and due to some lessons learned by others. In general, don't include the variable type as part of the name.

2
  • I think with two variable should occupy same space in memory the OP does not mean that they point to same memory location, but instead they require the same amount of memory each (as opposed to the names that require different amounts of memory to store depending on their length). Feb 12, 2012 at 11:45
  • 1
    @PersonalNexus, thanks for this clarification. In this case, given the OP's example, I would say they would occupy the same amount of memory indeed.
    – NoChance
    Feb 12, 2012 at 11:48
1

First, to answer the obvious sub-question:

"so is it better to name variables short..."

Short answer: No.

Long answer: Variable-names are for the programmer's amusement only. The linker replaces references to variables with memory-addresses and there is no run-time overhead at all. Also, it is bad practice to use variable-names that dont mean anything.

The title of your question "best way to name variables" is very broad and there are good names and bad names. In general it is a good idea to use something descriptive (what is it) and distinctive (how is it different than the other variables), more specifically there are certain best-practices like describing the type of the variable as a short prefix like in hungarian notation, like intTotal and strUsername.

I hope this helps!

4
  • 1
    Agreed, that variable names should mean something to the programmer. However, they are not entirely lost when compiling code, atleast in C#. They do end up in an assembly's metadata (occupying a negligible amount of space) and considering things like named arguments names of identifiers are actually very relevant and form part of your code's public interface. Feb 12, 2012 at 11:33
  • I agree with @eznme - but my reasoning is that the space that the variable name is taking has no meaning today (it was meaningful back in the 70's though)
    – Nir Alfasi
    Feb 12, 2012 at 11:34
  • 1
    The merits of Hungarian notation are highly disputed, especially when we're talking about Systems Hungarian (where the prefix merely states the already-obvious type rather than giving extra clues to the variable's meaning)
    – tdammers
    Feb 12, 2012 at 19:28
  • yes there are different opinions about this as with most other topics. it all depends on the situation etc. Feb 12, 2012 at 20:07
0

During runtime both variables occupy the same amount of memory. So here's no reason for optimization. But code is not for the one time shot, it's also for the maintenance of the project. It for different people trying to understand the code and the semantics after maybe a long time. So it's important that the meaning of the variable (or any other identifier) is in the given context as helpful as possible.

That doesn't mean that all variable names should be as long as possible. It depends of their scope and the transparency of the surrounding code. But when giving a class, method, constant or variable a name always keep in mind that anyone else may sometimes have the task to understand your code and your intention. This way you easily know when to just use a variable like i in a for-loop or MyUserfulEntityProxy for a class.

0

This question has been answered before on stackoverflow.

To quote from there:

No it doesn't. Compiler actually does not save original variable names, you can look at the IL code of any compiled assembly with disassembler.

So to cut it short, no there are no factors that support preferring shorter identifier names.

However, there are some side effects that should be considered when optimizing for performance: Don't compromise on maintainability!

When optimizing for performance, benefits that might be gained by optimization should always be valued against the disadvantages it brings you. Suppose variable name length would have an effect, it is often wiser to choose longer names to further readability and improve maintainability through this. But longer is not always better either. Openly discuss the choice of variable names through code reviews.

Also, variable names should is not something that is going to be there eternally. Be bold and refactor if the name does not fit the variable content anymore.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.