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

Discussion in 'Python' started by Tim Chase, Feb 10, 2010.

  1. Tim Chase

    Tim Chase Guest

    Larry Hudson wrote:
    > But a minor rearrangement is simpler, and IMHO clearer:
    >
    > if 'mystring' not in s:
    > print 'not found'
    > else:
    > print 'foundit'
    > print 'processing'


    I've always vacillated on whether that would better be written as
    Larry does, or as

    if 'mystring' in s
    print 'foundit'
    print 'processing'
    else:
    print 'not found'

    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 the
    code-blocks are more equal in size, I tend to use "if
    {positive}", but if the negative "not" section is only 1-2 lines,
    I tend to do as Larry writes and make it harder to miss by using
    "if {negative}". Otherwise the "if {positive}...else" can end up
    sufficiently distant from the "if" that it's easy to miss.

    Any thoughts on how others make the choice?

    -tkc
     
    Tim Chase, Feb 10, 2010
    #1
    1. Advertising

  2. Re: "if {negative}" vs. "if {positive}" style

    * Tim Chase:
    > Larry Hudson wrote:
    >> But a minor rearrangement is simpler, and IMHO clearer:
    >>
    >> if 'mystring' not in s:
    >> print 'not found'
    >> else:
    >> print 'foundit'
    >> print 'processing'

    >
    > I've always vacillated on whether that would better be written as Larry
    > does, or as
    >
    > if 'mystring' in s
    > print 'foundit'
    > print 'processing'
    > else:
    > print 'not found'
    >
    > 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 the code-blocks are more equal in
    > size, I tend to use "if {positive}", but if the negative "not" section
    > is only 1-2 lines, I tend to do as Larry writes and make it harder to
    > miss by using "if {negative}". Otherwise the "if {positive}...else" can
    > end up sufficiently distant from the "if" that it's easy to miss.
    >
    > Any thoughts on how others make the choice?


    I think it spills over from the practice of checking preconditions first, e.g.
    returning or raising exceptions or whatever.

    This avoids silly n-level nesting of the main "normal case" part.


    Cheers,

    - Alf
     
    Alf P. Steinbach, Feb 10, 2010
    #2
    1. Advertising

  3. Re: "if {negative}" vs. "if {positive}" style

    On 02/10/10 03:36, Tim Chase wrote:
    > Larry Hudson wrote:
    >> But a minor rearrangement is simpler, and IMHO clearer:
    >>
    >> if 'mystring' not in s:
    >> print 'not found'
    >> else:
    >> print 'foundit'
    >> print 'processing'

    >
    > I've always vacillated on whether that would better be written as Larry
    > does, or as
    >
    > if 'mystring' in s
    > print 'foundit'
    > print 'processing'
    > else:
    > print 'not found'


    <cut>
    >
    > Any thoughts on how others make the choice?
    >

    I cases like this when aesthetics are not that much of a deal breaker, I
    usually put the condition which I think will be the true the majority of
    the time first.
    This is not for performance reasons (never tested whether this has any
    effect) but more documentation like purpose to go from a most expected
    case to the least expected one. YMMV

    --
    mph
     
    Martin P. Hellwig, Feb 10, 2010
    #3
  4. Tim Chase

    Roy Smith Guest

    In article <>,
    Tim Chase <> wrote:

    > Larry Hudson wrote:
    > > But a minor rearrangement is simpler, and IMHO clearer:
    > >
    > > if 'mystring' not in s:
    > > print 'not found'
    > > else:
    > > print 'foundit'
    > > print 'processing'

    >
    > I've always vacillated on whether that would better be written as
    > Larry does, or as
    >
    > if 'mystring' in s
    > print 'foundit'
    > print 'processing'
    > else:
    > print 'not found'
    >
    > 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 the
    > code-blocks are more equal in size, I tend to use "if
    > {positive}", but if the negative "not" section is only 1-2 lines,
    > I tend to do as Larry writes and make it harder to miss by using
    > "if {negative}". Otherwise the "if {positive}...else" can end up
    > sufficiently distant from the "if" that it's easy to miss.
    >
    > Any thoughts on how others make the choice?
    >
    > -tkc


    In general, I try to avoid negation, because it makes things harder to
    understand. When you write:

    if not foo:
    blah
    else:
    blah-blah

    the else clause is executed if "not foo is not true". That quickly gets
    confusing.

    As for the code blocks being so large that the else becomes distant from
    the condition, that smells like the two codeblocks want to be refactored
    into distinct functions:

    if foo:
    do_one_thing()
    else:
    do_something_else()

    It's not always practical, but my first reaction would be to try that and
    see if it works.
     
    Roy Smith, Feb 10, 2010
    #4
  5. Tim Chase

    Carl Banks Guest

    On Feb 9, 7:36 pm, Tim Chase <> wrote:
    > Larry Hudson wrote:
    > > But a minor rearrangement is simpler, and IMHO clearer:

    >
    > > if 'mystring' not in s:
    > >      print 'not found'
    > > else:
    > >      print 'foundit'
    > >      print 'processing'

    >
    > I've always vacillated on whether that would better be written as
    > Larry does, or as
    >
    >    if 'mystring' in s
    >      print 'foundit'
    >      print 'processing'
    >    else:
    >      print 'not found'

    [pass]
    > Any thoughts on how others make the choice?



    I almost always arrange it as follows, regardless of the size of the
    blocks or whether the condition is negated or not:

    if (typical-condition):
    typical-case
    else:
    exceptional-case

    In particular I almost always test if something is not None, because
    being None is almost always the exceptional case:

    if x is not None:
    ...
    else:
    ...

    However, whenever the exceptional case action is to break, return,
    continue, or raise an exception, I'll test for it and leave the
    typical case unnested:

    if (exceptional-condition):
    exceptional-case
    typical-case

    No, it deosn't bother me in the least that I sometimes test for the
    exceptional case and sometimes for the typical.

    Whenever there is no typical case or exceptional case I'll go for the
    simpler condition.


    Carl Banks
     
    Carl Banks, Feb 10, 2010
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hicham Mouline
    Replies:
    2
    Views:
    829
    Keith Thompson
    Apr 23, 2010
  2. Norah Jones
    Replies:
    12
    Views:
    266
    88888 Dihedral
    Mar 14, 2013
  3. Jean-Michel Pichavant
    Replies:
    0
    Views:
    116
    Jean-Michel Pichavant
    Mar 12, 2013
  4. Wolfgang Maier
    Replies:
    0
    Views:
    106
    Wolfgang Maier
    Mar 12, 2013
  5. Devin Jeanpierre
    Replies:
    0
    Views:
    104
    Devin Jeanpierre
    Mar 12, 2013
Loading...

Share This Page