Confessions of a Samurai Coder

May 31, 11:20 AM

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 .

 

Comments:

  1. 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, 03:48 PM    #
  2. 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, 05:07 PM    #
  3. Oh and thanks for providing the excellent Cenqua tools to open source, if you have something to do with them.

    Stephan Schmidt    May 31, 05:09 PM    #
  4. 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, 06:39 PM    #
  5. How many times I’ve been there! Nice post.

    — Vincent Massol    May 31, 07:54 PM    #
  6. 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, 01:13 AM    #
  7. 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, 10:26 AM    #
  8. 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, 10:16 PM    #
  9. Hey, – you made the reddit.com front page! ;)

    http://programming.reddit.com/info/1y1p1/comments

    James Head    Jun 13, 11:16 PM    #
  10. Great story, all too relatable. Thanks! :)

    — Adam    Jun 13, 11:28 PM    #
  11. Now in my .bashrc:
    alias seppuku=”svn revert -R .”

    Stephen    Jun 13, 11:58 PM    #
  12. 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, 12:04 AM    #
  13. i wish samuraicoder.com was free.

    heri    Jun 14, 12:31 AM    #
  14. love revert

    jesús    Jun 14, 12:33 AM    #
  15. 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, 12:34 AM    #
  16. 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, 12:47 AM    #
  17. 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, 02:52 AM    #
  18. 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, 03:58 AM    #
  19. Lovely piece, congratulations! You’re using IntelliJ right? (option-F7)

    Bert    Jun 14, 06:17 AM    #
  20. “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, 06:45 AM    #
  21. I could feel you channeling Akira Kurosawa…
    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 ;-)

    Spencer    Jun 14, 07:05 AM    #
  22. 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, 01:10 AM    #
  23. Excellent post! Good on ya lad!

    HashiDiKo    Oct 31, 09:55 PM    #
  24. You need to sharpen your sword with unit testing :)

    Leave the battle field a hero!

    — bob    Nov 1, 02:43 AM    #

Blog roll

Codefeed
Madbean Matt
Vincent Massol
Codegargle
Mike Cannon-Brookes
Oliver Burn

Recent posts

Confessions of a Samurai Coder
JavaOne 2007 - random thoughts
Sneak Preview of Clover 2
Profanity in Software Considered Dangerous
20 Candles for CVS
Checking Pulse: CI Server initial impressions
Seethed Rivers on Our Sly Brain
FishEye plugin for JIRA
The Symmetry of Stupidity
Java Language Freakshow

Blatant plugs

Clover Clover & Clover.NET: Powerful code coverage for Java, C# and VB.NET
FishEye FishEye: datamining, browsing, monitoring for CVS, SVN, and Perforce
Crucible Crucible: efficient, distributed and process-neutral peer code review
Cenqua Cenqua: dedicated to the creation of practical, useful tools for software developers