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

Expand Messages
• From: Jeff Grigg To: extremeprogramming@yahoogroups.com
Message 1 of 10 , May 31 10:30 PM
From: "Jeff Grigg" <jeffgrigg.at.charter.net@...>
To: "extremeprogramming@yahoogroups.com"
<extremeprogramming.at.yahoogroups.com@...>
Sent: Tuesday, May 31, 2005 8:59 PM
Subject: [XP] Duplication vs Intention Revealing code (was: Zen Refactoring)

> --- Tony Byrne wrote:
>> As a TDD kata, I recently implemented a small program to
>> convert integers to a string representation in words.
>> (e.g. 1 --> one, 55 --> fifty-five). For me, this problem
>> contains one example of duplication which was easy to
>> make disappear once the duplicated code was made similar,
>> and one where no matter how I look at it, I can't make
>> the code similar enough to make the duplication disappear.
>> [...]
>
> This seemed like an interesting diversion, so I gave it a shot this
> evening:

---

I wind up with a very different result, I suspect because I started
with ones and then stopped to think. This is, of course, in
Python.

The tests are a bit obvious, so I'm going to skip them. The
first interesting result comes after tests for 0 to 9:

class ConvertNumbersToEnglish:
def numberToEnglish(self, number):
return self.digitsArray(number)

