When reading a programming book, it can be tempting to skim the code examples. But they likely have some nuggets that are worth learning. The problem is that we don't like to read code written by other people - or at least it doesn't come naturally to us. What we need is a technique for rapid understanding of new code.
I propose reading the code aloud
. Now, to make sure we don't get distracted by syntactic details, we can probably come up with some rules for what to omit. Consider this example from Robert Martin's Agile Software Development
class Ray : public LinearObject
Ray(const Point& p1, const Point& p2);
virtual bool IsOn(const Point&) const;
I would read this aloud as follows:
class Ray, extends LinearObject
Ray p1, p2
IsOn Point, returns bool
So we have a first cut at a rule of thumb for things to skip:
- start and end matter (e.g. ifndef, define, endif)
- access modifiers (public, protected, private)
- parameter types
We also see a couple of transliterations
- in C++, read ":" as "extends"
- in function declarations, read "[type] ..." as "... returns [type]"
A more enterprising reader of code could expand or revise these rules and we could have, say, 10 tips for reading code aloud, for maximum comprehension.