What kind of prefix do you use for member variable

2019-01-08 13:29发布

No doubt, it's essential for understanding code to give member variables a prefix so that they can easily be distinguished from "normal" variables.

But what kind of prefix do you use?

I have been working on projects where we used m_ as prefix, on other projects we used an underscore only (which I personally don't like, because an underscore only is not demonstrative enough).

On another project we used a long prefix form, that also included the variable type. mul_ for example is the prefix of a member variable of type unsigned long.

Now let me know what kind of prefix you use (and please give a reason for it).

EDIT: Most of you seem to code without special prefixes for member variables! Does this depend on the language? From my experience, C++ code tends to use an underscore or m_ as a prefix for member variables. What about other languages?

30条回答
趁早两清
2楼-- · 2019-01-08 14:04

_ instead of this.

I use _ too instead of this. because is just shorter (4 characters less) and it's a good indicator of member variables. Besides, using this prefix you can avoid naming conflicts. Example:

public class Person {
  private String _name;

  public Person(String name) {
    _name = name;
  }
}

Compare it with this:

public class Person {
  private String name;

  public Person(String name) {
    this.name = name;
  }
}

I find the first example shorter and more clear.

查看更多
够拽才男人
3楼-- · 2019-01-08 14:05

Your mul_ example is heading towards Charles Simonyi's Apps Hungarian notation.

I prefer keeping things simple and that's why I like using m_ as the prefix.

Doing this makes it much easier to see where you have to go to see the original declaration.

查看更多
Explosion°爆炸
4楼-- · 2019-01-08 14:08

I use camel case and underscore like many here. I use the underscore because I work with C# and I've gotten used to avoiding the 'this' keyword in my constructors. I camel case method-scoped variants so the underscore reminds me what scope I'm working with at the time. Otherwise I don't think it matters as long as you're not trying to add unnecessary information that is already evident in code.

查看更多
爷、活的狠高调
5楼-- · 2019-01-08 14:09

I'm weirdo and I prefix member variables with initials from the class name (which is camel-cased).

TGpHttpRequest = class(TOmniWorker)
strict private
  hrHttpClient  : THttpCli;
  hrPageContents: string;
  hrPassword    : string;
  hrPostData    : string;

Most of the Delphi people just use F.

TGpHttpRequest = class(TOmniWorker)
strict private
  FHttpClient  : THttpCli;
  FPageContents: string;
  FPassword    : string;
  FPostData    : string;
查看更多
何必那么认真
6楼-- · 2019-01-08 14:09

For my own projects I use _ as a postfix (as Martin York noted above, _ as a prefix is reserver by the C/C++ standard for compiler implementations) and i when working on Symbian projects.

查看更多
闹够了就滚
7楼-- · 2019-01-08 14:10

I've used to use m_ perfix in C++ but in C# I prefer just using camel case for the field and pascal case for its property.

private int fooBar;
public int FooBar
{
  get { return fooBar; }
  set { fooBar = value; }
}
查看更多
登录 后发表回答