sensing variables

August 19, 2006


Getting Serious about Preserving Behavior
is a great little article by Mike feathers, in which he introduces a refactoring trick he calls sensing variables. The gist is this:

You want to refactor some code, but it’s too ugly to test as it currently stands. So there’s a danger that the refactoring will somehow inadvertently change behaviour. Mike suggestes introducing a new variable outside of the scope of the code under test, and then altering the code to report state into that variable. Now, write tests that check the values in the variable, under a variety of conditions. Those tests will tell us whether our subsequent refactorings have changed the variable’s value - in other words, whether we have accidentally changed some deep behaviour somewhere. Very nice.

(The technique is also covered in Mike’s book Working Effectively with Legacy Code - see Chapter 22 “I Need to Change a Monster Method and I Can’t Write Tests for It”)

Entry Filed under: refactoring, tdd. .

Leave a Comment

Required

Required, hidden

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Trackback this post  |  Subscribe to the comments via RSS Feed


about

kr

I'm Kevin Rutherford, from Macclesfield in the UK. I'm an independent agile software developmer and mentor, specialising in test-driven development, agile, lean and the theory of constraints.

View Kevin Rutherford's profile on LinkedIn


Bookmark this Page

AddThis Social Bookmark Button Add to Technorati Favorites

Category Cloud

agile agilenorth blogging books carnival coaching conference dailystandup hexagonalarchitecture jidoka kaizen language lean leansoftwaredevelopment metaphor metrics muda planning qualitycontrol refactoring respect ruby scrum tdd theory of constraints thinkingtools throughput toc unittesting xp

Top Posts

archives

Links