Which is better Which is better Assume s
Which is better?
Which is better? • Assume s 1 and s 2 are Strings: A. if (s 1 == s 2) {. . . } B. if (s 1. equals(s 2)) {. . . }
Answer: B • s 1 == s 2 tests whether s 1 and s 2 reference the same string; s 1. equals(s 2) tests whether they reference equal strings • String s 1 = "ABC"; String s 2 = s 1; String s 3 = "ABC"; String s 4 = "AB" + "C" • All these strings are equal; but s 4 is in a different memory location than the others, so the == test yields false
Which is better? • Assume String s 1; A. if (s 1. equals("OK")) {. . . } B. if ("OK". equals(s 1)) {. . . }
Answer: B • s 1. equals("OK") sends a message to s 1 asking if it is equal to "OK" • "OK". equals(s 1) sends a message to "OK" asking if it is equal to s 1 – This is legal, because "OK" is a String • If s 1 is null, then: s 1. equals("OK") gives a Null. Pointer. Exception "OK". equals(s 1) gives false
Which is better? • 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;
Answer: A • for (int i = 0; i < 100; i++) is better than for (int i = 0; i <= 99; i++) • Three reasons: – The first is more traditional – The array size is 100, not 99, so it’s more obvious where the number came from • You have to do some arithmetic to get 99 – If you change the array size, a search for 100 won’t find the loop that uses 99
Which is better? • Assume int numbers[ ] = new int[100]; A. for (int i = 0; i < 100; i++) numbers[i] = i; B. for (int i = 0; i < numbers. length; i++) numbers[i] = i;
Answer: B • for (int i = 0; i < numbers. length; i++) is better than for (int i = 0; i < 100; i++) • If you later decide to change the size of the numbers array, you only need to do it in the declaration; the for loop that uses length will automatically adjust
Which is better? • Assume finished is a boolean variable: A. if (finished == true) {. . . } B. if (finished) {. . . }
Answer: B • finished == true is redundant: – If finished is true, then finished==true will be true – If finished is false, then finished==true will be false • The extra words don’t gain you anything – finished==true might seem more readable to a beginner, but you quickly learn to read the shorter form – Brevity in programming, as in writing, is a virtue – You can avoid the possible mistake of saying if (finished = true) {. . . }
Which is better? • Assume foo, bar, and larger are integers A. if (foo > bar) larger = foo; else larger = bar; B. larger = foo > bar ? foo : bar;
Answer: neither • For each of these, you have to look at the code carefully to make sure it is correct larger = Math. max(foo, bar); is easier to read and more obviously correct
Which is better? A. String s = "Hello"; B. String s = new String("Hello");
Answer: A • "Hello" is special syntax to implicitly construct a string • String 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 string
Which is better? • Suppose p is a Panel with a Border. Layout and ok. Button is a Button: A. p. add(ok. Button, Border. Layout. NORTH); B. p. add(ok. Button, "North"); • Note: Border. Layout. NORTH == "North"
Answer: A • p. add(ok. Button, Border. Layout. NORTH); is strongly recommended over the shorter form p. add(ok. Button, "North") -- but why? • Answer: better error detection – If you type p. add(ok. Button, "north"), there is no error, but it doesn’t do what you want – If you type p. add(ok. Button, Border. Layout. North) you will get a syntax error, because Border. Layout has no such variable as North
Which is better? • Suppose n is an int and s is a String: A. s = Integer. to. String(n); B. s = String. value. Of(n); C. s = new Integer(n). to. String(); D. s = n + "";
Answer: D • I prefer D (s = n + ""; ) because: – It’s a common idiom, therefore easily recognized – It’s short – It works for any type
Which is better? • Assume n is an integer: A. if (n < 0) n = 0; B. if (n < 0) n = 0; C. if (n < 0) { n = 0; }
Answer: C • If, 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 C – With C (using braces), you don’t have to change anything that’s already there – However, A (all on one line) is often convenient
Which 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 = 1; for (i = 2; i < n; i++) { factorial *= n; }
Answer: A • In most cases, you don’t care about the index of a for loop outside the loop – You typically give it an initial value in the loop – You typically already have its final value in some variable or by some simple computation • If you don’t need a variable, you shouldn’t have that variable – It doesn’t help anything, and it might get in the way
Conclusions • There are various ways to do things • One way may be better than another because: – – It’s easier to read and understand It’s more familiar—the way things are usually done It’s less error prone, or provides better error detection It’s more efficient
The End
- Slides: 25