[Tutor] Re: Request for code critique ['break' vs control flag]

Erik Price erikprice at mac.com
Sat Oct 25 16:53:33 EDT 2003

On Thursday, October 23, 2003, at 01:00  AM, Alan Gauld wrote:

> I think the issue is that this is NOT a style thing. It's
> one of the basic tenets of structured programming and based
> on actual hard data. Having multiple exit points makes code
> harder to comprehend and hence more prone to error.

Aren't exceptions a kind of exit?

Personally, I think multiple exits are great.  At work I'm responsible 
for maintaining some extremely ugly Java code (it looks more like C 
than Java, taking little advantage of OO), and one of the things I am 
doing is applying refactorings it to make it more readable.  A lot of 
it consists of gigantic methods, hundreds of lines long, which simply 
set various flag variables throughout the body of the method, and then 
analyze the flags at the end of the method to determine what value to 
return or behavior to perform.  Sometimes this is unavoidable, but I 
find it much more readable to use use "Extract Method" refactoring to 
put some large chunk of code into a purposefully-named utility method 
(read: "function"), and return values explicitly from the point in the 
code where the processing can stop -- otherwise the reader has to 
continue to read the code long past the point where the flag is finally 
set, and make sure that it's not being re-set at some point later in 
the code, before it's finally returned.


More information about the Tutor mailing list