I'm a writer, programmer, web developer, and entrepreneur. Preona is my current startup that began its life as the team developing Twitulater. Our goal is to create a set of applications for the emerging Synaptic Web, which would rank real-time information streams in near real time, all along reading its user behaviour and understanding how to intelligently react to it. Swizec is a DZone MVB and is not an employee of DZone and has posted 66 posts at DZone. You can read more from them at their website. View Full User Profile

What Refactoring Is, And What It Isn’t

04.18.2012
| 6456 views |
  • submit to reddit

Nikos Maravitsas writes about What refactoring is and what it isn’t over at Java Code Geeks.

Sometimes a programmer will come to me and explain that they don’t like the design of something and that “we’re gonna need to do a whole bunch of refactoring” to make it right. Oh Oh. This doesn’t sound good. And it doesn’t sound like refactoring either….

But that other guy before me was stupid! He did it wrong! All wrong! Wah wah wah …

Definitely something I’ve been guilty of throughout my coding career. Even went so far as justifying it in a blogpost where I asked Are you a boyscout coder? The gist of my question was whether clients should be charged for refactoring work or not.

More importantly, what level of refactoring work is on par for getting any work done in a foreign codebase and what’s taking aesthetics too far.

Refactoring, as originally defined by Martin Fowler and Kent Beck, is

A change made to the internal structure of software to make it easier to understand and cheaper to modify without changing its observable behavior… It is a disciplined way to clean up code that minimizes the chances of introducing bugs.

/…/

Refactoring is supposed to be a practice that supports making changes to code. You refactor code before making changes, so that you can confirm your understanding of the code and make it easier and safer to put your change in.

And that’s the gist of it. You are allowed to refactor as much as you want so long as the outside interface doesn’t change and you make the code demonstrably easier to work with in the future. I think any client would be happy to pay for making their development process cheaper.

Nikos goes on to explain how to refactor:

Refactoring is simple. Protect yourself from making mistakes by first writing tests where you can. Make structural changes to the code in small, independent and safe steps, and test the code after each of these steps to ensure that you haven’t changed the behavior – it still works the same, just looks different.

But there’s a difference between refactoring things as you go, just polishing up some function names, changing a variable here and there, making things more readable. Those little things you have to get in order to make everyone’s lives easier before you implement a new feature … and changing the architecture of the whole system.

“Large Scale Refactoring” changes can be ugly. They can take weeks or months (or years) to complete, requiring changes to many different parts of the code. They need to be broken down and released in multiple steps, requiring temporary scaffolding and detours, especially if you are working in short Agile sprints.

This isn’t refactoring!

Call redesigning and/or rewriting what it is – redesigning. If there’s a business case for it, go ahead, if it makes sense to redesign now before things get messy, go ahead. For the sake of yourself and everyone around you, just don’t do these things in the name of Refactoring.

Published at DZone with permission of Swizec Teller, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Jim Bird replied on Sat, 2012/04/21 - 3:56pm

I would appreciate this repost of a repost if it was attributed correctly. You lifted a repost of one of my blog posts

 http://swreflections.blogspot.ca/2012/04/what-refactoring-is-and-what-it-isnt.html

from Java Code geeks, attributed the post to somebody else (Nikos, who syndicated my original post with my permission), added a few comments of your own and reposted it here under your name. I'm free with my content, but not that free :-(

 

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.