Tuesday, February 26, 2008

Prefactoring

I have found it valuable to distinguish between the refactoring done in Red-Green-Refactor and the Refactoring done when I have a new story to implement and the design needs to change to accommodate the new feature.

This second form I call Prefactoring.

Prefactoring is performed on otherwise clean well factored code in preparation of a new feature. Prefactoring is perfectly normal on any XP project and in-fact indicates a healthy ongoing design process.

The distinction grew in my mind when we were facing a lot of exiting code debt. I needed a way to distinguish between the 'repair work' I was doing and the 'perfectly acceptable design work'. This was in response to questions like "How much more refactoring is left?" which really meant "How much repair work is left?".

A quick word on Prefactoring and Iterative Design.

Ideally your design applies the OpenClosedPrinciple, which would imply that adding new features would simply require you to add new code. That is great as long as the feature fits one of the axis you have opened your design to change.

However YAGNI indicates you should keep your code as simple as possible. Attempting to open your design to changes in more ways than you currently need can be over-design.

The solution is that you keep your code simple but Prefactor when you need your design to be open in a new dimension. You can then simply add the new feature by adding code. Utopia!

Splitting the work into these two steps makes implementing new features a much simpler process. The new tests are always easy to write. The design always nicely accommodates the the new feature.

As ever, I appreciate Rob for shaming me into finally blogging about this and helping me separate the dumb ideas from the gems.

3 comments:

Nigel Thorne said...

It seems someone is already using the term Prefactoring to refer to the application of lessons learnt from previous projects to lead to writing code that needs less refactoring.

http://www.artima.com/weblogs/viewpost.jsp?thread=147332

I'll keep using 'Prefactoring' until I think of a better term.

Ronald Widha said...

Interesting.
In terms of story cards, does 'Prefactoring' deserve its own card/task?

Nigel Thorne said...

Prefactoring is intended prepair the code for a feature to be added. This would be pointless without the feature so is part of implementing the story. I break stories down into task these days, so I would have a task to prefactor.

GitHub Projects