Browse Groups

• ## Re: Duplication vs Intention Revealing code

(3)
• NextPrevious
• ... Actually, I decided that the duplication between SimpleLabelFinder and TensLabelFinder was worth eliminating, which yielded LookupLabelFinder: struct
Message 1 of 3 , Jun 1, 2005
View Source
Anthony Williams <anthony_w.geo@...> writes:

> I'll have a go at the exercise later, and post my code.

Actually, I decided that the duplication between SimpleLabelFinder and
TensLabelFinder was worth eliminating, which yielded LookupLabelFinder:

struct LookupLabelFinder:
MagLabelFinder
{
int const magVal;
std::string const* strings;
unsigned const numStrings;

template<unsigned numStrings_>
LookupLabelFinder(int magVal_,std::string const (&strings_)[numStrings_]):
magVal(magVal_),strings(strings_),numStrings(numStrings_)
{}

int magnitude() const
{
return magVal;
}
std::string label(int magValue) const
{
return strings[magValue];
}
bool canHandle(int value) const
{
return value<(magVal*numStrings);
}
};

MagLabelFinderPtr findMagnitude(int i)
{
static std::string const simpleStrings[]={
"zero","one","two","three","four","five","six","seven","eight","nine",
"ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"
};

static std::string const tenStrings[]={
"","","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"
};

MagLabelFinderPtr const magnitudes[]={
MagLabelFinderPtr(new MinusLabelFinder),
MagLabelFinderPtr(new LookupLabelFinder(1,simpleStrings)),
MagLabelFinderPtr(new BigMagLabelFinder(1000000000,"billion")),
MagLabelFinderPtr(new BigMagLabelFinder(1000000,"million")),
MagLabelFinderPtr(new BigMagLabelFinder(1000,"thousand")),
MagLabelFinderPtr(new BigMagLabelFinder(100,"hundred")),
MagLabelFinderPtr(new LookupLabelFinder(10,tenStrings)),
};

for(unsigned magIndex=0;magIndex<arraySize(magnitudes);++magIndex)
{
if(magnitudes[magIndex]->canHandle(i))
{
return magnitudes[magIndex];
}
}
}
Your message has been successfully submitted and would be delivered to recipients shortly.
• Changes have not been saved
Press OK to abandon changes or Cancel to continue editing
• Your browser is not supported
Kindly note that Groups does not support 7.0 or earlier versions of Internet Explorer. We recommend upgrading to the latest Internet Explorer, Google Chrome, or Firefox. If you are using IE 9 or later, make sure you turn off Compatibility View.