Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why shouldn't I prefix my fields? [closed]

I've never been a fan of Hungarian notation, I've always found it pretty useless unless you're doing some really low level programming, but in every C++ project I've worked on some kind of Hungarian notation policy was enforced, and with it the use of some 'not-really-Hungarian' prefixes as m_ for fields, s_ for statics, g_ for globals and so on.

Soon I realized how much useless it was in C# and gradually started to drop all of my old habits... but the 'm_' thing. I still use the m_ prefix on private fields because I really find it very useful to being able to distinguish between parameters, locals and fields.

The naming conventions for fields page at MSDN says I shouldn't, but it does not say why (the way e.g. Google's conventions generally tend to rationalize their prescriptions).

Are there reasons why I shouldn't or is it only a matter of style. If it is the latter, are prefixes generally considered a bad style and can I expect negative reactions from other people working on the codebase?

like image 887
Trap Avatar asked Mar 18 '09 18:03

Trap


2 Answers

I like the underbar prefix for member fields. Mostly I like it because that way, all of my member fields are shown alphabetically before my methods in the wizard bar at the top of the screen.

WizardBar

like image 68
Matt Brunell Avatar answered Sep 22 '22 21:09

Matt Brunell


When you should:

  • When your project coding guidelines say you should

When you shouldn't:

  • When your project coding guidelines say you shouldn't

If you don't have any guidelines yet, you're free to choose whatever you or your team want and feel most comfortable with. Personally when coding C++ I tend to use m_ for members, it does help. When coding in other languages, particularly those without true classes (like Javascript, Lua) I don't.

In short I don't believe there is a "right" and a "wrong" way.

like image 29
MattJ Avatar answered Sep 20 '22 21:09

MattJ