If I had a nickel for every time this has happened to me… I’d have quite a few nickels.
There are clear advantages to writing code out in the simplest and longest form. Not only is it easier to understand by programmers less talented than you (after all, you are super smart, and most people around you are super dumb), it can save time later when someone is trying to find that code. At least, in this example.
Consider this logging snippet:
if (some_condition)
{
log ("Some condition is ACTIVE.");
}
else
{
log ("Some condition is INACTIVE.");
}
But, you are super smart, so you write this in a more clever way. Perhaps something like this:
log ("Some condition is %s.", some_condition ? "ACTIVE" : "INACTIVE");
…or, heck, because you are super SUPER smart, maybe you save two bytes by doing it like this:
log ("Some condition is %sACTIVE.", some_condition ? "" : "IN");
I like that last one. That’s super SUPER smart.
And, in the case of embedded programming, perhaps saving a few bytes makes the difference between code compiling, or not. I have now faced multiple projects at two different companies where I had to do tricks like that just to get new code to fit in limited program space of the hardware I was using.
At my day job I was trying to figure out what was happening leading up to an entry I saw in the system log of the program. Lets pretend it was the entry in my example:
Some condition is INACTIVE.
My first instinct was to search for that phrases in the source code and see where it gets logged. If the code had been written “dumb”, I would have found it, and been working on it.
But it was not found.
Hmmm, well, let me search just for “INACTIVE”.
Had this been done the second way, I would have found it, and been working on it.
But it was not found.
Okay, let me try a third search for “Some condition is”… And I found it.
My point is, dumb code would have saved me time. And, sadly, *I* wrote this code some years ago and was doing it a “smarter” way. No big deal, just an extra minute … but I do wonder how many “extra minutes” we waste every year due to smarter code. I am pretty sure the Clean Code movement focuses on this type of stuff.
And don’t get me started on how I like to do “nicely formatted” output in source code. In C you can concat longer strings. Sppose you have some long 80 column text string like this:
char *string = "This is a really long line that I need to print in my program.";
That line could be so long it wraps around on an 80 column printout or screen. Many coding standards ask you to keep lines within 80 columns. C allows you to contact constant strings like this:
char *string = "This is a really long line that "
"I need to print in my program.";
I have seen this at most companies I have worked for (which is actually how I became aware of this). BUT, if you were trying to search for something where the split it, like “that I need”, you won’t find it. I have certainly ran into that many times over the years ;-)
No point. No complain. No suggestions. Just pointing out that, sometimes, smarter code wastes time later.
Especially when folks not as super smart as you try to work with it ;-)
And, a side effect of the smart code is that I wasted even more time blogging about it.
Until next time…
