[H-GEN] Which is better?

Andrae Muys andrae at internode.on.net
Sun Apr 25 06:21:11 EDT 2004


Matthew Sellers wrote:

>>For example:
>>
>>First code:
>>int upperLimit = doCalcs(variable);
>>for (i = 0; i < upperLimit; i++) {
>>....
>>}
>>
>>public static int doCalcs(incoming int) { ...
>><many many lines of code>
>>return xxx;
>>}
>>
>>
>>Second code:
>>for (i = 0; i < doCalcs(variable); i++) {
>>....
>>}
>>
>>public static int doCalcs(incoming int) { ...
>><many many lines of code>
>>return xxx;
>>}
>>
> Here you are correct. If there is no reason for doCalc to be evaluated for 
> each iteration of the loop then it should not be.
> 

Except of course that the two code samples aren't equiv' so you can't 
really compare them.  In the first example, you have abstracted the loop 
termination condition.  In the second you have a time-varying 
termination check; I would probably want to know why you need a 
check-condition AND a loop counter, and look to refactoring one of the 
two out -- either into the first, or into a while loop.

Iterator i = datastructure.iterator();

while(i.hasNext() && doCalcs(variable)) {
   Element e = (Element)i.next();
   ...
}


And yes Ben, we do look forward to 'java-generics', even if they're not 
really generics :(.

Andrae





More information about the General mailing list