digitArray = ["zero", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine"]

It doesn't seem like it could get any simpler.

Looking at two digit numbers, it seems like a lot of them

So let's feed in, say 45 and 63 as test cases. I get this:

def numberToEnglish(self, number):
tens, digits = divmod(number, 10)
if tens == 0:
return self.digitArray[digits]
else:
return "%sty-%s" % (self.digitArray[tens],
self.digitArray[digits])

There's a bit of duplication, but I don't see how to get rid of it right
now.

There still seem to be three different special cases. A units digit of zero,
a tens digit of one, and tens digits of 2, 3, and 8. Let's tackle the units
digit of 0 first.

I feed in 40 and 60, and get this as a result:

def numberToEnglish(self, number):
tens, digits = divmod(number, 10)
if tens == 0:
return self.digitArray[digits]
elif digits == 0:
return "%sty" % digitArray[tens]
else:
return "%sty-%s" % (self.digitArray[tens],
self.digitArray[digits])

There's more duplication, but let's leave it for a while.

Let's hit twenty, thirty and eighty next. I happen to like arrays, so
we get this:

def numberToEnglish(self, number):
tens, digits = divmod(number, 10)
if tens == 0:
return self.digitArray[digits]
elif digits == 0:
return "%s" % tensArray[tens]
else:
return "%s-%s" % (self.tensArray[tens], self.digitArray[digits])

tensArray = ["", "ten", "twenty", "thirty", "fourty", "fifty",
"sixty", "seventy", "eighty", "ninety"]

digitArray = ["zero", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine"]

However, now we've definitely got some duplication between the
two arrays. If we use the digit array as a base, and go back to
applying the 'ty' in the routine, we can simply copy five of the
numbers from the digit array to the tens array. Now what we've got
is:

class ConvertNumbersToEnglish:
def __init__(self):
for i in range(10):
if tensArray[i] == None:
tensArray[i] = digitArray[i]

tensArray = ["", "ten", "twen", "thir", None, None,
None, None, "eigh", None]

digitArray = ["zero", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine"]

def numberToEnglish(self, number):
tens, digits = divmod(number, 10)
if tens == 0:
return self.digitArray[digits]
elif digits == 0:
return "%sty" % tensArray[tens]
else:
return "%sty-%s" % (self.tensArray[tens],
self.digitArray[digits])

We've now got everything except a tens digit of one. So let's feed in
13, 16 and 18. We notice something really interesting...

def numberToEnglish(self, number):
tens, digits = divmod(number, 10)
if tens == 0:
return self.digitArray[digits]
elif tens == 1:
return "%steen" % self.tensArray[digits]
elif digits == 0:
return "%sty" % tensArray[tens]
else:
return "%sty-%s" % (self.tensArray[tens],
self.digitArray[digits])

Almost there. This will handle everything except 10, 11 and 12. We'll
get teen, oneteen and twoteen. So let's put in tests for those three and
we get (after reducing the if-elif to an array):

class ConvertNumbersToEnglish:
def __init__(self):
for i in range(10):
if tensArray[i] == None:
tensArray[i] = digitArray[i]

tensArray = ["", "ten", "twen", "thir", None, None,
None, None, "eigh", None]

digitArray = ["zero", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine"]

def numberToEnglish(self, number):
tens, digits = divmod(number, 10)
if tens == 0:
return self.digitArray[digits]
elif tens == 1:
if digits < 3:
return self.teenArray[digits]
else:
return "%steen" % self.tensArray[digits]
elif digits == 0:
return "%sty" % tensArray[tens]
else:
return "%sty-%s" % (self.tensArray[tens],
self.digitArray[digits])

teenArray = ["ten", "eleven", "twelve"]

There's still some duplication here, but I don't think that
removing it will improve either clarity or modifiability.
Adding hundreds and thousands should be fairly
straightforward. So let's add hundreds. The first cut is:

def numberToEnglish(self, number):
tens, digits = divmod(number, 10)
hundreds = divmod(tens, 10)
if number == 0:
return "zero"
if tens == 0:
result = self.digitArray[digits]
elif tens == 1:
if digits < 3:
result = self.teenArray[digits]
else:
result = "%steen" % self.tensArray[digits]
elif digits == 0:
result = "%sty" % tensArray[tens]
else:
result = "%sty-%s" % (self.tensArray[tens],
self.digitArray[digits])
if hundreds == 0:
return result
return "%s hundred and %s" % (self.digitArray[hundreds], result)

That turned out to be pretty easy. Thousands aren't going to be quite
so easy, but it's getting quite late, so I'm going to wrap it up.

John Roth
• I have always heard and taught the rules in this priority order 1. Passes all the tests 2. Expresses all the intention 3. No duplication 4. Fewest classes &
Message 2 of 10 , Jun 1, 2005
I have always heard and taught the rules in this priority order

1. Passes all the tests
2. Expresses all the intention
3. No duplication
4. Fewest classes & Methods

So for those (admittedly rare) occasions where you have to choose between
duplication or epxressiveness, expressiveness /always/ wins. Where have you
seen it written that "No Duplication" comes before "Expresses Intention"?

I believe in the XPE2E book the rules have been re-phrased as
1.. Appropriate for the intended audience
2.. Communicative
3.. Factored
4.. Minimal
Again, "communicative" comes /before/ "factored".

Jeff Nielsen
Digital Focus
www.digitalfocus.com

From: "Jeff Grigg" <jeffgrigg@...>

> So...
> I think we've hit an illustrative corner case where the
> expressiveness rule trumps the no duplication rule. It doesn't
> happen very often, but I think it can happen from time to time that
> you have to reverse Kent's rules 2 and 3:
>
> 1. Runs all tests.
> 2. No Duplication.
> 3. Expresses Intention.
> 4. Fewest Classes and Methods.
>
> Sometimes, on rare occasion, it can be more important to write code
> that expresses your intention well than it is to remove all
> duplication.
>
> (We hit this in the Bowling Game example, as I recall. But this
> seems like a more compelling example.)
>
>
> Thanks!
> - jeff
>
>
>
>
> To Post a message, send it to: extremeprogramming@...
>
> To Unsubscribe, send a blank message to:
> extremeprogramming-unsubscribe@...
>
>
>
>
>
>
>
• ... Beck, /Extreme Programming Explained/, first edition, page 57. Ron Jeffries www.XProgramming.com Testing quality into a program is like spinning straw into
Message 3 of 10 , Jun 1, 2005
Around Wednesday, June 1, 2005, 10:08:32 PM, Jeff Nielsen wrote:

> So for those (admittedly rare) occasions where you have to choose between
> duplication or epxressiveness, expressiveness /always/ wins. Where have you
> seen it written that "No Duplication" comes before "Expresses Intention"?

Beck, /Extreme Programming Explained/, first edition, page 57.

Ron Jeffries
www.XProgramming.com
Testing quality into a program is like spinning straw into gold.
-- George Cameron.
• Another good reason to buy the second edition of XPE :-) Kent Beck Three Rivers Institute
Message 4 of 10 , Jun 1, 2005
Another good reason to buy the second edition of XPE :-)

Kent Beck
Three Rivers Institute

> -----Original Message-----
> From: extremeprogramming@yahoogroups.com
> [mailto:extremeprogramming@yahoogroups.com] On Behalf Of Ron Jeffries
> Sent: Wednesday, June 01, 2005 8:57 PM
> To: extremeprogramming@yahoogroups.com
> Subject: Re: [XP] Duplication vs Intention Revealing code
> (was: Zen Refactoring)
>
> Around Wednesday, June 1, 2005, 10:08:32 PM, Jeff Nielsen wrote:
>
> > So for those (admittedly rare) occasions where you have to
> choose between
> > duplication or epxressiveness, expressiveness /always/
> wins. Where have you
> > seen it written that "No Duplication" comes before
> "Expresses Intention"?
>
> Beck, /Extreme Programming Explained/, first edition, page 57.
>
> Ron Jeffries
> www.XProgramming.com
> Testing quality into a program is like spinning straw into gold.
> -- George Cameron.
>
>
>
> To Post a message, send it to: extremeprogramming@...
>
> To Unsubscribe, send a blank message to:
> extremeprogramming-unsubscribe@...
>
>
>
>
>
>
>
>
• ... Abso-damn-lutely, and not even close to the BEST reason. The second addition has a kind of depth and richness that takes it beyond the first. I do still
Message 5 of 10 , Jun 2, 2005
On Thursday, June 2, 2005, at 2:50:58 AM, Kent Beck wrote:

> Another good reason to buy the second edition of XPE :-)

Abso-damn-lutely, and not even close to the BEST reason. The second
addition has a kind of depth and richness that takes it beyond the
first.

I do still love the first as much as ever. I kind of wish the second
edition had been called "Extreme Programming 2005" or something,
because I find such value in the first one as well. I sure can
understand, though, a desire to improve the first. And it has a sort
of iterative aspect that one might like ...

Ron Jeffries
www.XProgramming.com
I must create a system, or be enslaved by another man's;
I will not reason and compare; my business is to create. --William Blake
• ... XPE2 is so different from XPE1 I wouldn t call it the second edition of the same book. I ve been calling it Extreme Programming for Managers, and
Message 6 of 10 , Jun 2, 2005
> --- Kent Beck wrote:
>> Another good reason to buy the second edition of XPE :-)

