Real life and patterns

pdcawley on 2002-11-21T12:10:50

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...


cool wiki

TeeJay on 2002-11-21T14:01:31

That link is way cool...

If only I had the time to start doing all this..

Re:cool wiki

james on 2002-11-21T14:58:42

Don't do it!

I have spent far, far too many hours on that thing!

Um....

jbodoni on 2002-11-22T00:35:58

and Robert is our parent's sibling.

<pratchett>But surely Bjorn Stronginghtearm is my uncle?</pratchett>

Re:Um....

pdcawley on 2002-11-22T13:56:29

Good point. And so is Robert.