Tue 1 Apr 2008
Coding tips: Generational Naming
Posted by Dennis B under Programming
[2] Comments
All too often, you need to make a new version of a function that does something a little bit different than the original function. Perhaps it just takes a new argument or removes an unused one. Maybe it’s a wrapper around the original function that does some more cool stuff. Furthermore, for backwards compatibility, you have to leave the original version of the function around, semantics unchanged.
This is a pretty common programming scenario which can become quite unnerving when you discover that the original function was named perfectly. Of course, you can’t reuse that name because you are programming to a C ABI, or the signature is not different enough to allow you to overload the function.
So, you spend the next half hour of your day figuring out a name for this new function which makes sense. Here is one example, the original function:
int findMatches(string regex, int flags);
And the new function changes the meaning of one of the flags. So you name it:
int findMatchesWithNewFlags(string regex, int flags);
But wait, that’s a pretty stupid name, and rather verbose. So you try to capture what is really different about the new function.
int findMatchesOldReentrantFlagIsReverseFlag(string regex, int flags);
Still pretty verbose, and if we change the function again in the future, this will just get silly. OK, how about this:
int findMatchesWithReverseOption(string regex, int flags);
This is better, but still verbose. There has to be a better way.
Generational Naming Conventions
Well, on this day of April 1, 2008, let me share with you the dearest naming convention to my heart, generational naming. In this paradigm, we recognize that the original name was perfect, so all we want to capture in the name of the new function is that it is newer. You do this by adding a “2″ to the end of the name.
int findMatches2(string regex, int flags);
When you need to again change findMatches(), you create the next generation:
int findMatches3(string regex, int flags);
Extended Naming
There are several less powerful variants of generational naming. For example extended naming, where you add “Ex” to the new function. This is in fact, a common practice at another large software company located in
int findMatchesEx(string regex, int flags);
And then there is “new naming”:
int newFindMatches(string regex, int flags);
Which can actually be extended twice over:
int newerFindMatches(string regex, int flags); int newestFindMatches(string regex, int flags); // buck stops here
Conclusion
The conclusion is simple. You do not need to be smart or clever to write code, you just need to know how to count.
April 1st, 2008 at 12:38 pm
For no good technical reason I prefer the ‘Ex’ naming convention for an ‘enhanced/fixed’ API. It’s purely aesthetic and it devolves into using numbers if you need another round. Hopefully, you give things enough thought on the second go around to not have to do a third.
April 2nd, 2008 at 1:14 pm
What I do is misspelling or subtely change the function name so that when you look at the new function you don’t even notice something changed.
For example findMatches could become findWatches.
For best results impose a font to your team that makes characters hard to tell apart.