Is Refactoring a "Needless Rework"?
Join the DZone community and get the full member experience.Join For Free
“refactoring is a needless rework - i want my team to get design right the first time!” told a project manager when one of us were discussing software design with him. is he right? why should developers “waste” time improving the design of existing code when they can be more productive and add value to customers by adding new features or make fixes in the software? why can’t designers or architects get design right the first time itself?
many of us have the experience of working in a long-running software projects that span millions of lines of code. such projects usually start with good design, but over a period of time the design degrades (what we know with the term “design decay”). gradually, work slows down and it takes increasingly more time to make fixes or enhancements. to get the work going, as developers we introduce some more short-cuts. the code starts stinking and often it reaches a state where development comes to a stand-still.
what options do we have in this situation? fundamentally there are only two options: rewrite or refactor. we can choose to throw away the existing software and buy a new one or rewrite it. this is what most developers prefer but is not very practical from the business perspective. another option is to refactor and that is the really only practical option we have. let us understand this with the help of an analogy.
think of a burgeoning city like bangalore - it is a perfect metaphor for the kind of struggles that software development teams undergo. as grady booch wrote in the foreword for our book :
“cities grow, cities evolve, cities have parts that simply die while other parts flourish; each city has to be renewed in order to meet the needs of its populace… software-intensive systems are like that. they grow, they evolve, sometimes they wither away, and sometimes they flourish…”
all three of us (ganesh, tushar, and girish - authors of this article) live in bangalore. we love and hate the city at the same time. the weather is good, people are friendly, and it’s a heaven for like-minded geeks like us to live - so we love it. we hate it because it is over-crowded and has notorious traffic snarls. it is rightly infamous for its traffic woes.
it is hard to believe now, but bangalore was a “pensioner’s paradise” just 30 years back. the existing infrastructure was adequate to live a peaceful life. with overcrowding (partly due to the it boom), the city’s infrastructure has become inadequate. the traffic situation is so bad that many a times, it takes a couple of hours just to cross 15 kilometers in many of the arterial roads.
is a complete overhaul of the city practical or feasible? no, not at all - we cannot afford to move people to other places and rebuild the city. do you remember lessons from history? for example, muhammad bin tughlaq’s attempt to move the capital of his sultanate from delhi to daulatabad in faraway maharashtra resulted in a complete failure .
how about refactoring? that is perhaps the only practical solution we have. refactoring (or restructuring) at different levels is needed. major restructuring such as introducing metro lines are necessary for getting a long-term solution for the traffic problems. but in the short-term, such restructuring causes inconvenience and pain to the citizens affected by such restructuring. at a lower-level, constructing flyovers, underpasses, expanding roads, etc are the more practical and immediate solutions to the traffic problems of the city. further, at an even more lower-levels, patching up the roads can enable a smooth ride.
- missing roads: in many places roads are missing and one has to struggle with muddy roadsevery day.
- uncontrolled access: traffic is uncontrolled in many of the underpasses (especially where the traffic signals are missing) - one can go from any direction to any other direction (causing frustration and gridlock in peak hours)
- bottleneck junction: there are junctions where it is possible to take numerous turns in different directions that lead to increased waiting time and single point of failure (e.g., the infamous total mall junction on outer ring road)
- footpath road: a footpath is meant for walking and not for biking. it is not uncommon to see people using footpaths as road driving bikes to bypass traffic.
- missing abstraction: what should have been a class or an interface in design is instead encoded as strings, group of variables, etc. an example is the case of getstacktrace() method in throwable class in jdk. the refactoring was introduced in java 1.4 in the form of providing a programmatic access to the stack trace (i.e., the method stacktraceelement getstacktrace() was introduced in java 1.4)
- deficient encapsulation: the accessibility of the members is more than required leading to unrestrained access. an example is the point class in jdk which has public data members x and y.
- hub-like modularization: when a type has a large number of incoming as well as outgoing dependencies. an example is the java.util.component class that has 498 incoming dependencies and 71 outgoing dependencies.
- broken hierarchy: when supertype and subtype do not share is-a relationship. for example, the stack and vector classes of java.util package do not share is-a relationship.
let us revisit the question we started with. “is refactoring a needless rework?” - absolutely not. it is of course necessary and forms the core practice for creating high-quality software. “status quo” is not acceptable - a creaking city like bangalore needs structural restructuring and improvements to sustain growth. “is it acceptable for managers to expect their teams to get design right the first time?” again, absolutely no - with changing requirements design becomes outdated and often transforms into a something that stinks. continuous evolution is the only way to deal with changing requirements.
the summary of this article: whenever you are confronted with this question of should you refactor, consider the city as an analogy to understand and convince why continuous evolution is the only practical way to live in it. you may delay it but you cannot avoid it.
Opinions expressed by DZone contributors are their own.