Private members of a class...

wyrd

Well-known member
Joined
Aug 23, 2002
Messages
1,408
Location
California
Ive been looking to perhaps change my style in programming just a tad. However, I cant remember which language an underscore at the beginning of a variable was considered bad (ie; _somevar). Was it C++, Java or .NET? I cant remember. :(

In any case, Ive 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, wasnt 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, its 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, its 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. Its 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, Im not sure that Im 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. Ill 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 wont work in VB since its case-insensitive (cant 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 wouldnt worry a WHOLE lot, wyrd, as long as youre consistent. Chances are that when you go to work somewhere, theyll already have a naming convention and youll 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 youre used to).

-Nerseus
 
Be consistent. Thats 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