Undoing coding mistakes with refactoring

 

Summary

Wish you could turn back the clock on a development project? Methodology expert Steve Hayes shows how to correct the mistakes of the past with refactoring.

Events

IBM Technology Conference & Expo 2012
May 23, 2012

Convention Centre B2 Room at 22nd Floor, Centara Grand @ Central World, 999/99 Rama I Road, Pathumwan, Bangkok 10330

Echelon 2012
June 11 and 12, 2012

University Cultural Centre, National University of Singapore

Startup Asia Jakarta 2012
June 7 and 8, 2012

12th Floor, Annex Building, Wisma Nusantara Complex, Jl. M.H. Thamrin No. 59 Jakarta 10350, Indonesia

MMA Forum Singapore
April 23-25, 2012

Grand Hyatt Singapore

Every day, software developers make decisions that they later regret. They may be tiny decisions, such as the name to use for a variable, or they may be major decisions such as the metaphor to use for a new application, but no one gets it right every time. Even the most experienced, skilled software developers would like to be able to revisit earlier decisions, because it's the nature of their work to make decisions based on limited information, then to learn more over time.

Historically, software developers lived with the limitations of their earlier misconceptions until the burden became so great that they were compelled to rewrite the offending code. The associated costs were born by both the developers and by the business people sponsoring the project. People lived with this constraint for two reasons--fully automated testing wasn't a commonplace practice, and there were no well understood practices for making incremental improvements. In the agile development world automated testing is now well established, and the approaches to making incremental improvements have been codified as refactoring.

Defining refactoring
The word “refactoring” is used in two distinct ways, to refer to activities at two quite different levels of abstraction. At the large scale, refactoring is the process of making design improvements that leave the external behaviour of a software application unchanged. At a smaller scale, refactoring is applying a specific pattern to improve one particular aspect of the design. For example, a very simple refactoring would be to replace a piece of code that previously required a comment with an invocation of a method whose name conveys the information that used to be in the comment. While this seems like a trivial change, refactoring also encompasses more drastic change, and the result of relentlessly applying even trivial refactorings, hundreds and thousands of times, can be a radical improvement in an application's design.

Of course, the idea of improving an existing program isn't a new one--every programmer has no doubt seen things that they would have liked to change. However, most programmers (quite sensibly) shy away from changing code that they don't absolutely need to, since they worry about inadvertently breaking existing correct behavior. Taken alone, refactoring doesn't do anything to address this possibility, but almost every team that adopts refactoring as a standard part of their development process also adopts test driven development, or some equivalent practice that ensures they can automatically retest the application after a change, to verify the expected behaviour has been preserved.

Refactoring in IDE
Although all refactoring can be done manually, one step at a time, the trend for many languages is to include refactoring support in an integrated development environment (IDE). Automated refactoring is much easier to implement in languages that have automated memory management, so refactoring tools are more common for languages such as Java and C# than they are for C++. In the Java world, which I'm most familiar with, automated refactoring support is now expected in any serious IDE, and the number of supported refactorings is a point of competition between vendors.

Many programmers who use IDEs with automated refactoring support find that they allocate their design time differently. For example, rather than agonize over a class or method name when it's first required, they may use a name that's approximately right, and then rename it later when the concept has been clarified through use, confident that this will only take a few seconds. This underscores that refactoring isn't “rework”, in the sense of doing over something that was done badly the first time. Programmers who use refactoring still code to the best of their ability at each stage of development, but don't have to worry that they will have to live forever with any particular decision. This frees programmers to focus on getting something to work with a “reasonable” design and then improve it, rather than spend a lot of time implementing an “ideal” design that doesn't actually work. For experienced programmers, this probably sounds a lot like they way they already work, but with some extra rigour around activities that they're currently doing intuitively.

Refactoring entered the mainstream with the release of “Refactoring : Improving the Design of Existing Code” by Martin Fowler in 1999, and until recently refactoring has focussed primarily on the repeated application of the small scale refactorings that Martin described. In August this year, Joshua Kerievsky published “Refactoring to Patterns”, which combines the concepts of refactoring with the Gang Of Four design patterns that many programmers are already familiar with, and provides techniques for performing larger scale refactorings in a rigorous way.

This help illustrates that there's a lot to learn about refactoring, and that a thorough knowledge of refactoring can help programmers make vast improvements in the development of new applications, and the maintenance of existing ones.

Steve Hayes is the Software Development Manager at Internet Business Systems (IBS). IBS provides agile methods consulting and development services, and browser hosted solutions to the financial services industry.

Talkback

Add your opinion

In order to post a comment, you need to be registered. (Sign In or register below)

Post your comment

ZDNet Asia Live

Big data acquisitions pave way to fast, effective innovation - ZDNet Asia News http://t.co/vDZpl0lu

"Big data acquisitions pave way to fast, effective innovation" including @Vivisimo_Inc (client) in @ZDnetAsia http://t.co/yNSdPqbb

Homegrown smartphone OSes gaining favor in China: 59 Jakarta 10350, Indonesia Locally-made mobile operating syst... http://t.co/BruP98Es

RT @MDMGeek: Big data acquisitions pave way to fast, effective innovation - ZDNet Asia http://t.co/ky8YgPAn #Bigdata #analytics via @ciropuglisi

Integration, focused investments to propel Windows Phone http://t.co/6JkDa9sB

RT @AsianFashionLaw: Malaysia offers some manufacturing benefits over China http://t.co/bMquIFiX

Acquisitions in the Big Data market increasingly important to enterprises… http://t.co/Br4BkXyZ

Experience trumps content in apps monetization http://t.co/iaCY5ebX

Malaysia offers some manufacturing benefits over China http://t.co/bMquIFiX

RT @MDMGeek: Big data acquisitions pave way to fast, effective innovation - ZDNet Asia http://t.co/ky8YgPAn #Bigdata #analytics via @ciropuglisi

Thats it.Im digging up an old bus plan i wrote around acquisition of #bigdata talent. http://t.co/gpkha5A1 Any investors want2 read/discuss?

Integration, focused investments to propel Windows Phone: By Kevin Kwang , ZDNet Asia on May 23, 2012 (2 mins ag... http://t.co/aaa0Cb73

Homegrown smartphone OSes gaining favor in China http://t.co/lOBVp1T6

Homegrown smartphone OSes gaining favor in China: 59 Jakarta 10350, Indonesia Locally-made mobile operating syst... http://t.co/gHypbdIY

Integration, focused investments to propel Windows Phone - ZDNet Asia http://t.co/7sZi6Dhb

So much as we know , MTK6575 extremely integrated frequency1GHz ARM Cortex-A9 processor, the superiority of 3G / HSPA Modem, and help the...

1 day ago by y15822137359 on 5 SaaS adoption speed bumps to avoid

I reckon your view: "CRM is strategy, not software", if a company replicating the approach uses in ERP implementation into CRM, what they...

2 days ago by wykoong on Gartner: Mobile CRM gives better ROI than social

This video will teach you about the Excel fill handle but also provide you with a workook to download... http://www.youtube.com/watch?v=...

3 days ago by TradeBrother on A quick fill handle trick for Microsoft Excel

waiting...

5 days ago by eapete on What should count in a company's market value?

Boy, you've opened a can of worms now.

Wait for the rants & raves.

5 days ago by eapete on What should count in a company's market value?

I was puzzling before this whether to replicate the success formula we executed for a financial institute, and come out with a standard s...

5 days ago by wykoong on Drop the egos, copy ideas, then innovate