Clean Code



Download 3,58 Mb.
Pdf ko'rish
bet134/384
Sana05.04.2022
Hajmi3,58 Mb.
#530298
1   ...   130   131   132   133   134   135   136   137   ...   384
Bog'liq
Clean Code

Listing 9-2 (continued)
SerializedPageResponderTest.java (refactored)


128
Chapter 9: Unit Tests
Consider the test in Listing 9-3. I wrote this test as part of an environment control sys-
tem I was prototyping. Without going into the details you can tell that this test checks that
the low temperature alarm, the heater, and the blower are all turned on when the tempera-
ture is “way too cold.” 
There are, of course, lots of details here. For example, what is that 
tic
function all
about? In fact, I’d rather you not worry about that while reading this test. I’d rather you just
worry about whether you agree that the end state of the system is consistent with the tem-
perature being “way too cold.”
Notice, as you read the test, that your eye needs to bounce back and forth between
the name of the state being checked, and the 
sense
of the state being checked. You see
heaterState
, and then your eyes glissade left to 
assertTrue
. You see 
coolerState
and your
eyes must track left to 
assertFalse
. This is tedious and unreliable. It makes the test hard
to read.
I improved the reading of this test greatly by transforming it into Listing 9-4.
Of course I hid the detail of the 
tic
function by creating a 
wayTooCold
function. But the
thing to note is the strange string in the 
assertEquals
. Upper case means “on,” lower case
means “off,” and the letters are always in the following order: 
{heater, blower, cooler,
hi-temp-alarm, lo-temp-alarm}
.
Even though this is close to a violation of the rule about mental mapping,
3
it seems
appropriate in this case. Notice, once you know the meaning, your eyes glide across
Listing 9-3 

Download 3,58 Mb.

Do'stlaringiz bilan baham:
1   ...   130   131   132   133   134   135   136   137   ...   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