This one is a personal thing for me, as ANYONE who has spent any time programming with me at all knows. I have almost walked away from a job because i feel so strong about it… so what is this thing that I hate so much: why nothing else but the lowly _.
Yes the underscore “_” is the scourge of my programming life. I know unless I am in charge of the project and impose it by fiat, you will all never go along with it, but If I had my way, the underscore would be almost completely eliminated from our source. Now the obvious question is “Why”. Take a moment and type _the _quick _red _fox _jumped _over _the _lazy _brown _dog. So now, which fingers on your hand are the most tired. Stop a second, rest your fingers on the home keys; now reachout and set your fingers in the position to necessary to type the “_”. Notice anything? you have to remove your fingers from the home row, and which fingers are you using? Yes the outermost fingers on your hands The Weakest Fingers On Your Hands. In fact you will find in most C++ source code prior to camelCase that the fingers that get used the most by a large margin turn out to be the Weakest Fingers on your hands, and you wonder why your hands feel so tired after a day on the keyboard? You have to remove you other fingers off the home row, slowing down all your other typing.
Doesn’t it make sense to you that if you are going to send your whole day typing, that perhaps it makes sense to limit the usage of the fingers that will feel sore first? Now of course the question you have is “well were would you use underscores” and the answer is, only in method names that capture events “MyButton_Click”; that’s it! everywhere else they are strongly discouraged when I control the project (oh let’s be honest I don’t really make it against the rules for everyone else, but I’ll refuse to personally use them if I can control events). I can here you screaming “… that’s stupid, what do you do with member variables then?”. I use a m. for example
1: public class MyFingerSaver{ 2: private int mHowMany = 10;
3: public int HowMany{ 4: get { return mHowMany; } 5: set { mHowMany = value; } 6: }
7: }
Oh yuck! the “m”? And why is the “m” any worse to look at then the “_”? It works with camelCase, and at least it stands for something “Member variable”. The other place people like to use the underscore is in Constant names: THIS_IS_MY_CONSTANT, BIG_LONG_CONSTANT_THAT_IS_HARD_TO_TYPE_AND_READ. And what is my solution to this one? Actually I’m really quite proud of this one, I first thought up as a semi-joke as a group of programmers I was working with were trying to hammer out some coding conventions (this was the first place where I dug in my heels and fought the underscore) back in 1998. Since that time I have actually come across completely unrelated teams who are using this convention, I don’t know if I started it or great minds think alike but I like to take credit for it in my mind.
the way that I recommend notation of constants is as follows: What is the mathematical symbol for constant? the lower case k. And that was my idea. just as we use the “m” for member variable we use the “k” to mean constant, and the rest becomes regular old TitleCase. Thus my constants become
kThisIsMyConstant
kBigLongConstantThatIsHardToTypeAndRead
Please feel free to use these new ideas, and help to stamp out the evil “_”.