return in for loop or outside loop

Now someone told me that this is not very good programming because I use the return statement inside a loop and this would cause garbage collection to malfunction.

That’s incorrect, and suggests you should treat other advice from that person with a degree of skepticism.

The mantra of “only have one return statement” (or more generally, only one exit point) is important in languages where you have to manage all resources yourself – that way you can make sure you put all your cleanup code in one place.

It’s much less useful in Java: as soon as you know that you should return (and what the return value should be), just return. That way it’s simpler to read – you don’t have to take in any of the rest of the method to work out what else is going to happen (other than finally blocks).

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)