Which is better?Slide 2Answer: BSlide 4Slide 5Which is best?Answer: DSlide 8Slide 9Slide 10Answer: neitherSlide 12Answer: ASlide 14Slide 15Slide 16Slide 17Slide 18Answer: CSlide 20Slide 21Slide 22Slide 23Slide 24Slide 25Slide 26Slide 27Slide 28Slide 29ConclusionsThe EndJan 14, 2019Which is better?2Which is better?Assume s1 and s2 are Strings:A. if (s1 == s2) { ... }B. if (s1.equals(s2)) { ... }?3Answer: Bs1 == s2 tests whether s1 and s2 reference the same string; s1.equals(s2) tests whether they reference equal stringsString s1 = "ABC";String s2 = s1;String s3 = "ABC";String s4 = "AB" + "C"All these strings are equal; but s4 is in a different memory location than the others, so the == test yields false4Which is better?Assume s1 is a String:A. if (s1.equals("OK")) { ... }B. if ("OK".equals(s1)) { ... }?5Answer: Bs1.equals("OK") sends a message to s1 asking if it is equal to "OK""OK".equals(s1) sends a message to "OK" asking if it is equal to s1This is legal, because "OK" is a StringIf s1 is null, then: s1.equals("OK") gives a NullPointerException "OK".equals(s1) gives false (Of course, in some circumstances you might prefer to get a NullPointerException)6Which is best?Assume int numbers[ ] = new int[100];A. for (int i = 0; i < 100; i++) numbers[i] = i;B. for (int i = 0; i <= 99; i++) numbers[i] = i; C. for (int i = 0; i < ARRAY_SIZE; i++) numbers[i] = i; D. for (int i = 0; i < numbers.length; i++) numbers[i] = i; ?7Answer: DD is best: for (int i = 0; i < numbers.length; i++)numbers.length changes automatically if array size is changedC is OK: for (int i = 0; i < ARRAY_SIZE; i++)Changing ARRAY_SIZE will fix this loop if array size is changedB is poor: for (int i = 0; i < 100; i++)Uses a “magic number”Must track down and change each occurrence if array size is changedA is worst: for (int i = 0; i <= 99; i++)This has all the same problems as BB is more traditional and therefore less “surprising”The array size is 100, not 99, so it’s more obvious where the number came fromYou have to do some arithmetic to get 99If you change the array size, a search for 100 won’t find the loop that uses 998Which is better?Assume finished is a boolean variable:A. if (finished == true) {...}B. if (finished) {...}?9Answer: Bfinished == true is redundant:If finished is true, then finished==true will be trueIf finished is false, then finished==true will be falseThe extra words don’t gain you anythingfinished==true might seem more readable to a beginner, but you quickly learn to read the shorter formBrevity in programming, as in writing, is a virtueYou can avoid the possible mistake of saying if (finished = true) { ... }10Which is better?Assume foo, bar, and larger are integersA. if (foo > bar) larger = foo;else larger = bar;B. larger = foo > bar ? foo : bar;?11Answer: neitherFor each of these, you have to look at the code carefully to make sure it is correctlarger = Math.max(foo, bar);is easier to read and more obviously correct12Which is better?A. String s = "Hello";B. String s = new String("Hello");?13Answer: A"Hello" is special syntax to implicitly construct a stringString s = new String("Hello"); actually constructs two strings: "Hello" constructs the first string, then it is given as a parameter to an explicit constructor, which constructs the second string14Which is better?Suppose p is a JPanel with a BorderLayout and okButton is a JButton:A. p.add(okButton, BorderLayout.NORTH);B. p.add(okButton, "North");Note: BorderLayout.NORTH is a String constant whose value is "North"?15Answer: Ap.add(okButton, BorderLayout.NORTH); is strongly recommended over the shorter form p.add(okButton, "North") -- but why?Answer: The former gives better error detectionIf you type p.add(okButton, "north"), there is no error, but it doesn’t do what you wantIf you type p.add(okButton, BorderLayout.North) you will get a syntax error, because BorderLayout has no such variable as North16Which is best?Suppose n is an int and s is a String:A. s = Integer.toString(n);B. s = String.valueOf(n);C. s = new Integer(n).toString();D. s = n + "";?17Answer: DI prefer D (s = n + ""; ) because:It’s a common idiom, therefore easily recognizedIt’s shortIt works for any type18Which is best?Assume n is an integer:A. if (n < 0) n = 0;B. if (n < 0) n = 0;C. if (n < 0) { n = 0;}?19Answer: CIf, later on, you want to add a statement, it’s easy to make this mistake with B:if (n < 0) System.out.println("n was " + n); n = 0;You won’t make this mistake with A or CWith C (using braces), you don’t have to change anything that’s already thereHowever, A (all on one line) is often convenient20Which is better?Assume n is an integer:A. int factorial = 1;for (int i = 2; i < n; i++) { factorial *= n;}B. int factorial = 1;int i;for (i = 2; i < n; i++) { factorial *= n; }?21Answer: AIn most cases, you don’t care about the index of a for loop outside the loopYou typically give it an initial value in the loopYou typically already have its final value in some variable or by some simple computationIf you don’t need a variable, you shouldn’t have that variableIt doesn’t help anything, and it might get in the way22Which is better?A. int search(int[] array, int target) { for (int i = 0; i < array.length; i++) { if (array[i] == target) return i; } return -1;}B. int search(int[] array, int target) throws NotFoundException() { for (int i = 0; i < array.length; i++) { if (array[i] == target) return i; } return new NotFoundException();}?23Answer: AExceptions should be used for exceptional cases, not for normal program controlIn almost all cases, not finding something in an array search is one of the expected outcomesOf course, there can be exceptions24Which is better?A. private void combinations(int n, int t) throws InvalidArgumentException { if (t < 0 || t > n) { throw new InvalidArgumentException(); ...B. private void combinations(int n, int t) { assert t >= 0 && t <= n; ... ?25Answer: BThe method is marked private, so you (the person writing this class) will be the only one using this methodIf you call it incorrectly, it’s most likely a bug, not just an unexpected external event, and should be fixed, not tested forBesides, using an assert statement is a lot less
View Full Document