Put the Mouse Down and Step Away from the Keyboard
来源:互联网 发布:正交矩阵的充要条件 编辑:程序博客网 时间:2024/05/21 08:45
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 formathh: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 forAM 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 returnedtrue
.
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
- Put the Mouse Down and Step Away from the Keyboard
- Put the Mouse Down and Step Away from the Keyboard
- The Mouse and the Keyboard
- How to detect when the mouse move away from a TPanel in Delphi ?
- Getting Input from the Mouse
- Getting Input from the Keyboard
- spoj 1418 The Cats and the Mouse
- 轻松告别鼠标滚轮摩擦声-鼠标滚轮上油 The solution of how to cut down the noise from cycle of you mouse
- Firefox Keyboard and Mouse Shortcuts
- lion and the mouse 小人物也起大作用
- please take me away from the world
- bzoj-3011 Running Away From the Barn
- Remember ahead and afterward matrimony a matron have to reside away from the 10 men
- Down the Excel template from SAP server
- travel the binary tree by level 3 ( from down to top and from right to left every level )
- travel the binary tree by level 4 ( from down to top and from left to right every level )
- travel the binary tree by level 5 ( from down to top and from left to right every level )
- The Mouse Love The Rice
- Log Shipping搭建
- 蓝港在线CTO双人行:轻量级开发模式保持竞争力
- Put Everything Under Version Control
- 知新网CTO:从程序员的视角,实践精益创业
- 清明节前最后一天
- Put the Mouse Down and Step Away from the Keyboard
- 知新网CTO:从程序员的视角,实践精益创业
- oracle数据库日志分析
- Read Code
- Session处理——Session同步
- Springmvc数据校验返回Json格式
- Read the Humanities
- 减少.net应用程序内存占用的一则实践
- 【ci框架】cookie登陆