Thursday, March 27, 2014

Another code smell

I came across a little fragment of code with an all-too-familiar pattern:
if ( $complicated->test("one") && $complicated->test("two") && $complicated->test("N") || 1) {
  // complicated nested block of code
}

I have seen le pet in code!

This little smell occurs whenever someone believes they shouldn't remove a bit of dead code.  Perhaps it serves as some sort of misplaced placeholder. Or they didn't write the line and so feel some sort of weird obligation to leave it alone after they've already defaced it. Maybe they don't feel "ownership" of the code. Or they just forgot to clean up a stray bit of crap they were slinging during debugging.

In this case, the programmer had left a comment - something to the effect that the rest of the conditional was left in because we might need it later. Unfortunately, there was no explanation of tests one to N or why we might need a dead and forgotten line of code.

Many real-world gas passing events arise out of overconsumption of low-quality Franken-food, or poor adaptation to real foods that contain fiber. Code smells like this can be due to something analogous: coding without adequate version control,  and attempting to tolerate uncertainty instead of resolving it.

Do you really need to hang on to that bit of cruft? Even if the code is that important, once it is in a source repository history, you should delete it. You can always find it again on the off-chance that you really did need to refer to it. More likely you'll forget about it, and if you ever do need to implement whatever the heck it was trying to do you'll find you didn't need it anyway.
Post a Comment