Or, why starting a major refactor at 10pm is never a good idea.
I’ve done it once too often. I’m sitting there at the end-end-end of a productive day’s coding, and suddenly I have a revelation, some stunning architectural insight that gleams with simplicity, the polished smooth lines of its design making my current implementation feel like some kind of Baghdad construction site. It’s as obvious as it is elegant. And it must be implemented. Now.
And so, ignoring my better judgement, the Samurai is unleashed, ploughing into a major refactoring at 10pm.
The shiny IDE with all its single-keystroke refactoring smarts is the Samurai Coder’s weapon of choice. I have mine wired to my favourite Emacs key-bindings. Its razored blades project destruction with the flick of a finger – slicing and dicing large tracts of code with the merest thought.
So there I am, in my own Kill Bill sequence of large-scale dismemberment as I render radical surgery to the code base. Classes dispensed with, renamed, repackaged. Members pushed up, down, out of existence. Mass graves for the disappeared. Nothing can hide from option-F7, “Find Usages”.
The ripple of destruction spreads relentlessly outward, until I find myself in unfamiliar territory – code I haven’t seen in quite a while. The Samurai sheds his first beads of sweat. It’s midnight. Behind me the code base lies flayed open, filleted and bleeding. The modified file count has hit 40.
No time for reflection. Don’t lose the vision. It’s onwards and inwards.
By 2am the clichéd chirping of the night crickets outside provides a suitable background as the full context of the carnage I’ve unleashed comes into focus. The gleaming vision of architectural purity has faded and been replaced by a vision of red squiggly lines everywhere. Unit test failure on a grand scale.
In the end there were just too many classes. The coupling was too severe; the dependencies too subtle. The Samurai is beaten. The blades of destruction spin down. Not so much Kill Bill – more my very own horror film, The Refactoring.
There is only one course of action left for the true Samurai:
$ svn revert -R .
Been there! Thank god for revert. Sometimes it’s better to live to fight another day. I find the second attempt on a new day (properly caffeinated) will find a more moderate path to success.
— Alex Miller May 31, 04:48 PM #Very good post. Kind of poetic.
$ svn revert .
At least it was fun and did you cost nothing.
I never was a good coder at night and my code got unusable after 2am. Usually in the morning at 11am my first tasks were to fix the dozens of stupid bugs I added to the code. Now I only code at night for my open source projects, never for money ;-)
— Stephan Schmidt May 31, 06:07 PM #Oh and thanks for providing the excellent Cenqua tools to open source, if you have something to do with them.
— Stephan Schmidt May 31, 06:09 PM #Hehe, this does sound familiar, although, working in a more traditional “9 to 5” environment, my Samurai instincts tend to kick in a bit earlier. Nothing like starting a major refactoring exercise at 5:50pm, when you can see people around you leaving the office and you nonchalantly go “I’ll see you guys at the pub in 20mns, this shouldn’t take too long…”
— Christophe Chuvan May 31, 07:39 PM #How many times I’ve been there! Nice post.
— Vincent Massol May 31, 08:54 PM #Yes, very true. Sometimes it’s worth checking your changes into a separate branch so you don’t lose then. Then again, frustration usually means wiping out any trace of folly.
— Mark McLaren Jun 1, 02:13 AM #Poor Baghdad! It was such a beautiful, and not so long ago, peaceful country! Now they have invaded you, destroyed you, and killed 600,000 of your children, and now they are making fun of you! Aahh…
— alan Jun 1, 11:26 AM #I find that using a more flexible VCS than Subversion (like bzr) helps with this, as you can commit to a local branch without having to break things for everyone else…
— Daniel Watkins Jun 13, 11:16 PM #Hey, – you made the reddit.com front page! ;)
http://programming.reddit.com/info/1y1p1/comments
— James Head Jun 14, 12:16 AM #Great story, all too relatable. Thanks! :)
— Adam Jun 14, 12:28 AM #Now in my .bashrc:
— Stephen Jun 14, 12:58 AM #alias seppuku=”svn revert -R .”
Great writing, but do yourself a favor and get Martin Fowler’s Refactoring book.
You should break up your refactor in to tiny little pieces, each step getting you a bit closer to the goal, each step passing unit tests, and each step committable.
— Risotto Jun 14, 01:04 AM #i wish samuraicoder.com was free.
— heri Jun 14, 01:31 AM #love revert
— jesús Jun 14, 01:33 AM #Personally, my brain doesn’t kick into high gear until about 1am.
About a month ago I went on a 10-hour code-binge that took me to about 9am. As best I can gather, I tapped into the universe’s knowledge and unleashed a codebase by the likes I’ve never seen.
— Andrew Herron Jun 14, 01:34 AM #nice :) Reminds me of a prank I pulled a while back, some guy had ‘written’ an article about “Woods Shock” (psychological reaction to being lost in wilderness)... I replaced the “woods shock” with “refactoring” (along with some creative editing), and the resulting text was shockingly accurate ;)
http://www.sakri.net/crap/RefactoringShock.html
— sakri Jun 14, 01:47 AM #Yikes revert… doesn’t that mean you lose all the work you just did? Even if it didn’t work out, there is usually something worth keeping in there. You should look into a distributed VCS, where working with branches is second nature. Good read though, thanks for the post.
— Damien Jun 14, 03:52 AM #I also had one of those moments during a weekend last month. It involved rebuilding a form for tables where hundreds of fields had changed, along with data that was migrated, and there was no going back. By 4am Monday the new form was only 80% finished, and users would be waking up soon, so I cut a path of least resistance. I created a sql view that imitates the old table, got the old form working in 1 hour, and slept for 14 hours.
— will Jun 14, 04:58 AM #Lovely piece, congratulations! You’re using IntelliJ right? (option-F7)
— Bert Jun 14, 07:17 AM #“Sometimes it pays to stay in bed in Monday, rather than spending the rest of the week debugging Monday’s code.” Dan Salomon
— benp Jun 14, 07:45 AM #I could feel you channeling Akira Kurosawa…
— Spencer Jun 14, 08:05 AM #I experienced it just a few weeks ago—actually implemented the rework successfully before I went out of town for other developers to work on.
left at 2 a.m.
11am flight.
all was good.
no thank you was offered…
again…
so now I leave my sword at home ;-)
Wow… The futility of this entire post astounds me. What was this story about exactly? You being stupid? How does this equate you to military nobility in pre-industrial japan? Warriors who dedicated their life to skill and verse. All you did was act foolish and waste an entire night, then dressed it up in some fancy prose to make yourself feel better about your idiotic behavior.
Way to go!
— ranting jerk Jun 15, 02:10 AM #Excellent post! Good on ya lad!
— HashiDiKo Oct 31, 10:55 PM #You need to sharpen your sword with unit testing :)
Leave the battle field a hero!
— bob Nov 1, 03:43 AM #