Chapter 72. Refactoring Toward Speed-Reading

Benjamin Muskalla

A casual reader usually reaches 150–200 wpm (words per minute) with a good comprehension rate. People who are into speed-reading can easily reach up to 700 wpm. But don’t worry, we don’t need to set a new world record for speed-reading to learn the basic concepts and apply them to our code. We’ll look at three areas that are particularly helpful when it comes to reading code: skimming, meta guiding, and visual fixation.

So what makes speed-reading that fast? One of the first steps is to suppress subvocalization. Subvocalization? Exactly. That voice in your head that just tried to properly articulate that word. And yes, you’re now aware of that voice. But don’t worry, it will go away soon! Subvocalization can be unlearned and is an essential first step to seriously improve reading speed.

Let’s look at this method with three parameters, which all need validating. One way to read the code is to follow where and how the input parameters are used:

public void printReport(Header header, Body body, Footer footer) {
  checkNotNull(header, "header must not be null");
  validate(body);
  checkNotNull(footer, "footer must not be null");
}

After locating header, we have to find the next parameter, body, which requires us to look down and left. We can start with a simple refactoring to align the first and third check so we only break the horizontal flow once:

public void printReport(Header header, Body body, Footer footer) {
  checkNotNull(header, "header must not be null");
  checkNotNull(footer, "footer must not be null");
  validate(body);
}

Alternatively, given that checking for null is a validation of the parameter as well, we could extract the checkNotNull method calls into their own properly named methods to help guide the reader. Whether these are the same or overloaded version of the method depends on the code at hand:

public void printReport(Header header, Body body, Footer footer) {
  validateReportElement(header);
  validateReportElement(body);
  validateReportElement(footer);
}

Meta guiding is another technique for speed-reading. Instead of trying to read word by word in a book, you try to capture the whole line at once. Children usually do that by using their finger to keep track of the word they’re reading. Using some sort of guidance helps us to keep moving forward and avoid jumping back a word or two. Funny enough, code itself can act as such a device as it has an inherent structure that we can leverage to guide our eye:

List<String> items = new ArrayList<>(zeros);
items.add("one");
items.add("two");
items.add("three");

How many items are in the list? One, two, three! Actually, it’s four. Maybe more. Oops, missed that zeros argument too? The structure that should help us actually gets in our way. While we have allowed our reader to be guided by the alignment of the add methods, we totally misguided the eye and missed the constructor argument. Rewriting this allows the reader to follow the guide easily without missing any important information:

List<String> items = new ArrayList<>();
items.addAll(zeros);
items.add("one");
items.add("two");
items.add("three");

Next time you write a piece of code, see if you can speed-read it. Keep in mind the basics about visual fixation and meta guiding. Try to find a structure that makes logical sense while guiding the eye to see the relevant information. Not only will it help you to read code faster in the future but it also helps keep you in the flow.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset