"if {negative}" vs. "if {positive}" style (was: New to Python)

Stephen Hansen apt.shansen at gmail.com
Wed Feb 10 05:05:09 CET 2010


On Tue, Feb 9, 2010 at 7:58 PM, Daniel Stutzbach <
daniel at stutzbachenterprises.com> wrote:

> On Tue, Feb 9, 2010 at 9:36 PM, Tim Chase <python.list at tim.thechases.com>wrote:
>
>> removing the "not" from the condition.  I admit I choose one over the
>> other based on some gut-feeling aesthetic that I can't really nail down.  I
>> think one of my major influencing factors revolves around the negative "not"
>> portion having one or two lines and the  positive portion having a large
>> block of code.
>
>
> If one block is much shorter, I tend to put the shorter block first.  That
> way, the "else" and the "if" are almost always on the screen together at the
> same time.  If they're both long, I factor out one or both of the blocks
> into functions.
>

Agree 100%.

I like whatever the little branch is to be first, with the big branch
falling into the "else:". It reads better then to have an "else:" at the end
with only a line or two: in such a situation what 'else' is doesn't really
have direct meaning, you have to look too far up to connect it to the "if"
logic. Comments shouldn't be needed-- if an "else:" falls too far away from
the "if" then scrolling around and looking for the code, it won't be
immediately obvious in meaning.

And if both branches are fairly long, it tends to imply that I just might
benefit from some more functions to re-organize logic into manageable
chunks. Then again, general rule of thumb? If a function is not entirely
visible on my screen, it might imply I might benefit from some more
functions. Though this is not anything like a hard rule.

--S
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100209/0ea696cf/attachment.html>


More information about the Python-list mailing list