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 13:58

I prefix member variables with 'm' and parameters (in the function) with 'p'. So code will look like:

class SomeClass {
    private int mCount;
    ...
    private void SomeFunction(string pVarName) {...}
}

I find that this quickly tells you the basic scope of any variable - if no prefix, then it's a local. Also, when reading a function you don't need to think about what's being passed in and what's just a local variable.

查看更多
放荡不羁爱自由
3楼-- · 2019-01-08 13:59

It depends on which framework I'm using! If I'm writing MFC code then I use m_ and Hungarian notation. For other stuff (which tends to be STL/Boost) then I add an underscore suffix to all member variables and I don't bother with Hungarian notation.

MFC Class

class CFoo  
{  
private:  
    int m_nAge;  
    CString m_strAddress;  
public:  
    int GetAge() const { return m_nAge; }  
    void SetAge(int n) { m_nAge = n; }  
    CString GetAddress() const { return m_strAddress;  
    void SetAddress(LPCTSTR lpsz) { m_strAddress = lpsz; }  
};

STL Class

class foo  
{  
private:  
    int age_;  
    std::string address_;  
public:  
    int age() const { return age_; }  
    void age(int a) { age_ = a; }  
    std::string address() const { return address_; }  
    void address(const std::string& str) { address_ = str; }  
};

Now this may seem a bit odd - two different styles - but it works for me, and writing a lot of MFC code that doesn't use the same style as MFC itself just looks ugly.

查看更多
萌系小妹纸
4楼-- · 2019-01-08 13:59

If the language supports the this or Me keyword, then use no prefix and instead use said keyword.

查看更多
甜甜的少女心
5楼-- · 2019-01-08 13:59

A single _ used only as a visual indicator. (C#)

  • helps to group members with intellisense.
  • easier to spot the member variables when reading the code.
  • harder to hide a member variable with a local definition.
查看更多
女痞
6楼-- · 2019-01-08 14:01

It really depends on the language. I'm a C++ guy, and prefixing everything with underscore is a bit tricky. The language reserves stuff that begins with underscore for the implementation in some instances (depending on scope). There's also special treatment for double underscore, or underscore following by a capital letter. So I say just avoid that mess and simply choose some other prefix. 'm' is ok IMO. 'm_' is a bit much, but not terrible either. A matter of taste really.

But watch out for those _leadingUnderscores. You'll be surprised how many compiler and library internals are so named, and there's definitely room for accidents and mixup if you're not extremely careful. Just say no.

查看更多
干净又极端
7楼-- · 2019-01-08 14:01

None if it's not necessary, single underscore otherwise. Applies for python.

查看更多
登录 后发表回答