On 09Feb2010 21:36, 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' | | 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.
I do it like you, usually: if one branch is small, it comes first, the other branch is easily in sight under the "else". Otherwise, I tend to go with "if {positive}", or at any rate "if {the easiest-to-read-form-of-the-test}" if it all still reads well. If the program flow gets ugly one way, that breaks "still reads well" and can be cause to flip the condition. In loops my tendency is a bit less flexible; I often have code like this: while ...: if winnowing-test: whinge set flag continue or break, depending main body of loop here... Not a lot of choice about positive/negative in that scenario, though it fits well with the first criterion. Cheers, -- Cameron Simpson <c...@zip.com.au> DoD#743 http://www.cskk.ezoshosting.com/cs/ A Master is someone who started before you did. - Gary Zukav -- http://mail.python.org/mailman/listinfo/python-list