Relevance of "_"

  • Thread starter Thread starter Vinod I
  • Start date Start date
V

Vinod I

Hi Team,

I just want to know, wat is the relevance /or benefit of using "_" before a
variable.
i.e., if I use a variable as "_MyVariable", wat benefit I am going to
achieve than just using "MyVariable".

Thanks in advance.
 
Vinod said:
i.e., if I use a variable as "_MyVariable", wat benefit I am going to
achieve than just using "MyVariable".

The only benefit to any naming convention is consistency which aids
readability.

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
 
Nothing. You can name variables however you like - no special treatment
from the compiler :)

-vJ
 
dear vinod.
we use "_ " mostly to differentiate properties from variables, we can use
_MyProp for proiperties and variable without _,and regarding benefit this is
naming convention and it can vary according to company standards u work for
.. u can check with your company naming conventions.
 
Thanks for the feedbacks.

There was a differentiation between these types of declarations in VB6/COM.
 
Vinod I said:
Thanks for the feedbacks.

There was a differentiation between these types of declarations in VB6/COM.


A lot of people used to use m_variableName to name their member
variables. This was/is a great way to differentiate between the
class' member variables and temporary local variables.
Nowadays a lot of developers use _variableName for the same reasons as
above but the 'm' was dropped since it's redundant

This also makes the constructor more readable..
this.foo = foo
becomes
_foo = foo
 
Phil C said:
This also makes the constructor more readable..
this.foo = foo
becomes
_foo = foo

Well, that's certainly subjective - I prefer the first form, finding _
very distracting when it's all over the place.
 
Basically this was like using "m_" in C++ for private variables. I use this
in the following way.

public class MyClass {
private string _myString;

public string MyString {
get { return this._myString; }
}

public MyClass (string s) {
this._myString = s;
}
}

I use it as a nice way to link my properties to the variables in the class
with out exposing the variable.
 
Jon said:
Well, that's certainly subjective - I prefer the first form, finding _
very distracting when it's all over the place.

Full ACK. If the language already has means to express "instance" access
(this.), why come up with weird coding conventions anyway. Plus, I hate
inconsistencies in code style between generated code and manually written
code.

Cheers,
 
Normally hidden fields are prefixed and if you need public exposure use
properties.

The problem with the this. notation while giving a clear indication that its
this instance you are referring to it polutes any mathmitical formulas you
may have with keyword spam.

Its easier to sit down with a numbers guy who formualted the formula and
step thru in the debugger with each value without having to explain what
"this" means.

These guys arnt programmers nor do they care.
 
Normally hidden fields are prefixed and if you need public exposure use
properties.

When you say "normally" - do you mean "in your code"? I rarely work
with code which uses prefixes...
The problem with the this. notation while giving a clear indication that its
this instance you are referring to it polutes any mathmitical formulas you
may have with keyword spam.

True - I only use it in the rare cases when I have parameters with the
same name. If you keep your methods short enough (which is a good idea
anyway) it's really not a problem, IMO.
 
Back
Top