Bloch8 - EffectiveJava: GeneralProgramming...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
Effective Java:   General Programming Last Updated: Spring 2009
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
General Programming 2 Agenda Material From Joshua Bloch Effective Java  2 nd  Edition Cover Items 45-56 “General Programming” Chapter Bottom Line:   Nuts and bolts of the Java language Treatment of two extralinguistic language  facilities Optimization and naming conventions
Background image of page 2
General Programming 3 Item 45: Minimize the Scope of  Local Variables Similar to Item 13: “Minimize the accessibility of classes and  members” Some older languages (eg C) require declarations at the beginning  of a block A habit worth breaking Important Case: Prefer  for  loops to  while  loops // Preferred idiom for iterating over a collection for (Element e : C) {    doSomething(e);      // Note the lack (of an  explicit) cast } // No for-each loop or generics before release 1.5 for (Iterator i = c.iterator(); i.hasNext(); ) {    doSomething( (Element) i.next());    // Note the  cast
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
General Programming 4 More Item 45: Problems with  while  Loops Sample problem with  while  loops  Problem disappears with local declarations in a  for  loop // Spot the bug? Iterator<Element> i = c.iterator(); while (i.hasNext()) {    doSomething(i.next()); } Iterator<Element> i2 = c2.iterator(); while (i.hasNext()) {                  doSomething(i2.next()); } BUG! Unfortunately,  this bug is silent
Background image of page 4
General Programming 5 More Item 45:  Solution with  for   Loops Consider the same formulation with for loops  Result is compile time error Note: with for loop – no reason to change variable names for (Iterator<Element> i = c.iterator();  i.hasNext(); ) {    doSomething(i.next()); } // Compile time error – cannot find symbol i for (Iterator<Element> i2 = c2.iterator();  i.hasNext(); ) {    doSomething(i2.next()); }
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
General Programming 6 More Item 45: Example of  Multiple Initializations A final for loop example for (int i=0, n = expensiveComputation(); i < n; i++) {    doSomething(i); } Note that there are two loop variables:  i  and  n Scope of both is limited to  for  loop Avoid if  expensiveComputation()  does not have  a constant value
Background image of page 6
General Programming 7 Item 46: Prefer for-each Loops to  Traditional for Loops // Preferred idiom for iterating over a collections and  arrays for (Element e: elements) {    // read “:” as “in”    doSomething(e); } // No longer the preferred idiom to iterate over a  collection for (Iterator I = c.iterator(); i.hasNext(); ) {    doSomething( (Element) i.next());   // No generics  before 1.5 // No longer the preferred idiom to iterate over an array
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/11/2011 for the course MATH 110 taught by Professor Staff during the Winter '08 term at BYU.

Page1 / 28

Bloch8 - EffectiveJava: GeneralProgramming...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online