There’s a school of thought for programming that says you don’t ship until you have everything in order. I mean, you don’t want to look like an idiot, right? There’s another school of thought that says you bang out release 1.0 as fast as possible, get feedback and iterate. Yesterday, I decided to just bang out a blog while it was fresh in my mind. The result? Mistakes, but instant feedback from people who care!
I was trying to give an example of how variable names can seem appropriate at the time but fail for the long haul.
There was some original code that looks similar to the following. It’s not the exactly the following, as I have no control over the copyright; perhaps that wasn’t obvious. Since it’s already written, complaining that it wasn’t done correctly The First Time makes me feel warm and fuzzy, but doesn’t help.
if (4 == some_function()) {
/* talking to old process */
}
...
if (4 == some_function()) {
/* talking to old process */
Not too good. A comment is needed to explain what’s going on. Plus we have that mysterious check all over the place.
#define OLD_PROC_VAL 4
...
old_process = (OLD_PROC_VAL == some_function());
...
if (old_process) {
/* talking to old process */
That’s better. The check is done once and it should be clear the code is talking to the old process. But what will this mean four years from now when every process is old and another flavor of process is introduced? We now have old #1 and old #2? Someone is going to have to dig through the revision control logs to see which process was in use when the original old process check was added.
Oh, and just for fun, let’s say that some_function returns a value that has nothing to do with the version number of the old process.
I propose the version should go into the variable’s name:
#define PRE_RELEASE_15_MAGIC 4
...
pre_release_15 = (PRE_RELEASE_15_MAGIC == some_function());
...
if (pre_release_15) {
/* Talking to process older than release 15 */
Now you can argue whether the final comment is necessary.
So what’s the point? Even the best architecture is still has to be implemented. You’re going to get old code and the only way to understand it is by reading. Make it easy for the others. Leave some breadcrumbs for them to follow.