>>> my instructor likes to use this construct in class but never really
>>> explained why/how it works:
[quoted text clipped - 20 lines]
> fact, I usually prefer "return" inside a loop over a "break". The
> "return" gives pretty easy short circuit exit
Yep, agree completely. I don't like a return statement being comment
"break from while," though, especially in a course for people who don't
yet know the language.
> and IMHO it is far
> superior in cases like this:
[quoted text clipped - 14 lines]
> Using the loop condition to break the loop makes this piece of code much
> more complex and probably also less efficient.
Maybe, but I still find it clearer, and easier to debug.
package cljp;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.List;
import java.util.Iterator;
public class Main {
private static PrintWriter out = new PrintWriter(System.out, true);
List<Integer> myInts = Arrays.asList(
new Integer[] { 1, 2, 3, 4, 5 });
public Integer findIt(Integer n) {
Iterator<Integer> iter = myInts.iterator();
Integer i = null;
boolean found = false;
while(iter.hasNext() && !found) {
i = iter.next();
found = (n == i);
}
return found ? i : null;
}
public static void main(String[] args) {
}
}
> Kind regards
>
> robert
Robert Klemme - 31 Oct 2006 17:07 GMT
>> and IMHO it is far superior in cases like this:
>>
[quoted text clipped - 15 lines]
>
> Maybe, but I still find it clearer, and easier to debug.
Amazing. It would never occur to me that (below) is clearer or easier
to debug than (above). But obviously people are very different.
> public Integer findIt(Integer n) {
> Iterator<Integer> iter = myInts.iterator();
[quoted text clipped - 7 lines]
> return found ? i : null;
> }
Cheers
robert
Jeffrey Schwab - 31 Oct 2006 17:31 GMT
>>> and IMHO it is far superior in cases like this:
>>>
[quoted text clipped - 18 lines]
> Amazing. It would never occur to me that (below) is clearer or easier
> to debug than (above). But obviously people are very different.
You said it, brother. I am continually amazed at how smart people can
have such different opinions about religion, politics and software
design. :)
>> public Integer findIt(Integer n) {
>> Iterator<Integer> iter = myInts.iterator();
[quoted text clipped - 7 lines]
>> return found ? i : null;
>> }