Clean Code


PrimeGenerator.java (refactored)



Download 3,58 Mb.
Pdf ko'rish
bet90/384
Sana05.04.2022
Hajmi3,58 Mb.
#530298
1   ...   86   87   88   89   90   91   92   93   ...   384
Bog'liq
Clean Code

PrimeGenerator.java (refactored)


74
Chapter 4: Comments
It is easy to argue that the first comment is redundant because it reads very much like
the
generatePrimes
function itself. Still, I think the comment serves to ease the reader into
the algorithm, so I’m inclined to leave it.
The second argument is almost certainly necessary. It explains the rationale behind
the use of the square root as the loop limit. I could find no simple variable name, nor any
different coding structure that made this point clear. On the other hand, the use of the
square root might be a conceit. Am I really saving that much time by limiting the iteration
to the square root? Could the calculation of the square root take more time than I’m saving? 
It’s worth thinking about. Using the square root as the iteration limit satisfies the old C
and assembly language hacker in me, but I’m not convinced it’s worth the time and effort
that everyone else will expend to understand it.
Bibliography
[KP78]:
Kernighan and Plaugher, 
The Elements of Programming Style
, 2d. ed., McGraw-
Hill, 1978.
private static boolean notCrossed(int i)
{
return crossedOut[i] == false;
}
private static void putUncrossedIntegersIntoResult()
{
result = new int[numberOfUncrossedIntegers()];
for (int j = 0, i = 2; i < crossedOut.length; i++)
if (notCrossed(i))
result[j++] = i;
}
private static int numberOfUncrossedIntegers()
{
int count = 0;
for (int i = 2; i < crossedOut.length; i++)
if (notCrossed(i))
count++;
return count;
}
}
Listing 4-8 (continued)
PrimeGenerator.java (refactored)


75

Download 3,58 Mb.

Do'stlaringiz bilan baham:
1   ...   86   87   88   89   90   91   92   93   ...   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