Performance Zone is brought to you in partnership with:

Mark is a graph advocate and field engineer for Neo Technology, the company behind the Neo4j graph database. As a field engineer, Mark helps customers embrace graph data and Neo4j building sophisticated solutions to challenging data problems. When he's not with customers Mark is a developer on Neo4j and writes his experiences of being a graphista on a popular blog at http://markhneedham.com/blog. He tweets at @markhneedham. Mark is a DZone MVB and is not an employee of DZone and has posted 534 posts at DZone. You can read more from them at their website. View Full User Profile

Reading Code: Assume it doesn’t work

03.01.2013
| 861 views |
  • submit to reddit

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, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)