TheRedPandas.com | Cognitive load testing, anyone?
Red Panda Innovation Labs is a software product and services firm which creates mobile applications, web applications and internet of things (IoT) applications. We consult businesses on their product strategy and work on user interaction, software architecture and software development of the products.
15133
post-template-default,single,single-post,postid-15133,single-format-standard,ajax_fade,page_not_loaded,,qode-theme-ver-8.0,bridge,wpb-js-composer js-comp-ver-4.9.2,vc_responsive

Cognitive load testing, anyone?

08 Feb Cognitive load testing, anyone?

There are burdens which are bad and blameworthy, and these it is our duty at once to cast away. – James Hamilton
A team-mate recently sought help over email while debugging a piece of JavaScript code that he was trying to get working. He had a working reference implementation in Python, but the JS equivalent didn’t seem to be working. Below are the Python and JS snippets he shared, asking if anybody could spot the difference.

 

Python code:
JS code:
It took just a glance for me to realize that spotting the difference in the pieces of code as they were written was gonna take me dangerously close to the limits on my capacity for processing information, what with upto 7 – 2 = 5 levels of nesting in the code. So, instead of trying to rise up to the challenge, I decided to begin by lowering the challenge for myself (a strategy I’ve found generally useful in software development).
The code and the situation here, even though almost trivial, present an instructive example of how programmers often fail to consider – to their own and their teams’ detriment – the effects of cognitive load when both reading and writing code. At such a small scale, such failures may seem like minor condonable infractions, but they add up superlinearly to the difficulty of understanding, and resultantly to that of debugging and modifying code. So, let’s look at some of the problems.

First, for the JS code:

  • The last line is the meat of the solution, and it’s supposed to be a simple mathematical formula to calculate equated periodic installments. However, the formula, which is the signal here, seems to be somewhat drowning in the noise of incidental and irrelevant details like $scope, parseInt and parseFloat.
  • The names are a bit misleading. paymentPerYear seems to indicate it stands for an amount, whereas it really is meant to be the number of paymentsPerYear (note the plural). interest should actually be interestRate.
Now, for the Python code:
  • The only noise is the self references. While I really appreciate the beauty and the power of the self in Python, I feel it often hurts readability by just the slightest amount.
  • Thelackofanyspacinginthecodemakesithardertocomprehend.
  • The inconsistent naming forces you to do a double-take every once in a while. You see the first variable, Loan_amount, and tentatively assume the naming convention to be ‘snake case starting with an uppercase first letter’. You see the second variable, Interest_rate, and it kinda confirms your assumption. And then you see… wait a minute, Payment_peryear. Double-take… Payment_per_year. Ah, that wasn’t too bad. Except that now you know you can’t just cruise along at a comfortable consistent speed on this road – you gotta be prepared to backtrack every once in a while, or simply slow down throughout.
Now, coming to spotting the difference. When diffing as well as debugging, I find it useful to strip out all the noise so you can focus on the signal and analyze it. When diffing, the commonalities are the noise, while the differences are the signal. In order to strip out the commonalities though, you first need to play them up and make them obvious. So, here’s what I did to make the commonalities (or is it the differences?) stand out:
  • insert some spacing for readability
  • get rid of the following extraneous things: self, $scope.loan., parseInt and parseFloat
  • factor out some duplication into a variable: r = interestRate / (paymentPerYear * 100)
  • factor out a computation into a variable: n = paymentPerYear * tenor
  • rename loanAmount to P
And here’s what I got:
P / ((1.0 - (1.0 / ((1 + r) ** n)))     / r) # Python
P / ((1.0 - (1.0 / Math.pow(1 + r, n))) / r) // JS
Or better yet (just the Python version):
(P * r) / (1.0 - (1.0 / ((1 + r) ** n))) # Python
In both cases, I see a somewhat convoluted form of the formula for periodic amortization payment. I can’t spot anything but syntactical differences between the two versions, and the only clue about what could be wrong with the JS code I have is a few guesses. But that’s beside the point.
The point is, I had to expend effort I think was undue to transform both the versions of the code into a form that wouldn’t strain my brain before I could say anything about them. The code would’ve turned out much better if the original author(s) had paid attention to minimizing the cognitive load for themselves and future readers and maintainers. With upto six levels of nesting (requiring the building up and subsequent winding down of a stack that deep in your mind) and the problems listed above, and having seen what the mathematical formula actually is, it would almost seem as if the original version was set up to obfuscate the solution rather than reveal it. This last version, with two-thirds the levels of nesting, is a direct and concise transliteration of the mathematical formula into programming language syntax, making it so transparent and easy to test that it highly minimizes, even nearly obviates, the possibility of bugs and the need to debug. As a bonus, one wouldn’t need to seek help to spot the differences between versions in two different programming languages.
In his excellent book “Specification by Example“, Gojko Adzic says this about examples used to illustrate specifications:
With traditional specifications, examples appear and disappear several times in the software development process. Everyone [e.g.Business analysts, developers and testers] invents their own examples, but there’s nothing to ensure that these examples are even consistent, let alone complete. In software development, this is why the end result is often different from what was expected at the beginning. To avoid this, we have to prevent misinterpretation between different roles and maintain one source of truth.
I think this description very nearly fits what most often happens with program source code as well. Every reader/maintainer ends up creating a less noisy representation of the code in their head while trying to understand it, and most often, in the absence of MercilessRefactoring, there it disappears into a black hole, only to be recreated the next time the code is read. So, the burden of the cognitive load is created once when writing the code, but is borne by everyone who has to read the code every time they do it… “create once carry forever”, anyone?
We commonly subject our code to load tests to understand how much load they can handle. How about if our code could undergo some form of cognitive load tests so we could gauge how much cognitive load it would impose? Now, I wish “cognitive load testing” were a thing.
1Comment
  • Avinash B
    Posted at 11:50h, 23 February Reply

    I feel your pain. As a practice I refuse to review a code if I can not understand it with in 15 seconds 🙂
    I just send it back for refactoring.

Post A Comment