Codementor Events

Perils of intuition

Published May 26, 2018Last updated Nov 22, 2018

My students learn things intuitively based on examples. In other words, they generalize. Most of the time, their intuition is correct. Sometimes, however, their intuition is spectacularly wrong and they can acquire false beliefs that become the foundation for other false beliefs. This is the story of one such example of intuition misleading many of my students.

Consider the following javascript code

let foo = () => { console.log("hello"); return 5; }
let x = foo();
console.log(x);

As a programmer, I will probably start reading the code from the bottom. I would notice that there's a variable x and I would investigate the previous lines of the program to learn more about the variable. My continued sleuthing would lead me to learn more about the foo function.

In other words, humans start at the end of the program and work their way back to find out the causal chain of events. This leads a human reader to read the program backwards. In other words, you read the last line, then you move one line up, and you continue the process.

A student might be lead to believe that this is how computers work. That a computer looks up information as it is needed. Unfortunately, this is the exact opposite of what a computer does.

Believing that the computer acts like a human impedes your ability to debug code and reason about your programs.

Let me demonstrate how this intuition can lead to false conclusions with some javascript code:

let foo = () => { console.log("hello"); return 5; }
foo();
let x = 10;
console.log(x);

Many of my students have argued that hello is never displayed since the output of foo is never needed. If something is not needed, why calculate it?

It is calculated because machines are very dumb and they blindly execute your program line by line without an iota of concern for reason, purpose or efficiency.

This is just one more example of how you need to be ready to put your past experiences and intuition aside when first learning programming. It is a hard and difficult journey. Thankfully, there's some good news: once you've internalized the counterintuitive rules of programming, you won't be able to imagine things being any other way.

Discover and read more posts from Jacques Le Normand
get started
post commentsBe the first to share your opinion
Show more replies