19 december 2024

An experiment in retention

Consider these seven words: Alligator, Baboon, Chameleon, Dolphin, Eel, Ferret, Gorilla. Now close your eyes, and try to recite them. That probably went well. Now do the same for these: skyscraper, soil, matrix, cherry, carburetor, door, incense. That probably did not go as splendidly, did it? Moreover, if you would try to recite the words again ten minutes from now, chances are that you bombed, especially on the second set.

You have now encountered The Magic Number 7 plus or minus 2. Hold that thought.

What made the second group different from the first, and what does your brain have to do with all of this, apart from the obvious point that it has to do with memory?

While the second set of words was a product of my internal randomizer of words, the first one was far from it. It had internal structure because there’s logic (alphabetical), categorical relevance (animals) and, as a bonus, it could be visually represented (picture a zoo).

Remember The Magic Number plus or minus 2? This refers to a scientific article, written by George A. Miller. He discovered the concept of temporary memory slots. These “registers” are used to create a mental image of a certain task. A person typically has between five and nine of these slots of these readily accessible memory spaces.

Our brain

A slightly outdated but workable model is that our brain has, in broad terms, three main parts. The reptilian brain, the one we crawled out of the ocean with when life began on earth. This part keeps us breathing while we’re reading this blog, while we’re coding, or eating a grilled cheese sandwich. The mammalian brain is the prime seat of our emotions, and what neurobiologists call the 4F: feed, fight, flight and … merge requests. And finally, there’s the cerebral cortex, the newest part of our brain. This is where cognitive thought process meets moral judgement.

What we, software developers, call coding, could be more accurately be described as “recoding”. That is the cognitive process, that deals with translating a concept into concrete code. This is a highly involved activity that heavily relies on the way our brains are wired. I feel that it’s time for an example.

Is it even even?

Assignment: Take a list of random numbers, and return a sorted list of even numbers

This assignment contains multiple steps:

  1. Loop over the list of random numbers
  2. Filter out the even numbers
  3. Sort the new list
  4. Return the list

Already we can see that, even though this is a very simple assignment, things could get complex. Why? Because, as we saw in the beginning, we like to construct a mental image of our task, and we have a maximum of seven plus or minus two elements to work with. Action #2 is the beef of the assignment, and – in the programming language of Java before there was version 8 – consists of the following statement:

 

if (i % 2 == 0) {
    evenNumbers.add(i);
}

If we read this code, then we need to translate the statement between parentheses to actual human language, a concept our mind is uniquely equipped to deal with. So, now we don’t have four steps, but five.

So, what happens when we move even this blatantly simple statement to its own method, and give it a name that linguistically (language) or semantically (meaning) appeals to us:

if (isEven(i)) {
    evenNumbers.add(i);
}

We don’t even have to bother with the code behind isEven. When we look at the main code, we have an image in our mind already on what the method does. The readability has increased and now our complexity is back down to four, and we still have between one and five “registers” left, to help us visualize the rest of the assignment. This way we have neatly divided our task into chunks, that have logical consistency. This is the essence of Clean Code: the offloading of complexity, to let the innocent software developer focus on the problem at hand. By having our working memory engaged, with relevant chunks of information flowing in and out of our registers we experience a much sought after state of flow. If the flow breaks, it takes a lot of effort to get into that state again.

A slight detour into teaching

And if we think that the principle applies largely to coding, I assure you it has other real world applications. For instance, when I was teaching programming languages at the University I had a problem with students regularly coming in quite late to the class. The students continued to do this, until I explained that teaching a class requires dealing with a number of things: the slides, the narrative, the temperature in the class, that student that seems to be dozing off, my laptop sleeping in regular intervals. This all contributed to my visual image of teaching the class. My “registers” were all engaged in maintaining a teaching atmosphere. If somebody comes in late, all registers are wiped, and the building of that visual image begins again, which takes time. When I showed them how the Magical Number worked, the number of late-comers significantly dropped after that.

Who benefits?

While writing code for the first time in ways that reduce complexity is really helpful, the real benefit lies in you, revisiting your code, or the hapless maintainer that touches upon your code two years from now. Raise your hand if you have ever looked at some code, and git blamed it, only to discover it was you who wrote that prattle. You could save yourself the cold shower of embarrassment, and do it right the first time.

Our prefrontal cortex is the part of the brain that practices advanced reasoning, and moral judgement. When thinking of coding, we rarely think about the ethics of our conduct, not only towards society at large (does my work contribute or do harm to mankind), but also to our immediate or future colleagues. Writing in a clean, structured, logical and understandable way tells them that we value them, too. And that benefits the whole team, current and future.

It might even help you understand why you come home from grocery shopping, only to have forgotten some ingredient you needed, and didn’t bother to write down. It won’t help you with remembering, though.

Further reading

It must be assumed that every developer reading this, that is now that you’ve actually come this far, has a copy of Robert C. “Uncle Bob” Martin’s Clean Code on their night stand.

This blog relies heavily on the article The Magical Number Seven, Plus or Minus Two by George A. Miller, cognitive psychologist at Harvard University. That article seems a bit abstract, but a there’s a helpful Wikipedia article.

Another great resource is Working Memory by Alan Baddeley, to provide a deeper insight into the brain’s memory systems, the concept of chunking and temporary storage of information

Kun je de vacature die je zoekt niet vinden?

Maak een Jobalert aan en ontvang een melding per mail
wanneer er nieuwe vacatures zijn!