Coding style and else statements

Carl Banks pavlovevidence at gmail.com
Wed Aug 30 15:29:53 EDT 2006


Steve Holden wrote:
> Carl Banks wrote:
> [...]
> > However, I have rare cases where I do choose to use the else (ususally
> > in the midst of a complicated piece of logic, where it's be more
> > distracting than concise).  In that case, I'd do something like this:
> >
> > def foo(thing):
> >     if thing:
> >         return thing+1
> >     else:
> >         return -1
> >     assert False
>
> I think that's about the most extreme defensive programming I've seen in
> a while! I can imaging it's saved your ass a couple of times when you've
> edited the code a while after writing it.

1. The "assert False" is more for documenting than error checking.
2. The right way to be defensive here is not to have redundant logic.
The above is really something I do rarely only when some other factor
makes the redundant logic a lesser of evils.


Carl Banks




More information about the Python-list mailing list