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

Re: [XP] Duplication vs Intention Revealing code (was: Zen Refactoring)

Expand Messages
  • Tony Byrne
    Hello Jeff, Okay, here s my Perl solution to the problem. I ve tweaked the layout in the hope that it ll be more email friendly. You ll see the duplication
    Message 1 of 42 , Jun 1, 2005
    • 0 Attachment
      Hello Jeff,

      Okay, here's my Perl solution to the problem. I've tweaked the layout
      in the hope that it'll be more email friendly. You'll see the
      duplication that I was talking about in the modular arithmetic. I've
      left out the tests, since they are pretty obvious. Comments on missed
      opportunities etc., are most welcome.

      ====================== CUT ==========================

      my %integerLookup =
      (

      0 => 'zero', 1 => 'one', 2 => 'two', 3 => 'three',
      4 => 'four', 5 => 'five',6 => 'six', 7 => 'seven',
      8 => 'eight',9 => 'nine',

      10 => 'ten', 11 => 'eleven', 12 => 'twelve',
      13 => 'thirteen', 14 => 'fourteen', 15 => 'fifteen',
      16 => 'sixteen', 17 => 'seventeen', 18 => 'eighteen',
      19 => 'nineteen',

      20 => 'twenty', 30 => 'thirty', 40 => 'forty', 50 => 'fifty',
      60 => 'sixty', 70 => 'seventy', 80 => 'eighty',
      90 => 'ninety'
      );

      my %orderLookup =
      (
      100 => 'hundred',
      1000 => 'thousand',
      1000000 => 'million',
      1000000000 => 'billion'
      );

      sub convert
      {
      my ($self, $integer) = @_;

      return $integerLookup{$integer}
      if exists $integerLookup{$integer};

      if ($integer < 100)
      {
      return $self->_convertSubOneHundred($integer);
      }
      else
      {
      return $self->_convertHighOrder($integer);
      }
      }

      sub _convertSubOneHundred
      {
      my ($self, $integer) = @_;
      my $ones = $integer % 10;
      my $tens = $integer - $ones;
      return $self->convert($tens)
      . " " . $self->convert($ones);
      }

      sub _convertHighOrder
      {
      my ($self, $integer) = @_;

      my $order = $self->_orderOfInteger($integer);

      my $numberOfOrder = int($integer / $order);
      my $remainder = $integer % $order;

      return $self->convert($numberOfOrder)
      . " " . $self->_suffixForOrder($order)
      . $self->_convertRemainder($remainder);

      }

      sub _convertRemainder
      {
      my ($self, $remainder) = @_;
      return '' unless $remainder;
      return $self->_joinStringForRemainder($remainder)
      . $self->convert($remainder);
      }

      sub _joinStringForRemainder
      {
      my ($self, $remainder) = @_;
      my $joinString = " and ";
      $joinString = ", " if $remainder > 99;
      return $joinString;
      }

      sub _orderOfInteger
      {
      my ($self, $integer) = @_;
      foreach my $order (reverse sort keys %orderLookup)
      {
      return $order if $integer >= $order;
      }
      }

      sub _suffixForOrder
      {
      my ($self, $order) = @_;
      return $orderLookup{$order};
      }

      Regards,

      Tony.

      --
      Tony Byrne
    • Alex Chaffee
      ... One way I ve found to avoid this is to use IntelliJ s awkwardly named Replace Method Code Duplicates refactoring. It looks for all blocks of code that
      Message 42 of 42 , Jun 4, 2005
      • 0 Attachment
        On 6/1/05, Elizabeth Keogh <ekeogh@...> wrote:
        > I've extracted a method from a test, then applied the extraction to a
        > similar chunk in another test, but missed out one of the parameters which
        > made it different.

        One way I've found to avoid this is to use IntelliJ's awkwardly named
        "Replace Method Code Duplicates" refactoring. It looks for all blocks
        of code that have the same structure as the contents of the method,
        and replaces them with a call to that method. It's the natural
        successor to Extract Method -- perhaps a better name would be "Apply
        Method". It's not perfect, but when it works it's a thing of beauty.

        Yesterday it saved us from just the problem you mentioned. We tried to
        apply it and it *didn't* select the code we thought it would; it
        turned out we'd missed a parameter. Once we introduced that parameter
        it worked. If we'd been doing this by hand we'd have made your
        mistake.

        - A
      Your message has been successfully submitted and would be delivered to recipients shortly.