Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Re: "if {negative}" vs. "if {positive}" style (was: New to Python) (http://www.velocityreviews.com/forums/t714483-re-if-negative-vs-if-positive-style-was-new-to-python.html)

Tim Chase 02-10-2010 03:36 AM

Re: "if {negative}" vs. "if {positive}" style (was: New to Python)
 
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



Alf P. Steinbach 02-10-2010 04:09 AM

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

Martin P. Hellwig 02-10-2010 08:40 AM

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

Roy Smith 02-10-2010 01:38 PM

Re: "if {negative}" vs. "if {positive}" style (was: New to Python)
 
In article <mailman.2269.1265773024.28905.python-list@python.org>,
Tim Chase <python.list@tim.thechases.com> 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.

Carl Banks 02-10-2010 09:47 PM

Re: "if {negative}" vs. "if {positive}" style (was: New to Python)
 
On Feb 9, 7:36*pm, Tim Chase <python.l...@tim.thechases.com> 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


All times are GMT. The time now is 04:40 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.