I apologize for shocking your system by posting more than once a month – there are reasons for that, but I unfortunately can’t get into them right now – but Keith added an interesting comment to my last post. He said:
Side Note: a bit disturbing you’re using C++ naming conventions in C# though? 🙂 No doubting your a ninja coder and I love your stuff, but seriously, bringing the m_ prefixing into C# is a bit of a “cant teach an old dog new tricks” thing.
This is pretty close to a “religious question”, but since it’s my blog, I’m always right (as codified in the “decree on Gunnerson infallibility of 1997”), so I’ll take it on.
When I first started writing code, a lot of our samples were written without any way of indicating whether something was a field or not, and I wrote a considerable amount of code using that idiom. What I found was that when I went back and looked at my code later, I had to scroll around to find out where each variable came from, and that made understanding the code harder.
I toyed for a while with using just and underscore “_name”, but I didn’t like that. A single underline is a bit hard to pick up visually, and it seemed like I was inventing a different expression just to be different. So, I switched back to “m_”, and I must say that I’m happy with the choice. The only place I don’t like it is with events or other public fields, which are then named differently, but I’m willing to deal with that.
The only other place I use prefixes is on pointers, where I just use “p”. Unsafe code is rare enough that I want to have an indicator of what’s going on.
[Update: Another reason to use m_ is to make it easier to find your variable names in intellisense when you’re working with controls, since there are roughly 4000 members in the average control class. I’ve also been using “c_” in the names of controls for the same reason]
So, what do you think, keeping in mind that if you disagree, you’re wrong…