Monday, November 06, 2006

Refactoring Shock Can Kill

A psychology lesson that could save your life

WHEN STEVE DUMB STUMBLED ACROSS THE SPAGHETTI of a legacy code library sunk into the muck in the labyrinthine of Upper Management Cost Saving Schemes, the unease he had felt since becoming lost in the Corporate wilderness turned to terror. "I'm going to die here." he remembers thinking. Alone and cold. Dumb was catapulting into the third and most dangerous stage of the phenomenon psychologists call "Refactoring shock," the confusion and fear that humans feel when they become lost in code.

As an experienced programmer, he knew that the wisest action was to shelter in the HR wreckage and signal for rescue, or at least rest awhile and gather his wits, But being lost caused so much stress on Dumb's nervous system that he was not able to think logically.

[1] DISORIENTATION
The first symptom of Refactoring shock is denial. If it seems to be taking too long to reach the code smell, or if a method that's supposed to be familiar appears different, your brain tries to bend what you see to fit its mental map of the code. In Dumb's case, he started to believe that the main artery of the bug he wanted to fix was just beyond the next line of code, and then, when he crossed that line, the one after that. At precisely the point when you should backtrack to the last known build, you press forward. That's when you become lost.

[2] URGENCY
As you become progressively more confused, the tentacles of the spaghetti seem to close in. Some people will feel claustrophobic or experience vertigo. The silence becomes oppressive. William G. Sucktruck — author of Analysis of Lost Refactoring Behavior — notes that as the feeling of being closed in increases, the impulse to act becomes overwhelming and people try to "break out" by coding faster.

[3] PANIC
In full-fledged panic, the victim becomes frantic, stumbling over rough lines of code, busting structure, often rewriting entire classes to get a vantage. Sweating profusely, he may shed hair, skin follicles, and clothing in an effort to go faster. For a programmer, the temper is the first to go. A Google search-and-rescue coordinator told me that once rescuers begin to find discarded tempers, their mission becomes a body search, as the odds of survival plummet precipitously.

[4] PLANNING
Exhausted and caffeine-overdosed, victims who survive the initial burst of panic without succumbing to insomnia or delirium may belatedly try to form a logical plan of escape, to return to a place of familiarity on the UML map. For most, it's an exercise in futility, for they are both too befuddled and too far off the track to find it again. Dumb didn't stop to make a plan until he'd thrashed through code for several hours and realized he'd gone in a circle and was back at the original cvs entry. The sight sobered him up. Convinced then that the trail was on the far side of the classlib, he struck off in a straight line. He soon found himself up to his chest in the first subclass he came to, with his fingers getting stuck in the sweat on his keyboard.

[5] FATIGUE
Failure of the plan is the straw that breaks the spirit. Emotionally and physically devastated, often verging on hypothermia, victims finally admit to being lost with no chance of regaining familiar ground. Many are so disheartened that they make no effort to build backup plans or excuses, even if they have the means to do so. At this point, any hope of survival lies not around the next bend but in the character of the lost programmer.

[6] WILL TO LIVE
Why does one person lie down and die, when another, poised just as precariously on the brink of despair, regains his composure and resolves to live? Case studies suggest that this depends on mental strengths that many survivors share: a sense of humor, the determination to see loved ones again, an ability to live in the moment rather than be engulfed by the seeming hopelessness of their situation. Survivors set and accomplish small goals such as building to do lists, and then they do the next right thing. In this manner, they stay busy and focused, right up until the moment of glory!

What course did Dumb take? He had the resolve to push through his fatigue and free himself from the spaghetti and keep going. He never had to discover whether he had the reserve of will to overcome another setback, because his guess was right. He managed to cross three deep bugs, and on the far side of them was the trail leading back to his business objectives, where he met some open source code which gave him aid. Some luck saved him, for sure, but also a will to live.

Three Tips to Overcome Refactoring Shock
SPEND
a few unprepared nights in Spaghetti code. The more times you build at nightfall, the more likely you are to succeed in a survival situation.

TUCK a photo of your wife, kids and PCMod in your wallet. Many survivors have drawn strength from looking at a picture of someone they love.

TAKE Refactoring training. Studies suggest that although an emotional response always precedes a logical response, the brain does draw upon experience to shape its initial reaction to stress.

By Keith McCafferty (har har)

Labels: