Effective Java



Download 2,19 Mb.
Pdf ko'rish
bet235/341
Sana11.07.2022
Hajmi2,19 Mb.
#776765
1   ...   231   232   233   234   235   236   237   238   ...   341
Bog'liq
Effective Java



ITEM 57: MINIMIZE THE SCOPE OF LOCAL VARIABLES
263
code compiles without error and runs without throwing an exception, but it does
the wrong thing. Instead of iterating over 
c2
, the second loop terminates immedi-
ately, giving the false impression that 
c2
is empty. Because the program errs
silently, the error can remain undetected for a long time.
If a similar copy-and-paste error were made in conjunction with either of the
for
loops (for-each or traditional), the resulting code wouldn’t even compile. The
element (or iterator) variable from the first loop would not be in scope in the
second loop. Here’s how it looks with the traditional 
for
loop:
for (Iterator i = c.iterator(); i.hasNext(); ) {
Element e = i.next();
... // Do something with e and i
}
...
// Compile-time error - cannot find symbol i
for (Iterator i2 = c2.iterator(); 
i
.hasNext(); ) {
Element e2 = i2.next();
... // Do something with e2 and i2
}
Moreover, if you use a 
for
loop, it’s much less likely that you’ll make the
copy-and-paste error because there’s no incentive to use different variable names
in the two loops. The loops are completely independent, so there’s no harm in
reusing the element (or iterator) variable name. In fact, it’s often stylish to do so. 
The 
for
loop has one more advantage over the 
while
loop: it is shorter, which
enhances readability.
Here is another loop idiom that minimizes the scope of local variables:
for (int i = 0, n = expensiveComputation(); i < n; i++) {
... // Do something with i;
}
The important thing to notice about this idiom is that it has 
two
loop variables, 
i
and 
n
, both of which have exactly the right scope. The second variable, 
n
, is used
to store the limit of the first, thus avoiding the cost of a redundant computation in
every iteration. As a rule, you should use this idiom if the loop test involves a
method invocation that is guaranteed to return the same result on each iteration.
A final technique to minimize the scope of local variables is to 

Download 2,19 Mb.

Do'stlaringiz bilan baham:
1   ...   231   232   233   234   235   236   237   238   ...   341




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