The point of this story is not that I eventually wrote one line of code that did what the original didn’t quite accomplish with over thirty. The point is that until I got away from the computer, I thought my first attempt was the best solution to the problem.

+

The point of this story is not that I eventually wrote two lines of code that did what the original didn’t quite accomplish with over thirty. The point is that until I got away from the computer, I thought my first attempt was the best solution to the problem.

If you’re an experienced programmer, you’ve probably had similar experiences. It often goes like this: You’ve been focused for hours on some gnarly problem and there's no solution in sight. You get up for a bio-break, or to hit the vending machines, and on the way back the answer suddenly becomes obvious.

If you’re an experienced programmer, you’ve probably had similar experiences. It often goes like this: You’ve been focused for hours on some gnarly problem and there's no solution in sight. You get up for a bio-break, or to hit the vending machines, and on the way back the answer suddenly becomes obvious.

Revision as of 02:47, 7 March 2009

While cleaning up some legacy code, I found a method designed to verify that a formatted string contained a valid time. The expected format was “hh:mm:ss xx”, where ‘hh’ represents the hour, ‘mm’ represents minutes, ‘ss’ represents seconds, and ‘xx’ is either ‘AM’ or ‘PM’. It seemed fairly long so I took a closer look.

The method used the following code to convert two characters (representing the hour) into a number, and verify that it was in the proper range:

If none of the previous code found a problem and returned “false” then the method returned “true”.

If the preceding code seems wordy and difficult to follow, don’t worry. Even an experienced Java developer might think the same; which meant I’d found something worth cleaning up. I refactored it and wrote a few unit tests - just to make sure it still worked.

I felt pleased with the rsults. The new version was easier to read, half the size, and more accurate because the original code had only tested the upper bounds for the hour, minutes, and seconds. Not bad for half an hour’s work.

While getting ready for work the next day, an idea popped in my head; use a regular expression to validate the string. After a few minutes typing, I had a new implementation that only needed one line of code. It worked, but it felt too complicated so I split it in two to make it more readable. Here it is:

The point of this story is not that I eventually wrote two lines of code that did what the original didn’t quite accomplish with over thirty. The point is that until I got away from the computer, I thought my first attempt was the best solution to the problem.

If you’re an experienced programmer, you’ve probably had similar experiences. It often goes like this: You’ve been focused for hours on some gnarly problem and there's no solution in sight. You get up for a bio-break, or to hit the vending machines, and on the way back the answer suddenly becomes obvious.

The trick is that while you’re coding, the logical part of your brain is active and the creative side is shut out. It can’t present anything to you until the logical side takes a break. So put the mouse down and step away from the keyboard. The insights you gain may surprise you.