I have seen too much C# and C++ code where the variable naming convention seems to ask programmers to write variable names using underscore before the text of the variable. e.gr.
int? _countMoney;
What's the rationale supporting that convention?
I have seen too much C# and C++ code where the variable naming convention seems to ask programmers to write variable names using underscore before the text of the variable. e.gr.
int? _countMoney;
What's the rationale supporting that convention?
In C# I usually prefix with _
private fields but never local variables. The rationale behind this is that when I need a private variable I type _
and the Intellisense filters the list and it is easier to find. This way I am also able to distinguish between private from local variables and I no longer need to type this.variablename
for class fields but simply _variablename
.
You should not use _ as a prefix in c++. Names starting with _ are reserved for the compiler.
The most common prefix is c++ is m_ (as in 'member)
For C# its very common to use _.
At my site where we do equal amount of c++ and c# we always use m_ to be consistent
The Microsoft Guidelines for member naming specifies that you do not use prefixes for fields.
Do not use a prefix for field names. For example, do not use g_ or s_ to distinguish static versus non-static fields.
You can read Microsoft's guidelines for names here. This just applies to C#, of course.
Like all other conventions it is about making code easier to understand.
I have seen this as a convention for private fields - in that case, it is very easy to see that a private field is being used.
You can ask the same question about hungarian notation.
Like others have said, that naming convention helps distinguish member variables from locals. This gives two major advantages:
Prevents naming clashes. I can write a constructor like:
SomeObject(int foo, int bar)
{
_foo = foo;
_bar = bar;
}
That way, I don't have to name the arguments new_foo
or something like that.
I'm not really sure what the rationale is behind this convention. Personally, I do not care for the use of any kind of variable name prefix for denoting the scope of a variable, nor do I particularly care for the use of underscores in naming anything. What's so bad about using the "this" keyword and adopting a convention of lower camel-cased names for private instance/member variables?
public void IncrementFoo()
{
this.foo += 1;
}
It's only 5 additional characters to type, but it's very explicit. If you've adopted the lower camel-cased convention for your private instance variables, then this tells you right away that you're accessing a private instance/member variable, and you didn't need to use any kind of prefix to denote it.
Sometimes people do that in member variables to help distinguish them from local variables. If you're directly accessing an underscore variable, maybe you should be using a getter/setter for access instead.
Something I noticed by Java devs in Eclipse when I had to do Java work, they wouldn't underscore there vars. Reason being? The members vars were color coded in the IDE...felt no need to do it so to speak. From habit it came naturally as I found it easy to locate a member var in the VS IDE by way of some visual cue...and underscore was it as it it fairly popular. In the rare event you have to look at code in its rawest form...text...those types of things help out tremendously.
The _
prefix is pretty essential in VB.NET because it's not case-sensitive. We code in both C# and VB.NET, and for the sake of everyone's sanity it's important to have the same naming conventions in both languages.