Too many return-statements = bad style?

Magnus Lyck? magnus at thinkware.se
Thu Jul 15 16:34:24 CEST 2004


steve_thompson at prodigy.net (Steve Thompson) wrote in message news:<c83c2133.0407140613.a0b5d78 at posting.google.com>...
> These days, all of my Python routines are single entry/single exit. 

This is really an old discussion that came up after Dijkstra
started writing about Structured Programming. His take on
S.P. was that functions should only have a single exit point.

Don Knuth, on the other hand, who is more of a house-god among
Pythonistas argued that a more lax view on S.P. was sufficient
and more effective. As far as I understand, Knuth's view is
consistent with multiple return statements.

"Alternative thinking on structured loops exists. Donald Knuth has
advocated flow graphs that can be written with all forward branches
drawn to the left of the code, all backward branches drawn to the
right, and no branches crossing each other in this representation.
Many of those knowledgeable in compilers and graph theory have
advocated allowing only reducible flow graphs."

>From http://en.wikipedia.org/wiki/Structured_programming

> And for those folks that actually view many exits as a Good Thing, I
> would argue that increasing the cyclomatic complexity of any piece of
> code is by definition the very antithesis of good software
> engineering.

Agreed, but setting a variable to different values in different 
if-else-branches in a function and returning that in a single 
return doesn't give you a lower cyclomatic complexity than multiple 
exits in the if-else-structure.

Your argument that sticking with single exits forces you to factor 
your code sounds a little like: "I never use my windshield wipers. 
That forces me to stop and clean my car more often, and that is a 
good thing." ;)



More information about the Python-list mailing list