--- Ron Jeffries <ronjeffries@X...> wrote:
> [...] The second addition has a kind of depth and richness
> that takes it beyond the first.

XPE2 is so different from XPE1 I wouldn't call it the second edition
of the same book. I've been calling it "Extreme Programming for
Managers," and recommending it to same. It's a different book, not
just a minor update or enhancement.
• The first edition had two listings of the rules of simple design, with different orders. Bonus points goes to whomever tells us the page number of the second
Message 7 of 10 , Jun 2, 2005
The first edition had two listings of the rules of simple design, with
different orders. Bonus points goes to whomever tells us the page
number of the second listing.

On Jun 1, 2005, at 8:56 PM, Ron Jeffries wrote:

> Around Wednesday, June 1, 2005, 10:08:32 PM, Jeff Nielsen wrote:
>
>> So for those (admittedly rare) occasions where you have to choose
>> between
>> duplication or epxressiveness, expressiveness /always/ wins. Where
>> have you
>> seen it written that "No Duplication" comes before "Expresses
>> Intention"?
>
> Beck, /Extreme Programming Explained/, first edition, page 57.
>
--
C. Keith Ray
<http://homepage.mac.com/keithray/blog/index.html>
<http://homepage.mac.com/keithray/xpminifaq.html>
<http://homepage.mac.com/keithray/resume2.html>
• ... 109. I have them both marked with those little pagepoint thingies. Ron Jeffries www.XProgramming.com 2 + 2 = 5, for sufficiently large values of 2.
Message 8 of 10 , Jun 2, 2005
On Thursday, June 2, 2005, at 10:02:25 AM, Keith Ray wrote:

> The first edition had two listings of the rules of simple design, with
> different orders. Bonus points goes to whomever tells us the page
> number of the second listing.

109. I have them both marked with those little pagepoint thingies.

Ron Jeffries
www.XProgramming.com
2 + 2 = 5, for sufficiently large values of 2.
• Both first edition and second edition are good books - but neither stands completely on its own. Will it continue to be possible to buy the first edition?
Message 9 of 10 , Jun 6, 2005
Both first edition and second edition are good books - but neither
stands completely on its own.

Will it continue to be possible to buy the first edition?

Regards, Ben

Kent Beck wrote:

