Loading ...
Sorry, an error occurred while loading the content.

50Re: [extremeperl] Refactorings?

Expand Messages
  • Adrian Howard
    Jul 30, 2002
      On Monday, July 29, 2002, at 04:02 pm, Rob Nagler wrote:

      > I'm trying to get an idea what people consider useful refactorings.
      [snip]

      Okay... here's one.

      (I don't have my Fowler to hand, so please excuse me if I wander off
      the "standard" format :-)

      ---

      TITLE: HashBecomesObject

      SYNOPSIS:

      Turn a hash that represents a separate entity into an blessed object
      that reflects it's purpose.

      $Tests->{$class}->{$method}->{num_tests} = 4;
      $Tests->{$class}->{$method}->{type}->{setup} = 1;
      $Tests->{$class}->{$method}->{type}->{teardown} = 1;

      becomes:

      my $info = bless {}, 'Test::MethodInfo';
      $info->{num_tests} = 4;
      $info->{type}->{setup} = 1;
      $info->{type}->{teardown}=1;
      $Tests->{$class}->{$method} = $info;

      MOTIVATION:

      Perl lets you quickly create complex data-structures out of
      hashes-of-hashes, often acting as simple collections.

      These can quickly become hard to comprehend, use and maintain.

      If a part of a hash-of-hashes (or a hash all by it's lonesome) makes
      sense as a separate entity, turn it into an blessed hash.

      This acts as a precursor to things like ExtractClass and allows
      HashAccessBecomesMethodCall for your new object (I'll leave
      HashAccessBecomesMethodCall, and the obvious ObjectBecomesHash as
      exercises for the reader :-)

      ----

      I'm tempted to stick up a perl refactoring wiki (or is there one
      already hiding in a corner somewhere?)... Anybody second the
      suggestion?

      Adrian
      --
      Adrian Howard <adrianh@...>
      phone: 01929 550720 fax: 0870 131 3033 www.quietstars.com
    • Show all 14 messages in this topic