[Tutor] am I missing another simpler structure?
Brian van den Broek
bvande at po-box.mcgill.ca
Thu Dec 16 09:54:12 CET 2004
Liam Clarke said unto the world upon 2004-12-16 02:05:
> Alright, so that was a quick example, but....
>>return not x % 2
> A light dawns.
> On Thu, 16 Dec 2004 15:58:38 +0900, Guillermo Fernandez Castellanos
> <guillermo.fernandez.castellanos at gmail.com> wrote:
>>>I find multiple returns to be rather useful
>>> if not x % 2: return False
>>> return True
>>I find this easier though...
>> return not x % 2
Hi Liam, Guille, and all,
Liam, the relationship between the code you put up and the suggestion
Guille made closely parallel the my code from Marc's check_range thread
and the suggestion DogWalker made. Thinking that our common slip in
these two chucks might be more common in my code than I can currently
recognize is what moved me to start the present thread.
If my original bit of code, the structure was like:
output_value = False
output_value = True
Mine would be like yours if transformed to:
It seems to me that the two structures are inter-substitutable without
difference to outcome (speed, readability, and other 'pragmatics' aside
-- my structure will be slower for the assignments, for instance).
What makes me lean toward mine still? I'd encountered a general
injunction to avoid multiple exit point somewhere on the wild web
(cannot recall where). At the time, it didn't make sense to me. But, as
I posted earlier today in reply to Kent, I was burned by multiple exits
a while ago. (To be fair, by multiple exits and overlong functions which
obscured the multiplicity from me -- stupid low res monitor!)
The best discussion I found with a quick google was
<http://c2.com/cgi/wiki?SingleFunctionExitPoint>. Much of the
anti-multiple-exits rational there only applies to languages like C that
make the coder manage the memory. But "Having MultipleReturns is the 21
century equivalent of GotoConsideredHarmful" resonates with me. The idea
is that multiple returns, particularly when separated by numerous lines
of code, makes for spaghetti code.
('Goto Considered Harmful' is a famous short and fairly accessible paper
[no math or code] by Dijkstra arguing that the use of Goto's makes code
hard to follow <http://www.acm.org/classics/oct95/>. When I first
started with Python, I'd not programmed in a decade or so after some
highschool BASIC. My first reaction was "where's the bleedin' goto?".
Once I coded some and noticed substantially less frustration than I
recall from managing my gotos, I learned to appreciate that there wasn't
a goto after all.)
Anyway, that's plenty of ramble for ce soir.
But, Liam, this is the first post I've made in your direction since I
read you mention it: congrats for your new son!
Best to all,
More information about the Tutor