October 22, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Can Refactoring Produce Better Code?

  • March 6, 2007
  • By Jeff Langr
  • Send Email »
  • More Articles »

Up-Front Design

If you could design things properly before you started coding, and if you could always produce the best code possible, maybe you could dispense with the need for refactoring. Some developers firmly believe that the need to refactor indicates a poor up-front design.

The reality is that neither perfect design nor perfect coding exist. Inevitably, the act of coding brings to light things you might never have considered during an up-front design phase. And often, code that makes sense to you while you're developing it may be unreadable six months later. That's just human nature. You can improve on up-front design and initial coding skills, but you'll never be perfect. So, any solution that presumes you should only worry about up-front design and getting code "perfect" the first time is going to be deficient.

Imagine you initiate a one-year project. Suppose you could craft a perfect design and write perfect code for this project. That's still not good enough! There are always new requirements. You usually want your systems to grow over the years, continuing to generate revenue or continuing to save money. The first day after you ship your product, you're probably going to be faced with a requirement that your design never considered.

Instead of presuming that you can plan everything perfectly up-front, and cover every eventuality, you can take a different approach by using test-driven development (TDD). Test-driven development is a technique that involves continual development of unit tests to drive out the system's implementation. With TDD, developers always use an incremental approach to enhance the design and support new requirements. The more developers learn how to transform your design from state A to state B to accommodate new feature X, the better they get at it. By using TDD, developers can learn how to keep the design clean through refactoring at all times.

Refactoring is Everywhere

Ten years ago, refactoring was a high-risk proposition. Even without considering the safety net that unit tests provide, the tools were simply inadequate. Today, by using a powerful IDE like Eclipse, you can safely and quickly rename classes, methods, and variables many times. You can extract and move methods with little risk. Most development tools now include some support for automated, safe refactoring.

My motto today is "it's just code!" It's just code, and keeping it clean through refactoring is easy if you have tests. In fact, refactoring is an essential part of crafting software that can be maintained over the long run at reasonable cost.

About the Author

Jeff Langr is a veteran software developer with a score and more years of experience. He's authored two books and dozens of published articles on software development, including Agile Java: Crafting Code With Test-Driven Development (Prentice Hall) in 2005. You can find out more about Jeff at his site, http://langrsoft.com, or you can contact him directly at jeff@langrsoft.com.



Page 2 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel