Reading Code: Assume it doesn’t work
Join the DZone community and get the full member experience.Join For Free
Jae and I have spent a reasonable chunk of the past few weeks pairing on code that neither of us are familiar with and at times we’ve found it quite difficult to work out exactly what it’s supposed to be doing.
My default stance in this situation is to assume that the code is probably correct and then try and work out how that’s the case.
After I’d vocalised this a few times, Jae pointed out that we couldn’t be sure that the code worked and it didn’t make sense to start with that as an assumption.
He pointed out that we were often going down paths of the code that were executed infrequently and since there were no tests around the code we couldn’t be sure what it did.
A more useful approach was to assume that it doesn’t work and then reason about it from scratch to determine whether or not that assumption is correct.
I quite like making this assumption because it makes you concentrate much more closely when you’re reading the code rather than skimming over it and assuming that it does what it’s supposed to do.
Reading the code has become a mini debugging session where we try to discover how some unfamiliar code actually works.
We’ve found a couple of bits of code that weren’t actually working by following this mindset but it is mentally quite tiring and obviously slower than other styles of reading code!
Published at DZone with permission of Mark Needham, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.