Wednesday, July 12, 2006

Don't ignore warnings...

A very good practice I've picked up from some of the smarter people I've worked with, and also from reading Steve McConnell is not to ignore compiler warnings. Sometimes it's so tempting - you know, "it's compiling, it must be working, who cares about that..."

Well today, after a real dumb mistake on my part, a warning that I didn't ignore, just saved me hours of pain a few days from now.

It was a stupid mistake on my part, but then, the real doosies alway are aren't they.

If had an if /else if/else statement. I was doing some ANDING on a certain status and basically wanted to intialise a message based on the status - 0 = OK, 1 = Partially OK, 2 = Screwed, but then there is other information built into this status, like, "future dates found in result set" and "errors logged" = 8, so , a result of 9 means Partially OK, errors found in the log, or 6 means "No Processing and Future dates found - yes it is possible for this status to be 4, which would mean "all processing occurred but errors were logged" - this is a success, of sorts...anyway I digress...

I had this if/else if/else statement set up to initialise the message about the status,

if (result.ok & result)==result.ok
do stuff
else if ((result.partial & result)==result.partial
do other stuff
else if ((result.stuffed & result)==result.stuffed
do more stuff

I was getting a warning on the first "else if" that said "unreaschable code detected". A fairly shicking warning none-the-less" After a little checking it dawned on me that the value of result.ok was 0! Of course the first condition will always be met!!! Stupid? Yes. This wouldn't have shown up though, until every message from this component came back the same. Worse still, it possibly would never have shown up, because the component would have always returned a positive response - my little SMS message each morning that is derived from this service, would have had me merrily going about my business, oblivious to the fact that things were not as they seemed....so....don't ignore warnings....

A simple restructure of the statement quickly resolved the issue of course, and a quick review of just how "ANDING" works...