Put the Mouse Down and Step Away from the Keyboard

From WikiContent

Revision as of 03:20, 13 March 2009 by BurkHufnagel (Talk | contribs)
Jump to: navigation, search

I was cleaning up some legacy code and ran into an interesting method. It was designed to verify that a string contained a valid time using the format “hh:mm:ss xx”, where ‘hh’ represents the hour, ‘mm’ represents minutes, ‘ss’ represents seconds, and ‘xx’ is either ‘AM’ or ‘PM’.

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

try {
    Integer.parseInt( time.substring(0, 2) );
} catch ( Exception x ) {
    return false;

if ( Integer.parseInt(time.substring(0, 2) ) > 12) {
    return false;

The same code appeared twice more, with appropriate changes to the character offset and upper limit, to test the minutes and seconds. The method ended with these lines to test for ‘AM’ or ‘PM’:

if ( !time.substring(9, 11).equals("AM") & !time.substring(9, 11).equals("PM") ) {
    return false;

If none of the comparisons failed and returned “false” then the method returned “true”.

If the preceding code seems wordy and difficult to follow, don’t worry. I thought so too; 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.

When I finished, I felt pleased with the results. The new version was easy to read, half the size, and more accurate because the original code only tested the upper boundary for the hour, minutes, and seconds.

While getting ready for work the next day, an idea popped in my head; why not validate the string using a regular expression? After a few minutes typing, I had a working implementation in just one line of code. Here it is:

public static boolean validateTime( String time ) {
    return time.matches( "(0[1-9]|1[0-2]):[0-5][0-9]:[0-5][0-9] ([AP]M)" );

The point of this story is not that I eventually replaced over thirty lines of code with just one. 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, my story may sound familiar. It often goes like this: You’ve been focused for hours on some gnarly problem and there's no solution in sight. So you get up to stretch your legs, 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.

P.S. If you’re interested in learning more about how your brain works, there’s lots of sources available. My current favorite is Andy Hunt’s book “Pragmatic Thinking and Learning”.

By BurkHufnagel

This work is licensed under a Creative Commons Attribution 3

Back to 97 Things Every Programmer Should Know home page

Personal tools