Optimization – Factors to Consider When Choosing Names for Identifiers

cmemoryoptimization

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?

Best Answer

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.