Sunday, August 10, 2008

Refactoring in action

Quite recently I attended my company's internal presentation regarding agile software development (ASD). During Q&A (questions and answers) part someone who was not convinced to use ASD complained that refactoring means rewriting the code from scratch. It's bullshit, of course, but as you can see people who don't know ASD can proliferate misconceptions about many ASD ideas.

In this post I would like to show you an example of refactoring in an unconventional way. I hope you will like it.


My very first apartment
Two years ago I bought my very fist apartment in the center of Gdynia. This picture of the kitchen was taken a day after housewarming party (before the rennovation):


Refactoring phase
We (me and my wife) didn't want to change too many things in this flat. We wanted to have a new kitchen and bathroom but the rest should be more or less untouched. In other words we wanted to do only slight refactoring of our apartment.

We were Product Owners and we changed our mind after some pieces of advice made by our parents and friends and we decided to change electrical, heating and water infrastructures as well. The refactoring was getting more serious.

When the heating and electrical teams finished they work (they completely destroyed our bathroom - this was part of the plan, unfortunately) we employed another team in charge of making our bathroom and walls flat ;) shiny, colorful and beautiful. At that stage we really wanted to keep the parquet on the floor but as our refactoring proceeded we discovered many unpleasant surprises ;) We had to remove the parquet and fill some holes in our floor with new layer of concrete! Oooops!

Our "product" looked like this (the middle of the "release"):


Refactoring often hurts
I as a Product Owner was in despair - I didn't want to watch this mess. Our apartment looked like big shit and it was getting worse and worse. Our refactoring phase was at the peek!

I was a good project manager of this project and coordinated work of 5 different teams - when one team finished the job the next one entered the flat few days later. Of course we had many many problems with the guys and it was often big hurt but my stubbornness and will made it to the happy end.

The refactoring was painful (especially that it was our beloved and very first apartment we wanted to inhabit very quickly) but the effect was stunning:


Retrospection
Our release took 2 months and the phases we went through were:

  1. completely destroyed bathroom

  2. destroyed walls in every single room

  3. destroyed floors


and maybe something more traumatic but I don't want to remember this.

We didn't destroy the structure of the walls and any other fixed elements of the building but within the flat we refactored almost everything. We made this flat amazingly beautiful, safe and modern (all "middleware" infrastructures are new). And it was worth it - even taking into account "renovation trauma" we still have :)

Software analogy
Please note that the analogy to the software refactoring is not very strong here as you will be probably tempted to perform rather shorter refactoring cycles. However, such big software refactoring happens (I used to do some) and can last even for half of the 30-day iteration but you have to perform them very carefully (hopefully you have good test coverage and you can sleep well after such operation). Going with this analogy further - it's much easier to make another short software refactoring task after few iterations and add some more features than destroying another room in the apartment while already living there - these worlds are not 100% compatible here :)

Refactor or not?
I think this example is quite extreme but hopefully shows how code refactoring could look like. In the middle of the refactoring phase, which can be project-wide and affect 80% of your classes and packages, you can have complete mess (or brothel if it translates to English). But at the end refactoring pays off and you can easily get new infrastructure and internal design. Your code will be more extensible and ready to implement new features. And it's not rewriting your code from scratch (look, we didn't destroy the whole floor of flats - we reordered the internals).

If you need to refactor your code significantly and you know the value will be greater than the cost you should definitely go for it. It could be painful, your manager and colleagues could hate you during this period of time but when they see the effect they will love you (of course if you succeed :). And you could go on holiday - someone else will add new feature basing on new refactored code.

Originally published on AgileSoftwareDevelopment.com

No comments: