Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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..

 

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.

Gamer extraordinaire. Programmer wannabe.
Posted

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.

Can't afford to be neutral on a moving train...
  • Moderators
Posted

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.

Visit...Bassic Software
Posted

jfackler:

Yeah I remember that post. I still hate strVariable and m_variable. :p

Gamer extraordinaire. Programmer wannabe.
  • *Experts*
Posted

I do the same as divil. So:

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

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Posted

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).

.nerd

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...