Clean Code



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

The JUnit Framework
for (; !suffixOverlapsPrefix(suffixLength); suffixLength++) {
if (charFromEnd(expected, suffixLength) != 
charFromEnd(actual, suffixLength))
break;
}
suffixIndex = suffixLength;
}
private char charFromEnd(String s, int i) {
return s.charAt(s.length()-i);}
private boolean suffixOverlapsPrefix(int suffixLength) {
return actual.length() - suffixLength < prefixLength ||
expected.length() - suffixLength < prefixLength;
}
This is much better. It exposes that the 
suffixIndex
is really the length of the suffix
and is not well named. The same is true of the 
prefixIndex
, though in that case “index” and
“length” are synonymous. Even so, it is more consistent to use “length.” The problem is
that the 
suffixIndex
variable is not zero based; it is 1 based and so is not a true length. This
is also the reason that there are all those 
+1
s in 
computeCommonSuffix
[G33]. So let’s fix that.
The result is in Listing 15-4.
Listing 15-4 
ComparisonCompactor.java (interim)
public class ComparisonCompactor {
...
private int 
suffixLength
;
...
private void findCommonPrefixAndSuffix() {
findCommonPrefix();
suffixLength = 0;
for (; !suffixOverlapsPrefix(suffixLength); suffixLength++) {
if (charFromEnd(expected, suffixLength) != 
charFromEnd(actual, suffixLength))
break;
}
}
private char charFromEnd(String s, int i) {
return s.charAt(s.length() - i 
- 1
);
}
private boolean suffixOverlapsPrefix(int suffixLength) {
return actual.length() - suffixLength 
<=
prefixLength ||
expected.length() - suffixLength 
<=
prefixLength;
}
...
private String compactString(String source) {
String result = 
DELTA_START + 
source.substring(prefixLength, source.length() - 
suffixLength
) + 
DELTA_END;
if (prefixLength > 0)
result = computeCommonPrefix() + result;


262

Download 3,58 Mb.

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