Private members of a class...

wyrd

Senior Contributor
Joined
Aug 23, 2002
Messages
1,405
Location
California
I've been looking to perhaps change my style in programming just a tad. However, I can't remember which language an underscore at the beginning of a variable was considered bad (ie; _somevar). Was it C++, Java or .NET? I can't remember. :(

In any case, I've been thinking about using _somevar rather then say, somevar (which I usually use, heh). So, my code would look something like this..

C#:
public class Hi {
   private string _s;
   private int _i;

   public Hi(string s, int i) {
      _s = s;
      _i = i;
   }
}

I know back in the days (heh, wasn't that long ago I suppose), it was something like m_somevar, however being anal as I am, I hate the extra typing (yeah, I know, it's just one char for crying out loud), and not to mention I just hate the way the m_ looks, and in my opinion just a single _ sticks out more.

Anyway.. suggestions, thoughts, inputs? And if anyone knows which language the _ is considered bad in please tell me, it's going to bother me until I remember, heh.
 
Well, in c++ a variable must begin with a letter, so an underscore is out of the question. I have no idea about Java and I was really suprised you could head a variable with an underscore in c#.

In any case, whatever works for you. It's your code.
 
Funny thing is I find that m_var sticks out more than _var, that being said, I started using the former. To be honest, I'm not sure that I'm comfortable with it.

btw, I do the same in C# as in VB.
 
I always use _memberVariable; as much as I dislike using
underscores in variables, it works.
 
I often (when using C#) differentiate between private member variables and the public properties that reflect them by the casing of the first letter. I'll use camel case for the private member variable.
 
I do the same as divil. So:
C#:
private string firstName;
private string lastName;
private int age;

public string FirstName { get { return this.firstName; } }
public string LastName { get { return this.lastName; } }
public string Age { get { return this.age; } }

This won't work in VB since it's case-insensitive (can't have a private firstName and a public FirstName).

We also dropped all hungarian notation as the variable name is good enough about 90% of the time and the other 10% can be figured out at a glance.

I despise the leading underscore, though I think it was relatively valid in C (not C++ or any other language for that matter). They even use two underscores for some reason though I could *never* quite see which vars had one and which had two underscores in the old IDEs. I worked with a guy many years ago in VB3 who used 1, 2 and 3 underscores at the end of variables to designate scope: 1 for a function, 2 for a form level and 3 for global (remember the keyword Global? :)). He eventually commited suicide.

I wouldn't worry a WHOLE lot, wyrd, as long as you're consistent. Chances are that when you go to work somewhere, they'll already have a naming convention and you'll have to follow it. Or, in some cases, a client will dictate naming conventions (such as on a database, if they use a brand/version different than the one you're used to).

-Nerseus
 
Be consistent. That's all I can say.

Just for the record, in our project:

mThis for class-wide private vars.

pThis for vars that have been passed into the method as a
parameter.

this for all other vars. additionally, controls and database objects get the well known prefixes (tblThis, rowThat, lvwThis, txtThat).
 
I think it is rather ironic that those developers who choose to use any form of notation on variables neglect to use that same notation on member variables. -This- makes little sense to me.
 
Back
Top