Clean Code



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

The JUnit Framework
Even though the authors left this module in very good shape, the 
Boy Scout Rule
2
tells
us we should leave it cleaner than we found it. So, how can we improve on the original
code in Listing 15-2? 
The first thing I don’t care for is the 
f
prefix for the member variables [N6]. Today’s
environments make this kind of scope encoding redundant. So let’s eliminate all the 
f
’s.
private int contextLength;
private String expected;
private String actual;
private int prefix;
private int suffix;
Next, we have an unencapsulated conditional at the beginning of the 
compact
function
[G28].
public String compact(String message) {
if (expected == null || actual == null || areStringsEqual())
return Assert.format(message, expected, actual);
findCommonPrefix();
findCommonSuffix();
String expected = compactString(this.expected);
String actual = compactString(this.actual);
return Assert.format(message, expected, actual); 
}
This conditional should be encapsulated to make our intent clear. So let’s extract a method
that explains it.
public String compact(String message) {
if (
shouldNotCompact()
)
return Assert.format(message, expected, actual);
findCommonPrefix();
findCommonSuffix();
String expected = compactString(this.expected);
String actual = compactString(this.actual);
return Assert.format(message, expected, actual);
}
if (sfx > 0) {
int end = Math.min(s1.length() - sfx + 1 + ctxt, s1.length());
result = result + (s1.substring(s1.length() - sfx + 1, end) +
(s1.length() - sfx + 1 < s1.length() - ctxt ? "..." : ""));
}
return result;
}
}
2.
See “The Boy Scout Rule” on page 14.
Listing 15-3 (continued)
ComparisonCompator.java (defactored)


258

Download 3,58 Mb.

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