Refactoring the “Legacy” Hudson.java with the Mikado Method as a Code Dojo
Join the DZone community and get the full member experience.Join For Free
i’m preparing a code dojo for my colleges at iterate where we will try to collectively refactor the “legacy” hudson/jenkins, especially hudson.java, to something more testable, using the mikado method . i’ve got the idea after reading gojko adzic’s blog on how terrible the code is and after discovering the mikado method by a chance. since a long time i’m interested in code quality and since recently especially in improving the quality of legacy applications, where “legacy” means a terrible code base and likely insufficient tests. as consultants we often have to deal with such application and with improving their state into something easier and cheaper to maintain and evolve. therefore such a collective practice is a good thing.
the mikado method
the mikado method, which the authors describe as “a tool for large-scale refactorings”, serves two purposes:
- exploring an unknown, legacy codebase with the aim of learning enough to be able to perform a particular change
- performing the change with the minimal risk, that means especially without ever bringing the application into a broken state where it cannot be built or tested
the method itself doesn’t introduce any new refactorings, it is just a “container” for various well-known refactorings and servers as a guide or a map, adding the big picture of where we started, where we want to get, and where we are right now, thus helping us concentrate on that which is important and not loosing sight of our primary goal, which is otherwise rather easy once you enter the swamp of a rotten legacy code base.
the method is surprisingly simple, you need only a whiteboard and a version control system (vcs). the process is as follows:
- identify the goal you want to achieve – the mikado goal – and write it down on a whiteboard
- implement a naive solution, i.e. try to do a rather simple solution
- if the changes broke your code – so that it doesn’t compile or tests fail – add the things that must be done or satisfied before you can perform the change safely as child nodes to the mikado goal and revert your changes to the code base
- pick one of the new child nodes and go to 2.
- once you are actually able to do a change without breaking something, you can check the corresponding leaf node of the growing mikado graph and continue with another leaf until you eventually get to the original goal and get it implemented
following these simple steps you go on discovering what needs to be changed without getting broken code base which is impossible to work on or without wandering too far away from the goal that you ultimately want to achieve. it may sound as a waste to undo changes on a regular basis but with modern automated refactorings and eventually backups in a vcs it is a small price to pay for the safety that non-broken code base provides.
the insistence on a naive solution is important because legacy code bases are often so intertangled and complex that whenever you try to outsmart their badness you usually fail, hitting again and again the walls of unforseen problems, dependencies and crazy design decisions. with a simple solution you are more likely to succeed or at least discover the hidden complications before you wasted too much effort and you avoid the trap of “paralysis by analysis”. it doesn’t mean that you should never think and analyse – but do it only when it really pays off, keeping in mind that the code base is usually yet worse than you expect.
to understand the benefits and proper application of the mikado method, read the freely available draft of the mikado method book and try it on the “code kata” exercises that the authors have prepared for you. some interesting quotes from the book:
computer programs has to improve or they are doomed to a slow death. we, the developers, hold the fate of the code in our hands and we are the only ones that have the power to improve it. it is our responsibility to keep the code clean and fit for purpose. this means we have to be able to add code, improve our own code and the code of others.
the mikado method helps us visualize, plan and perform business- value-focused improvements over several iterations and increments of work, without ever having a broken code-base during the process. it enhances communication, collaboration and learning in software de- velopment teams. it also helps individuals or programming pairs stay on track while doing their day to day work. it provides us with frame- work to help us morph our system into the desired shape.
we naïvely try to implement a change, without analyzing too much in advance. by doing so, we don’t need to think about how this affect that or the other, something that keeps us from ending up in analysis paralysis.
the key to changing any system is to first change the restrictions enough to make the desired changes possible.
when there are errors we always roll back all our changes! this is extremely important! the reason is that we never want to edit the code when we don’t know what state it is in. since we have made changes that resulted in errors, we don’t have a stable state to make changes from.
the conclusion is yet to be drawn . i’ll publish a blog post with our experiences after the code dojo. stay tuned.
Opinions expressed by DZone contributors are their own.