Prefixes are a rather controversial topic. Taking everything into account, I think we should not use prefixed names. Here’s why.
A few days ago I touched the topic of prefixed names for variables and types in a discussion. Today I’ll write a few points about the topic, and why I am mostly against the use of prefixed names.
Why does it matter?
To have easily understandable code, names should be meaningful and readable. We have enough complexity to put up with that it is not worth having names that are harder to read than they have to be. Most people formulate any text they read in their minds, and reading a
PrfxSomething name just adds unnecessary noise to that process.
Prefixes also add an extra burden when we search generated documentation: Many documentation generators sort e.g. classes in alphabetical order. Having an alphabetical index of classes where almost everything is empty except for the letter C foregoes some of the usefulness of these tools.
Some of those tools have features to ignore a set of known prefixes, but then there’s still the human reading through that index: We search for words starting with the first letter, and our brains do not have the feature to skip the prefix.
There are basically two flavors of Hungarian notation: Prefixes that denote the type of a variable, and prefixes that denote some kind of intent or semantics. In modern day languages like C++ where you define hundreds of types in a single program, putting the type in a variable name is not feasible, regardless of whether you use an abbreviated prefix or something else.
I have seen
co as the variable name prefix for anything that is not of built-in type (for “complex object” – even if it was not complex at all). In well-designed programs that would mean that almost every variable should have a prefix like that, which makes it pretty useless. Most people use modern IDEs these days where you just hover the mouse over a variable and the IDE tells you what type it has.
In addition, having the type encoded in a variable name makes maintenance cumbersome: imagine you have to change the type of a variable. It does not make much fun to also have to change its name everywhere, or worse, not changing it and having the variable name lie to you from now on. In C++, we also have type aliases, which makes finding a proper prefix hard or impossible. It’s just not worth the effort.
When it comes to semantic prefixes, there is another problem. Encoding the semantics of a variable in its name means the compiler can not enforce the semantics. It’s usually better to solve this by using the type system, i.e. use strong types instead of weak naming.
There are frameworks that prefix every single class with one or two characters, e.g. Qt where everything is a QSomething. Others use C as a prefix – for “class”. It might be to distinguish the name from other classes, but that’s what namespaces are for. It might be to distinguish classes from enums or structs, but I don’t buy that.
Structs and classes are pretty much the same, and they are all just types. What is important about a type is its interface, which determines how you use it. What language features you used to achieve that interface is less important.
Another convention that some people have taken over from C# and Java is to prefix interface names with an
I. To me, this does not make much sense. If I use a type, I should not have to care whether it is an abstract interface or the real thing. I just use its methods, and I expect them to do what their names say they will do. If on the other hand, I am implementing the interface itself or a class that derives from it, then I hopefully know that it’s an interface class and don’t need the I-prefix to remind me.
If a name really needs that addition
In some cases, it might actually be necessary to add some information to a name. I still don’t think that should be done via a prefix. Those cases should be rather rare, which means the added information will not be very common. Having to know what a prefix for such uncommon cases means us just an unnecessary burden.
In addition, if it really is important to add information to a name, it should be worth a little more than just one or two letters.
Like braces and indentation, prefixes are a styling topic that is mostly a matter of taste. Even if you agree with what I think about prefixed names, you might not want to stray from naming conventions you have been used to for years.
I’d like to hear what you think. Do you like prefixes? What peculiar prefixing conventions have you seen? Please leave a comment!