Below are two snippets. Notice the ONE AND ONLY difference between the programs is that one break to return
and the other return
immediately. I understand it's good design practice to have one point of exit inside a method. But I am not worrying about design here. If I pay extra for using break
, how much extra computation/memory/clock-cycle do I pay?
Program one:
public boolean doThis(String[] A){
boolean indicator = false;
for(int i=0; i<A.length; i++){
if(A[i].equals("Taboo"))
break;
for(int x=0; x<=i; x++)
//some work is done here. to make indicator true or false
}
return indicator;
}
Program two:
public boolean doThis(String[] A){
boolean indicator = false;
for(int i=0; i<A.length; i++){
if(A[i].equals("Taboo"))
return false;
for(int x=0; x<=i; x++)
//some work is done here. to make indicator true or false
}
return indicator;
}