Beware the finger trap!

I think it’s useful to coin pithy terms for phenomena that all software engineers experience but have a name to put to. Our term of the day is “finger trap”.


<!--more>

A finger trap is a programming problem that is conceptually very simple to state, and algorithmically fairly trivial, but an unreasonable pain in the ass to actually code given that simplicity.

It's named after a gag called the <a href="https://en.wikipedia.org/wiki/Chinese... Finger Trap</a>, simple, deceptive, and the harder you struggle with it the more trouble you have extricating yourself.

My favorite very simple example of a finger trap is interpreting <a href="https://en.wikipedia.org/wiki/Escape_... backslash escapes.</a> The trap closes when you realize you have to interpret \\, \\\, \\\\, and so forth.

A very famous finger trap is BitBlt - copy a source rectangle of pixels to a target location. The tricky part comes from the background having edges; you may have to clip the copy region to avoid trying to copy pixels that are off the edge of the world, and then modify the copy so you only copy to locations in the world.

These two examples exemplify what I think is the most common kind of fingertrappage: when it's easy to miss edge cases, and as you get deeper in the edge cases proliferate. In the case of BitBLT these are both figurative and <em>literal</em> edge cases!

When I bounced this term off some friends I learned that Amazon uses finger traps to screen potential developer hires. A finger trap, you see, is a test both of coding skill and personality. How good are you at anticipating edge cases? How do you react when you discover there are edge cases you didn't expect?

Do you plow on stolidly, spot-gluing a series of increasingly dubious kludges to your original naive approach? Do you panic and recover only with difficulty, if at all? Or do you step back and rethink the problem, sitting on your coding fingers until you have mapped out the edge cases in your head to at least one level deeper than before?

A Chinese finger trap gets its grip on people because, having gotten themselves in a little bit of trouble, they do an instinctive but wrong thing, and then repeat the failing behavior as though doing it harder will get them out of trouble.

Software finger traps catch developers in a very similar way. To get out of one, relax, rethink, and reframe.

The good news, if you're a junior or intermediate-level developer, is that once you've seen enough finger traps you'll start to be able to detect them before they close on you. In which case you can move straight to the winning part of the game, which is <em>developing a clear mental model of the edge cases before you write more code.</em>

You will not always succeed at this the first time. Serious finger traps have multiple layers, and spiky bits where you're least expecting them. The point is, when you hit a finger trap, you have to relax and think your way out. Trying to code your way out will only get you more stuck.

My commenters are invited to suggest other examples of finger traps.

</p>

 •  0 comments  •  flag
Share on Twitter
Published on November 19, 2019 21:07
No comments have been added yet.


Eric S. Raymond's Blog

Eric S. Raymond
Eric S. Raymond isn't a Goodreads Author (yet), but they do have a blog, so here are some recent posts imported from their feed.
Follow Eric S. Raymond's blog with rss.