>Another good reason to buy the second edition of XPE :-)
>
>Kent Beck
>Three Rivers Institute
>
>
>
>>-----Original Message-----
>>From: extremeprogramming@yahoogroups.com
>>[mailto:extremeprogramming@yahoogroups.com] On Behalf Of Ron Jeffries
>>Sent: Wednesday, June 01, 2005 8:57 PM
>>To: extremeprogramming@yahoogroups.com
>>Subject: Re: [XP] Duplication vs Intention Revealing code
>>(was: Zen Refactoring)
>>
>>Around Wednesday, June 1, 2005, 10:08:32 PM, Jeff Nielsen wrote:
>>
>>
>>
>>>So for those (admittedly rare) occasions where you have to
>>>
>>>
>>choose between
>>
>>
>>>duplication or epxressiveness, expressiveness /always/
>>>
>>>
>>wins. Where have you
>>
>>
>>>seen it written that "No Duplication" comes before
>>>
>>>
>>"Expresses Intention"?
>>
>>Beck, /Extreme Programming Explained/, first edition, page 57.
>>
>>Ron Jeffries
>>www.XProgramming.com
>>Testing quality into a program is like spinning straw into gold.
>> -- George Cameron.
>>
>>
>>
>>To Post a message, send it to: extremeprogramming@...
>>
>>To Unsubscribe, send a blank message to:
>>extremeprogramming-unsubscribe@...
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>
>
>
>To Post a message, send it to: extremeprogramming@...
>
>To Unsubscribe, send a blank message to: extremeprogramming-unsubscribe@...
>
>
>
>
>
>
>
>
>
• Ben, I don t know precisely when the first edition will go out of print. Eventually, though, the first edition will no longer be sold. Kent Beck Three Rivers
Message 10 of 10 , Jun 7, 2005
Ben,

I don't know precisely when the first edition will go out of print.
Eventually, though, the first edition will no longer be sold.

Kent Beck
Three Rivers Institute

> -----Original Message-----
> From: extremeprogramming@yahoogroups.com
> [mailto:extremeprogramming@yahoogroups.com] On Behalf Of BenAveling
> Sent: Monday, June 06, 2005 6:27 AM
> To: extremeprogramming@yahoogroups.com
> Subject: Re: [XP] Duplication vs Intention Revealing code
> (was: Zen Refactoring)
>
>
> Both first edition and second edition are good books - but neither
> stands completely on its own.
>
> Will it continue to be possible to buy the first edition?
>
> Regards, Ben
>
> Kent Beck wrote:
>
> >Another good reason to buy the second edition of XPE :-)
> >
> >Kent Beck
> >Three Rivers Institute
> >
> >
> >
> >>-----Original Message-----
> >>From: extremeprogramming@yahoogroups.com
> >>[mailto:extremeprogramming@yahoogroups.com] On Behalf Of
> Ron Jeffries
> >>Sent: Wednesday, June 01, 2005 8:57 PM
> >>To: extremeprogramming@yahoogroups.com
> >>Subject: Re: [XP] Duplication vs Intention Revealing code
> >>(was: Zen Refactoring)
> >>
> >>Around Wednesday, June 1, 2005, 10:08:32 PM, Jeff Nielsen wrote:
> >>
> >>
> >>
> >>>So for those (admittedly rare) occasions where you have to
> >>>
> >>>
> >>choose between
> >>
> >>
> >>>duplication or epxressiveness, expressiveness /always/
> >>>
> >>>
> >>wins. Where have you
> >>
> >>
> >>>seen it written that "No Duplication" comes before
> >>>
> >>>
> >>"Expresses Intention"?
> >>
> >>Beck, /Extreme Programming Explained/, first edition, page 57.
> >>
> >>Ron Jeffries
> >>www.XProgramming.com
> >>Testing quality into a program is like spinning straw into gold.
> >> -- George Cameron.
> >>
> >>
> >>
> >>To Post a message, send it to: extremeprogramming@...
> >>
> >>To Unsubscribe, send a blank message to:
> >>extremeprogramming-unsubscribe@...
> >>
> >>
> >>
> >>
> >>
> >>
> >>
> >>
> >>
> >>
> >
> >
> >
> >To Post a message, send it to: extremeprogramming@...
> >
> >To Unsubscribe, send a blank message to:
> extremeprogramming-unsubscribe@...
> >
> >
> >
> >
> >
> >
> >
> >
> >
>
>
> To Post a message, send it to: extremeprogramming@...
>
> To Unsubscribe, send a blank message to:
> extremeprogramming-unsubscribe@...
>