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

Expand Messages
• Hello Jeff, Wednesday, June 1, 2005, 3:59:14 AM, you wrote: JG This seemed like an interesting diversion, so I gave it a shot this JG evening: Oh my deity,
Message 1 of 42 , Jun 1 1:15 AM
Hello Jeff,

Wednesday, June 1, 2005, 3:59:14 AM, you wrote:

JG> This seemed like an interesting diversion, so I gave it a shot this
JG> evening:

Oh my deity, what have I started? :-)

JG> Doing Test Driven Development, I write one test at a time (and then
JG> a few at a time), and end up with this for the final test class:

[snipped tests]

Your tests look very similar to mine. We both eliminated the obvious
duplication [extract method] in the tests to make each test case a
one-liner.

JG> And I a nice simple implementation for two byte positive integers:

final int tensDigit = value / 10;
final int onesDigit = value % 10;

I see that already our approaches are slightly different, but the
above fragment is similar to my own code and it's this bit the becomes
duplicated in an awkward way in my own solution.

JG> I can go on to bigger numbers, but I see duplication here:
JG> - The two "base 10" numbers, used to separate "tens" from "ones".
JG> - That "four" and "four-teen" and "four-ty" is duplication.

Now that's a new insight for me! It never occurred to me to
programmatically handle the appending of 'teen' or 'ty'. Mind you
won't 'Fivety' be wrong?

JG> So I *VERY AGGRESSIVELY* eliminate duplication, and get this:

[Snipped - the email formatting of the code made this tricky to
follow, but I this I got the gist]

JG> Sometimes, on rare occasion, it can be more important to write code
JG> that expresses your intention well than it is to remove all
JG> duplication.

I believe that this observation is right. BTW, the duplication that I
noticed was a kind of statement duplication. The 'teen-ness' or
'ty-ness' of the numbers didn't really bother me, since I couldn't
visualize a world where I'd have to change either.

I'll post my own code since everyone is sharing.

Regards,

Tony.

--
Tony Byrne
• ... 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 8:03 AM
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

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.