Clean Code


prefixIndex ); 260 Chapter 15: JUnit Internals



Download 3,58 Mb.
Pdf ko'rish
bet244/384
Sana05.04.2022
Hajmi3,58 Mb.
#530298
1   ...   240   241   242   243   244   245   246   247   ...   384
Bog'liq
Clean Code

prefixIndex
);


260
Chapter 15: JUnit Internals
compactExpected = compactString(expected);
compactActual = compactString(actual);
}
private int findCommonSuffix(
int prefixIndex
) {
int expectedSuffix = expected.length() - 1;
int actualSuffix = actual.length() - 1;
for (; actualSuffix >= prefixIndex && expectedSuffix >= prefixIndex; 
actualSuffix--, expectedSuffix--) {
if (expected.charAt(expectedSuffix) != actual.charAt(actualSuffix))
break;
}
return expected.length() - expectedSuffix;
}
I’m not really happy with this. The passing of the 
prefixIndex
argument is a bit arbitrary
[G32]. It works to establish the ordering but does nothing to explain the need for that
ordering. Another programmer might undo what we have done because there’s no indica-
tion that the parameter is really needed. So let’s take a different tack.
private void compactExpectedAndActual() {
findCommonPrefixAndSuffix
();
compactExpected = compactString(expected);
compactActual = compactString(actual);
}
private 
void findCommonPrefixAndSuffix
() {
findCommonPrefix();
int expectedSuffix = expected.length() - 1;
int actualSuffix = actual.length() - 1;
for (;
actualSuffix >= prefixIndex && expectedSuffix >= prefixIndex;
actualSuffix--, expectedSuffix--
) {
if (expected.charAt(expectedSuffix) != actual.charAt(actualSuffix))
break;
}
suffixIndex = expected.length() - expectedSuffix;
}
private 
void
findCommonPrefix() {
prefixIndex = 0;
int end = Math.min(expected.length(), actual.length());
for (; prefixIndex < end; prefixIndex++)
if (expected.charAt(prefixIndex) != actual.charAt(prefixIndex))
break;
}
We put
findCommonPrefix
and
findCommonSuffix
back the way they were, changing the
name of 
findCommonSuffix
to
findCommon
PrefixAnd
Suffix
and having it call 
findCommon-
Prefix
before doing anything else. That establishes the temporal nature of the two func-
tions in a much more dramatic way than the previous solution. It also points out how ugly
findCommonPrefixAndSuffix
is. Let’s clean it up now.
private void findCommonPrefixAndSuffix() {
findCommonPrefix();
int suffixLength = 1;


261

Download 3,58 Mb.

Do'stlaringiz bilan baham:
1   ...   240   241   242   243   244   245   246   247   ...   384




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish