To add my 2 cents, I think having a single return makes a lot of sense in C
(perhaps where the rule carries over from), but it makes no sense in Java at
all. Just the opposite actually. Prohibiting multiple returns in Java is
downright dangerous. Setting a variable which you return once at the end of
a method puts you at risk of returning null when you forget to set the
return value or of setting the result more than once and ignoring all but
the last, two things the Java compiler can easily detect if you'd just use
multiple returns.
Also, exceptions force you think in terms of multiple exit points anyway.
Multiple returns make code more readable (no extraneous variables), easier
to follow. The same things that make it easy for a compiler to follow make
it easier for me to follow, too. "Oh, it must return here--there's no chance
execution can continue and return a different result later in the method."
I've heard some people argue that having a variable makes it easier to check
it in a debugger but modern debuggers should enable you to see set a method
exit breakpoint and see the result.
Bob
===================================
This list is hosted by DevelopMentorĀ®
http://www.develop.com
View archives and manage your subscription(s) at
http://discuss.develop.com
opensubscriber is not affiliated with the authors of this message nor responsible for its content.