Some Hard Data on Code Readability
Analysis of how the brain processes negations gives those pedantic, pragmatic programmers amongst us some hard science to use as backup for some of our minor code changes.
An important tactic when programming is to simplify things to make it easier for the readers of your code. A computer will not complain about the shoddy-ness of your code. The next person who has to update your code, however, will thank you if your code is clean, readable and easy to follow. More often than not, that person is yourself, allowing you to reap what you sow.
Often this means paying attention to the smallest details, details which some programmers see as pedantic bluster. Today I bring you some retort to one of these accusations.
I feel compelled to correct code similar to the following:
The reason for this little modification — a simple renaming of the method called in the if-clause — is to remove a negation. This small modification has brought a number of snide remarks my way over the years. “Can’t you understand a little not-operator?”, I’ll be asked. And, up until now, I’ve had only my gut instinct it feels easier to understand. Is there any better reason that one can present than this (mere) feeling? It turns out there is.
There is a large body of research in psychology into how people comprehend negations. Negations are statements which contain a positive statement which is negated; the first code snippet is a prime example of a negation. I had not read about this area before this weekend, and there are several interesting points which can be made based on the research.
My first finding is it has been long known by researchers in the area people are quicker at understanding an affirmative statement than a negation. This in itself provides reasoning behind the seemingly small change above, implying the second version will be comprehended faster.
The piece which caught my eye, though, was an article about research into the methods the brain actually uses to process a negation. I won’t go into the details; if you are interested, I would suggest reading the linked article — the method used by the researchers to test their hypothosis was very cunning.
The experiment presented suggested it can take up to a second before the negation is represented in the mind, and, more interestingly, that the positive version of the statement is present initially. That is, on reading the negative version of the code, you first imagine the positive version then bring to mind the opposite case, the negation. As opposed to understanding the negation straight away.
What this means for our small example is that it takes longer to comprehend the negation in the first snippet than the affirmative statement in the second, even though their meaning is exactly the same. This has several implications.
Firstly, it will simply take a longer time to read the negation in the first snippet. These mental road-humps will quickly build up over the course of a method or two.
Secondly, if you skim too quickly, a possible implication is your brain doesn’t have time to represent the negation. Explicitly, you only comprehend the affirmative version before you begin processing the next statement. This would leave you with an incorrect view of how the code works.
Finally, and most importantly, the research indicates using the affirmative version reduces cognitive load, meaning that the code is less tiring to read. If you have to represent first the positive, then the negative, you may have to expend twice the effort than just understanding the positive and be done. It is not simply slower to read the negation, but more tiring.
So, write affirmative statements and make it easy on your future self’s brain.