Saturday, August 23, 2014

Focus in Programming: Laser Focus vs. Soft Focus

As a programmer, I prize focus. Part of becoming a programmer is improving your ability to focus. Why? Because natural levels of human focus are insufficient to find common programming bugs such as a "single equals" in a conditional statement:

if( x = 5 ){
     print ("5")

Over time and with great pains, I have learned to hone my focus.  Although it serves me well most of the time, and I have become efficient at finding subtle syntax bugs like single equals, occasionally I run into bugs like this:

While building the application:
client/index.js:217:1: Unexpected token }

=> Your application has errors. Waiting for file change.

I saw the error and my first thought was "Great! The compiler knows exactly where the bug is! Easy peasy!" But then I go and look at line 217 of the file.

There is no line 217.  The last line is 216.

There's only one thought that goes through my head at this point: "Dear Compiler, WHAT THE FUCK?!"

Then my debugging gets stalled.  I look back at the error and confirm it is line 217. I look back at the file and make sure it is really only 216 lines. I double check the file name.  I focus as hard as I can and drill into this bug.

And it goes nowhere.

Although focus is usually the right solution, it is not the right tool to fix this bug.  More accurately, the narrow, laser-beam focus that I have trained myself to do is not the right tool. Instead, I need to consider my depth of field.

Focus in optics has as property called depth of field.  I have recently started modeling my own attention and focus as having this property.  I think of it like this: the concept of focus is not binary (focused or not focused). When choosing to focus, I must also choose what range of distance (or size of problem) I want to see sharply. 

For example, if I want to focus on one leaf in a tree, I use what I call "laser focus". (This is what I do to find the single-equals bug). I often feel intense when I do this. Caffeine helps.

But I can also focus not on a single leaf, but on the entire tree. I call this "soft focus" because I can feel my eye muscles relax and "soften" when I focus this way. Soft focus in not un-focused. It might not help you find the single equals bug, but it is useful for focusing on an entire system. For example, when I want to see how leaves move together in the wind, I use soft focus.  Soft focus lets me focus on the system as a whole rather than precisely tracking all the leaves individually.

Look out the window and try it right now.

For the "line 217" bug, I stop focusing on the line and the "Unexpected token }" message. Instead, I adjust my focus and consider the file as a whole. I think about recent changes I made, I see if I can Ctrl+Z my way back to working code. If I have to, I do a git diff.

In this case, the problem happened to be a missing parenthesis, not a curly brace as the compiler suggested.

function submit(){
 var radioButton = $('input:checked').val();
   var f ='submit', {
     user_id: Meteor.userId(),
     answer: radioButton
   }  ) <--- I FORGOT THIS FUCKER!!!  

Perhaps other people have better spidey sense for this type of bug, but I never would have found that bug by looking at the code.

My strategy is to stop using laser focus to drill into the details, and instead, I think about the system as a whole with a softer focus.

No comments:

Post a Comment