Put the Mouse Down and Step Away from the Keyboard

From WikiContent

(Difference between revisions)
Jump to: navigation, search
m
Current revision (10:48, 16 August 2009) (edit) (undo)
 
(14 intermediate revisions not shown.)
Line 1: Line 1:
-
While cleaning up some legacy code, I found a method designed to verify that a formatted string (cleverly named “time”) contained a valid time.
+
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 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’. When called, the method would report “05:23:42 AM” as valid, but “5:23:42 AM” would fail.
+
Does this scenario sound familiar? Ever wonder why it happens? 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.
 +
 
 +
Here's a real-life example: 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:
-
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:
 
try {
try {
-
Integer.parseInt( time.substring(0, 2) );
+
Integer.parseInt(time.substring(0, 2));
-
} catch ( Exception x ) {
+
} catch (Exception x) {
return false;
return false;
}
}
-
 
+
-
if ( Integer.parseInt(time.substring(0, 2) ) > 12) {
+
if (Integer.parseInt(time.substring(0, 2)) > 12) {
return false;
return false;
}
}
-
The same basic 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’:
+
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 check for ''AM'' and ''PM'':
-
if ( !time.substring(9, 11).equals("AM") & !time.substring(9, 11).equals("PM") ) {
+
 
 +
if (!time.substring(9, 11).equals("AM") & !time.substring(9, 11).equals("PM")) {
return false;
return false;
}
}
-
If none of the previous code found a problem and returned “false” then the method returned “true”.
+
If none of this series of comparisons failed, returning <code>false</code>, the method returned <code>true</code>.
-
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.
+
If the preceding code seems wordy and difficult to follow, don't worry. I thought so too &mdash; 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 was done, the new version was easier to read, half the size, and more accurate. The original code only tested upper bounds for the hour, minutes, and seconds. Not bad for half an hour’s work.
+
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 tested only the upper boundary for the hours, minutes, and seconds.
-
While getting ready for work the next day an idea popped in my head. Use a regular expression to validate the string. After a short round at the keyboard, I had a method that only needed one line of code, but it felt too complicated so I split it up to make it more readable. Here it is:
+
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 ) {
+
 
-
String REGEX_PATTERN = "(0[1-9]|1[0-2]):[0-5][0-9]:[0-5][0-9] ([AP]M)";
+
public static boolean validateTime(String time) {
-
return time.matches( REGEX_PATTERN );
+
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 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 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.
 +
 
 +
So the next time you hit a nasty problem, do yourself a favor. Once you really understand the problem go do something involving the creative side of your brain &mdash; sketch out the problem, listen to some music, or just take a walk outside. Sometimes the best thing you can do to solve a problem is to put the mouse down and step away from the keyboard.
 +
 
 +
By [[BurkHufnagel]]
 +
 
-
If you’ve been programming for a while, you’ve probably had similar experiences. You’re focused for hours on some gnarly problem with no solution in sight so you get up for a bio-break or to hit the vending machines, and on the way back the answer suddenly becomes obvious.
+
This work is licensed under a
 +
[http://creativecommons.org/licenses/by/3.0/us/ Creative Commons Attribution 3]
-
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 get may surprise you.
+
Back to [[97 Things Every Programmer Should Know]] home page

Current revision

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.

Does this scenario sound familiar? Ever wonder why it happens? 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.

Here's a real-life example: 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 check for AM and PM:

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

If none of this series of comparisons failed, returning false, 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 tested only the upper boundary for the hours, 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.

So the next time you hit a nasty problem, do yourself a favor. Once you really understand the problem go do something involving the creative side of your brain — sketch out the problem, listen to some music, or just take a walk outside. Sometimes the best thing you can do to solve a problem is to put the mouse down and step away from the keyboard.

By BurkHufnagel


This work is licensed under a Creative Commons Attribution 3

Back to 97 Things Every Programmer Should Know home page

Personal tools