Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

pdcawley (485)

  (email not shown publicly)
AOL IM: pdcawley (Add Buddy, Send Message)

Journal of pdcawley (485)

Thursday November 21, 2002
07:10 AM

Real life and patterns

[ #9055 ]

So, I have this funky transaction object, with all sorts of state dependent behaviour. There's loads of methods with a very similar nest of conditional logic at the top.

So, being a good boy, I add a 'state' attribute to my class and start moving things over to my State classes. I move the first couple of methods over, run the tests and a huge pile of 'em fail. Big style.

So, I look through the code and find a repeated:

croak "some message" unless $self->{foo}{bar};

and the tests are failing 'cos they're dying with that self same message. So, we 'Extract Method' on that $self->{foo}{bar} into a is_initialized method, rewrite is_initialized to do $self->state->is_initialized, and Robert is our parent's sibling.

Of course, this refactoring would have been so much easier if we already had an Cis_initialized method. Just goes to show that, no matter how small the unit of repetition is, the DRY Principle applies. It also goes some way to showing that there's something in Design Patterns, but that's an argument for another day...

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.