Friday, December 28, 2007

Needless Complexity...

One of the many challenges in software development is avoiding complexity that serves no good purpose. As a particular program matures, and more programmers work on it, the almost inescapable tendency is for more and more of this needless complexity to be introduced. This happens for lots of reasons – new programmers working on code that they don't understand well (and therefore don't realize that there are simple ways to do things), programmers thinking in complex ways about problems that are actually simple, programmers not thinking a design through, etc. There are some patterns: sophisticated programmers with heads full of academic knowledge about algorithms and systems analysis will often make complexity mountains out of complexity molehills; their deep understanding somehow leads them to ignore (or miss) opportunities for simplification. Mind you, I'm as vulnerable to these things as the next programmer – I'm observing here a phenomenon that is almost universal in my own experience. However, the “strength” of the tendency varies radically from individual to individual – some engineers have a real talent for finding simple solutions; others have a real talent for finding complexity even in areas where most engineers see the simple solution.

I just read an interesting essay on this very subject, by Ryan Tomayko. It's called Insects and Entropy, and here's a sample (but do go read the whole thing!):

Right before the class ended the dumb kid asked Jon to take a look at his insect. Jon had to fight the urge to laugh out loud when he saw that the entire insect was a mere 25 lines of code that barely made it through the compiler and with some lines having no chance of even being executed. The dumb kid had not even configured his insect's basic set of traits but had left them at the professor provided defaults.

Looking more closely, Jon found that the insect was programmed to do the same thing every time it had a turn to move:

  1. Rotate 90 degrees.
  2. Attack.

Turn and then attack. That's it? Jon asked, to which the dumb kid replied, Do you think I'll pass?

Jon tried to give the dumb kid some ideas on making his insect more advanced but the dumb kid wasn't interested. Jon decided that the dumb kid would most assuredly not pass.

The next day the competition was on. The professor loaded up the simulation program and everyone hooked their insects into the system. The dumb kid was late and then couldn't figure out how to get his insect loaded up. Jon helped him out while mumbling something about futility...