Agile Refactoring
There's always something to refactor. Requirements evolve, short-cuts are taken, code doesn't get reviewed, and anti-patterns emerge. How does refactoring fit into agile development? How do you have to convince the product owner to make a refactoring system story a high priority for the next sprint? How do you address that crumbing design pattern a random contractor setup years ago? Here are some practices to help address these issues:
Document areas to refactor. When you have to take a short-cut to hit a deadline, first of all comment the code and then create a ticket to address the short-cut in another sprint. I also suggest tagging the ticket as "technical debt." This will give your team visibility into the amount of technical debt that is accrued.
Include refactoring into story estimations. If you know that a story would be best implemented if you refactored some code, don't start off by giving an estimate that assumes you'll use a short-cut. It can be natural to people-please and say things can be done sooner but try to avoid that and keep in the mind the goal of writing high quality software which includes refactoring.
Clean up the small things when you see them. Refactoring doesn't have to be a clean sweep of the whole house, it can be just a good habit of picking up things along the way. I wouldn't recommend rewriting significant logic for fun, but if you're in some code and find that a minor change would significantly improve readability then it might be worth the few minutes to do the cleanup.
Break down large refactoring changes into manageable tasks. You'll never have the time to do that gigantic overhaul of that terrible reporting system (or what-have-you) all at once. What you can do is break it down so that the work can be spread out across many sprints. This effort can be complex so I suggest starting by creating a task to outline all the changes that should be made.
Evaluate and convey the business value of the refactoring need. It's likely difficult to find a product owner who will add a refactoring system story into a sprint just because you tell them it'll make the code better. There's no apparent business value in that reason. Think of the issue from the product owner's perspective and consider the "so that" part of the system story. Ask yourself, "Would it improve system performance? lower technical debt? improve throughput? address the root causes of the growing list of bugs?" You may even find that the refactoring you wanted to do doesn't have significant business value and is just clean and maintainable enough the way it is.
One of the great things about agile development is the exposure and mitigation of risk. If a system needs significant refactoring then that's risk that the entire team should be mindful of. Refactoring can then be something that happens, not just something you want to do.
I like adding a #technicaldebt tag to the code. Kinda like a TODO, but more meaningful.
I tend to refactor as a go along, and build the cost into my user stories.
Jason
13 Nov 09 at 12:32 PM
Great post Sam. A discipline commonly used by XP teams is to keep a backlog of “clean up” tasks. Then, you can either schedule 10%-20% of the velocity for each iteration for cleanup tasks or you can fix cleanup tasks every 3rd or 4th iteration.
Another key discipline is implementing unit tests, preferably via TDD, that help teams to confidently refactor as they go.
A good product owner knows that he has to pay off his debt at some point or the velocity will suffer. I spoke to this in more detail on my blog: http://cdan.posterous.com/technical-debt-when-agile-software-developmen
Craig Daniel
18 Nov 09 at 1:10 PM