With many years in the software development, we all have some fire side stories to share. These range from deleted files, chasing elusive bugs, that all night effort to meet a deadline. In the following series, I recollect some of these tales. The present article tells the story of how beautifying the code introduced a bug into a program!
I was helping my colleague debug a highly complicated algorithm. He told me that the algorithm had been working well in the morning and by afternoon, after he had beautified the code it stopped working! This seemed bizzare! Can code beautification and formatting introduce a bug? It semed very unlikely. I was sure that my friend had altered the code accidentally while beautifying!
I spent many hours tracing the algorithm and it seemed to be correct. Then I suspected the seed values to that algorithm. Long story short - I have extracted the problem into the following piece of code. Here is the program and its output. In the program you see the plain array of integer values. You also see another array of the same integer values - only this time - the numbers have been properly aligned one below the other. Notice the output - how come the values in the beautified array print different values!
ZERO padding the integers caused them to be interpreted as Octal constants! The value 011 in octal is 9 in decimal. Mystery solved!