Empathy for the Machine - Or, How to Improve as a Programmer and a Human

This article was originally published here by Hack Reactor engineer Blake West

Stop getting angry at the computer and start getting angry at yourself. When you’re learning to code, it’s easy to get frustrated at the tiny details that derail your program. It seems like the computer is being dumb. But it’s not. It’s doing exactly what you told it to do, and you just need some empathy. That empathy is key in separating beginner and experienced developers. When beginners throw up their hands, experienced developers try to debug by gaining a deeper understanding of the machine. What was it thinking? Why did that thinking cause this bug?

Sometimes at Hack Reactor, we tell students to walk through code line by line, pretending you are the machine. Here’s a simple example (if you’re new to javascript, console.log basically means ‘print’):

var superHero = {name: 'Batman'}

What happens when this code runs? You get nothing (undefined, actually). But no errors. Why? It looks like we’re just trying to print out the name property of the superHero object. Everything should work, right? No it shouldn’t, and this is a subtle bug because it doesn’t throw errors. But if you understand what the machine is doing, and treat it as an equal, the answer is clear. Let’s run it, looking through the computer’s eyes.

1.) Set aside memory for a variable.
2.) Let the string ‘superHero’ point to the newly constructed variable
3.) Make an object, and give it a property ‘name’, with a value of ‘Batman’
4.) Assign that object to the memory slot that ‘superHero’ refers to
5.) Look for a variable called name
6.) Oh no! No variable name has been defined before, so I’ll call it ‘undefined’
7.) Try to find ‘undefined’ property on the object superHero
8.) Can’t find it, so return undefined
9.) console.log what we found, which is undefined.

Ha! There we go. Step 5 is the secret. Because the word `name` wasn’t written with quotes, JavaScript thought you meant a variable called `name`, and it went looking for that variable. No such variable had been declared, so it kinda cascaded from there in to a bunch of undefined-ness. Had we written console.log(superHero['name']) , then everything would be fine, because it would know you meant to look for a property directly on the object with the string ‘name’. That does exist, and it has a value of ‘Batman’, so we’d get what we wanted.

Without empathy, this would be an easy time to get frustrated that the computer can’t simply ‘get’ what you mean. But with empathy, we see that we were the ones who made the mistake, because we didn’t think about how the words would be interpreted, and so our meaning was lost.

Man, having my meaning lost because I didn’t properly think how my words would be interpreted. That sounds eerily familiar to what I’ve heard from ex-girlfriends. And now we come full circle. Really getting in the mindset of your receiver is make-or-break whether you’re dealing with machines, or people. We can’t assume our meaning will be transferred just because we said the ‘right’ words. Often our meaning is confused by tiny details in how we interpret those words (or semi-colons, or tones of voice). So practice this kind of empathy on machines, and then bring it home to your loved ones, and you’ll see that everyone’s happier.