Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question
2  
Check out Microsoft's Guidelines for Names for rules that every .NET developer should follow to be consistent with the platform. I'm not sure though, what you mean by two variable intend to have same space occupied in memory Could you edit your questions to elaborate on that? Also, you might want to do something about your 0% accept rate. Some people are less inclined to provide an answer when you never accept any. –  PersonalNexus Feb 12 '12 at 11:23
    
edited please check –  Afnan Feb 12 '12 at 11:27
3  
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. –  delnan Feb 12 '12 at 12:51
3  
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. –  Eric Lippert Feb 13 '12 at 23:04

4 Answers 4

up vote 3 down vote accepted

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.

share|improve this answer
    
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). –  PersonalNexus Feb 12 '12 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. –  Emmad Kareem Feb 12 '12 at 11:48

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!

share|improve this answer
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. –  PersonalNexus Feb 12 '12 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) –  alfasin Feb 12 '12 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 '12 at 19:28
    
yes there are different opinions about this as with most other topics. it all depends on the situation etc. –  eznme Feb 12 '12 at 20:07

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.

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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