Stop It With the "m" Prefix
Remnant of the Past
Many open source libraries I look at are still using the “m” prefix for naming variables, likely due to habit or seeing someone else do it. I hold the opinion that Hungarian notation should have died long ago. Modern programming tools are good enough to help you figure out the context of a variable. For those unfamiliar, the “m” prefix is used to denote a variable as a member variable. Hungarian notation goes a lot further than just the “m” prefix, there’s also an “s” prefix for static variables among others.
I’ve always wondered what problem the notation was solving for Java programmers. It’s easy enough to see if a variable is a member or not. Same for static variables. To be fair, I have only been programming for the last six years, so maybe this was a problem seven years ago.
Update: Listen to Jake Wharton people!
It’s pretty easy to show just how redundant the prefix is in a modern IDE. I’m using Android Studio which is based on IntelliJ.
As you can see, any IDE out there will color your member variables.
IntelliJ also has a neat feature called
Quick Definition that can show you the origin of something.
Lastly, there’s the
Declaration feature that will navigate you to where the variable is declared. On my Macbook Pro I can access this shortcut by clicking on the variable and then pressing
Command + B. This will also work on methods and class types! If I become un-lazy then I will make a GIF of this in action. :)
Since I mentioned the “s” prefix earlier, I figured I should show that the IDE italicizes static variables too.
I can see Hungarian notation being useful if you’re programming over SSH in Nano and staring at a black and white screen inside a small terminal window, but how often does that happen?
While appearance is mostly personal preference, there are practical benefits to not using the prefix notation. Most IDE’s will have a code generation tool, such as for creating getters and setters. Having a prefix will generate ugly method names, such as
getmMemberVariable() rather than
getMemberVariable(). Of course, you likely can configure the generator to ignore the prefix, but that takes more work.