return command in a function.

Travers Naran tnaran at direct.ca
Thu Sep 27 01:31:05 EDT 2001


"Brian Lee" <senux at senux.com.NOSPAM> wrote in message
news:Kvvs7.110175$2k2.2906571 at news.bora.net...
> Hi all! I'm newbie at programming and especially Python langauge.
>
> What do you think when someone put return, break or continue command
> in half of a function. I think that commands are like ``goto'' It is very
> confused for me to understand big code because such commands
> jump un-excepted line.

Not too bad an observation, but as someone else pointed out years ago, goto
ain't such a bad thing.

A return mid-way through a function is controversial.  My view is this:

If the function is really short or there's a bunch of returns clustered
together, then it's ok.  Otherwise, avoid.

As for break's and continue's, they can be used well and used poorly.  It's
not a bad thing for structured programming to use them, because they still
refer to the inner most loop and still respect one-entry/one-exit rule.  But
if they're not immediately obvious where they are to anyone else looking at
the code, then avoid them.

All of these situations can be avoided if you're willing to use a lot of
if-else statements, but using a lot of if-else statements makes your
programs harder to read, so you'll have to decide which is best.

In general, the best advice I can give is:

1. Does it make it easier for another programmer to see what's really going
on?
2. Does it make it easier to maintain the program to do it one way or the
other?
3. Does it work??

If you answer yes to all three, then do it.  Don't just do something because
it satisfies some obscure philosophy of aesthetics for code.






More information about the Python-list mailing list