Look at this code:
(within a while body)
...
if (log.isDebugEnabled()) log.debug("Parsing line " + (lineNumber++));
...
lineNumber used here
...
You might be wondering: what´s wrong with it? Well, it turns out that this sort of construction it could become extremely difficult to spot in certain conditions. Imagine a hypothetical situation where this code is not working properly in the production environment, and meanwhile the developer in charge of this block of code -the guy as cool as a cucumber- is totally sure about the correctness of it. Okay, you got it, you may be thinking of me: “How noob this guy is!”; but Hey!, sometimes you are toiling late in the night and this kind of bugs pass unnoticed until you receive an email from your Development Manager muttering:
Subject: Potential bug
I have come across with this statement –incidentally- while surfing the code. Beware that the log will be printed out only if the log level is set to DEBUG, so the lineNumber variable won´t be incremented.
All the best,
Your Development Manager
Guess which level the Production Support had established in their environment? Fortunately, I started off saying “Imagine a hypothetical situation …“.
Ruben! A public confession was not necessary!
Anyway, we all could benefit from regular code reviews. They are worth the time in the elimination of bugs and also cross-training/mentoring.
Later.
Dean (the non-hypothetical Dev Mgr)