Clean Code


private String compactExpected



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

private String compactExpected;
private String compactActual;
...
public String formatCompactedComparison(String message) {
if (canBeCompacted()) {
compactExpectedAndActual();
return Assert.format(message, compactExpected, compactActual);
} else {


259
The JUnit Framework
return Assert.format(message, expected, actual);
}
}
private void 
compactExpectedAndActual
() {
findCommonPrefix();
findCommonSuffix();
compactExpected = compactString(expected);
compactActual = compactString(actual);
}
Notice that this required us to promote 
compactExpected
and
compactActual
to member
variables. I don’t like the way that the last two lines of the new function return variables,
but the first two don’t. They aren’t using consistent conventions [G11]. So we should
change
findCommonPrefix
and 
findCommonSuffix
to return the prefix and suffix values.
private void compactExpectedAndActual() {
prefixIndex
findCommonPrefix();
suffixIndex = 
findCommonSuffix();
compactExpected = compactString(expected);
compactActual = compactString(actual);
}
private 
int
findCommonPrefix() {
int
prefix
Index
= 0;
int end = Math.min(expected.length(), actual.length());
for (; prefix
Index
< end; prefix
Index
++) {
if (expected.charAt(prefix
Index
) != actual.charAt(prefix
Index
))
break;
}
return prefixIndex;
}
private 
int
findCommonSuffix() {
int expectedSuffix = expected.length() - 1;
int actualSuffix = actual.length() - 1;
for (; actualSuffix >= prefix
Index
&& expectedSuffix >= prefixIndex;
actualSuffix--, expectedSuffix--) {
if (expected.charAt(expectedSuffix) != actual.charAt(actualSuffix))
break;
}
return
expected.length() - expectedSuffix;
}
We should also change the names of the member variables to be a little more accurate
[N1]; after all, they are both indices. 
Careful inspection of 
findCommonSuffix
exposes a 
hidden temporal coupling 
[G31]; it
depends on the fact that 
prefixIndex
is calculated by 
findCommonPrefix
. If these two func-
tions were called out of order, there would be a difficult debugging session ahead. So, to
expose this temporal coupling, let’s have 
findCommonSuffix
take the 
prefixIndex
as an
argument.
private void compactExpectedAndActual() {
prefixIndex = findCommonPrefix();
suffixIndex = findCommonSuffix(

Download 3,58 Mb.

Do'stlaringiz bilan baham:
1   ...   239   240   241   242   243   244   245   246   ...   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