Chris Johnston

Web development and design with a little VFX thrown in for fun
  • Home
  • About Me
  • Contact Me
  • Projects
  • Resume

Rewrite or Refactor?

Published by Chris Johnston on November 22, 2007 12:06 am under Agile, Application Development, Programming

When you inherit code, how do you know when to spend time refactoring a particular class versus simply throwing that class out and rewriting it? How bad does a class, method, package, section of code have to be to warrant rewriting it?

On my current project, we have a few classes that are 1000s of lines of code in length with maybe one or two methods. These classes are very procedural and generally only have one unit test. (On the up side, they have about 80% code coverage) The powers that be have decided to scrap the current code and rewrite it. Under these circumstances, definitely agree with, and helped push, this decision. But this is a fairly extreme example.

Is this decision only faced in these extreme examples, or are there other legitimate reasons for rewriting code? In my experience, rewriting the code, if done properly (e.g., with refactoring and using TDD), would be faster and result in better results then refactoring.

5 Comments so far

  1. Chris on November 28th, 2007

    The ramp up time on “reverse engineering” legacy code can be substantial. If you have clear requirements a rewrite may not be a bad idea.

    What I usually see in practice though is that the requirements are *not* 100% and the legacy code contains various little bits of logic to handle accumulated undocumented reqs. Or it contains “features” that other logic depends on without necessarily realizing it.

    When you have that rat nest of issues a rewrite usually ends up working until it gets to the end user or someone notices the new code does not handle case xyz properly or like the “old code did.”

    If you have the time (who does?) you can begin covering the legacy code in behavior tests to discover just what it is doing. You can then apply those tests to your new code and ensure both sides pass.

    Code coverage tools are essential to make sure you cover the old stuff sufficiently. Of course, by the time you get to this point, the pendulum swings back to just refactoring (since you now have an extremely high test coverage!)

    No good answer yet. I usually lean towards rewrite because if it breaks a “feature” that isn’t spec’d out… it usually points to a bigger problem anyway.

  2. Carfield Yim on November 28th, 2007

    Would you describe more about you experience??

    By the way, the most difficult part of rewrite is to collect back the requirement, how do you overcome that?

  3. Chris on November 29th, 2007

    I haven’t yet figured out a great way to collect “real” requirements yet. I’m not sure anyone has really (that I have seen).

    Often it is because the stakeholders themselves do not know what they don’t know and don’t always know what they want. And sometimes they get what they want but it’s not what they need, etc. etc.

    No silver bullets here.

  4. Chris Johnston on November 30th, 2007

    @Carfield – We are going through that process right now. Thankfully we have a lot of documents in which those requirements were captured the first time; however, as Chris stated, those requirements are not necessarily right and going back over them gives the business a chance to take a second look at them.

    So far, we are getting confusion over why we are doing the rewrite, but once we explain, the client is very willing to work with us to collect the requirements.

  5. Chris Leishman on December 5th, 2007

    I _always_ favour refactoring, rather than replacing or creating parallel implementations. This is because I tend to look at software development from a systems perspective.

    Instead of trying to understand all the requirements for a system as a whole, I prefer to try and understand _what the system currently does_ and _how I want to change that behaviour_. Changing behaviour could mean fixing bugs or adding new features. This applies equally at all levels of abstraction (eg. the entire business system, or a single interface/class).

Posting your comment.

  • Search

  • Categories

    • .NET (2)
    • Agile (41)
    • Apple Mac (15)
    • Application Development (124)
    • Articles (4)
    • ColdFusion (2)
    • Demo/Tutorial (3)
    • Eclipse (1)
    • Flash (6)
    • General (567)
    • Git (1)
    • Google (1)
    • Hibernate (4)
    • J2EE (39)
    • Java (111)
    • Java Frameworks (5)
    • Links (1)
    • Linux (33)
    • Miscellanous (2)
    • NetBeans (3)
    • News (10)
    • Open Source (6)
    • Photography (2)
    • Programming (33)
    • Python (1)
    • Ruby (27)
    • Ruby on Rails (14)
    • Ruby on Rails Web Apps (1)
    • Software (14)
    • Spring (4)
    • Teaching (1)
    • TeamDocs (6)
    • Technology (2)
    • Test Driven Development (1)
    • Thoughts (33)
    • ThoughtWorks (8)
    • Tips and Tricks (1)
    • User Experience (1)
    • Web Design (7)
    • Web Development (37)
    • Wicket (1)
  • Archives

    • September 2009 (1)
    • June 2009 (1)
    • May 2009 (1)
    • April 2009 (7)
    • March 2009 (2)
    • February 2009 (6)
    • January 2009 (4)
    • December 2008 (3)
    • October 2008 (1)
    • September 2008 (2)
    • August 2008 (6)
    • July 2008 (4)
    • June 2008 (1)
    • May 2008 (8)
    • April 2008 (7)
    • March 2008 (2)
    • February 2008 (1)
    • January 2008 (5)
    • December 2007 (3)
    • November 2007 (4)
    • October 2007 (5)
    • September 2007 (2)
    • August 2007 (3)
    • July 2007 (6)
    • June 2007 (5)
    • May 2007 (5)
    • April 2007 (5)
    • March 2007 (6)
    • February 2007 (9)
    • January 2007 (16)
    • December 2006 (6)
    • November 2006 (15)
    • October 2006 (17)
    • September 2006 (27)
    • August 2006 (22)
    • July 2006 (14)
    • June 2006 (10)
    • May 2006 (18)
    • April 2006 (3)
    • March 2006 (6)
    • February 2006 (15)
    • January 2006 (7)
    • December 2005 (11)
    • November 2005 (8)
    • October 2005 (18)
    • September 2005 (24)
    • August 2005 (18)
    • July 2005 (21)
    • June 2005 (14)
    • May 2005 (23)
    • April 2005 (18)
    • March 2005 (34)
    • February 2005 (27)
    • January 2005 (27)
    • December 2004 (15)
    • November 2004 (17)
    • October 2004 (20)
    • September 2004 (10)
    • August 2004 (21)
    • July 2004 (9)
    • June 2004 (11)
    • May 2004 (4)
    • April 2004 (15)
    • March 2004 (12)
    • February 2004 (7)
    • January 2004 (17)
    • December 2003 (11)
    • November 2003 (8)
    • October 2003 (12)
    • September 2003 (12)
    • August 2003 (12)
    • July 2003 (23)
    • June 2003 (22)
    • May 2003 (14)
    • April 2003 (9)
    • March 2003 (22)
    • February 2003 (24)
    • January 2003 (32)
    • December 2002 (11)
    • November 2002 (16)
    • October 2002 (10)
    • September 2002 (9)
    • August 2002 (13)
  • Pages

    • About Me
    • Contact Me
    • Projects
    • Resume

Copyright © 2010 Chris Johnston
WordPress Theme based on Light Theme