More permanent stuff at http://www.dusbabek.org/~garyd

08 September 2008

Technical Hubris

Programmers must often strike a balance between correct and pragmatic. This is often the difference between do it right and merely get it done. Rarely does the intersection of the two equal one and the other. But if you ask just about any programmer, he/she will almost always prefer a correct approach versus a more-pragmatic-but-less-correct approach when given the choice. Why? That's just the way most programmers are wired. That, and we're kind of arrogant.

The conflict is that software users almost always prefer that software be usable (pragmatic) even if it comes at the expense of being correct.

A case in point: one problem I worked on recently was to generate a unique set of keyboard shortcuts for a given set of buttons. The stipulation is that a shortcut (keystroke) could only be used once and must be included in the text of the button. I created an algorithm that was guaranteed to find a solution if one existed. It did this by recursively finding the characters (shortcut candidates) that were unique to each button over the set of all characters already mapped and all buttons that had not been assigned a shortcut. When the recursive algorithm bottomed out, all possibilities had been exhausted. If there were any buttons left over it was because assigning any shortuct to one of the remaining buttons would require a shortcut to be taken from another button. My algorithm was correct and I was pleased that I had thought of it to boot.

So given two buttons "My Button One" and "My Button Only", it would return the shortcuts as "e" and "l" respectively (first unique character in each sequence). It turns out this isn't as intuitive as returning, say "M" and "B". Ouch! It would have been difficult to make the correctness argument in view of the seemingly nonsensical shortcut characters that were returned.

The solution I settled on was to alter the algorithm to prefer the first character of the first word, and then the second word, etc. when selecting a shortcut. If no shortcut is found it then starts using other characters from the text. Also, I hard-wired a few words to shortcuts (exit -> x, quit -> q, etc.). This algorithm isn't guaranteed to find a solution if one exists (it isn't correct), but it produces shortcuts that make more sense to users.* It is practical instead.

* Geeky/gory details: here is where the new algorithm breaks down. Consider the following buttons "ab" "bc" "cd" "ac". The modified algorithm would produce shortcuts "a" "b" "c" and nothing for the fourth button. But the old algorithm would have come up with "b" "c" "d" and "a". Chances are slim that such a deviant case would ever be encountered in the wild though.

0 comments: