Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #12269

Re: how to format long if conditions

From Roy Smith <roy@panix.com>
Newsgroups comp.lang.python
Subject Re: how to format long if conditions
Date 2011-08-27 12:39 -0400
Organization PANIX Public Access Internet and UNIX, NYC
Message-ID <roy-896BCD.12390127082011@news.panix.com> (permalink)
References <mailman.457.1314428909.27778.python-list@python.org>

Show all headers | View raw


In article <mailman.457.1314428909.27778.python-list@python.org>,
 Arnaud Delobelle <arnodel@gmail.com> wrote:

> Hi all,
> 
> I'm wondering what advice you have about formatting if statements with
> long conditions (I always format my code to <80 colums)
> [...]
>         if (isinstance(left, PyCompare) and isinstance(right, PyCompare)
>                 and left.complist[-1] is right.complist[0]):
>             py_and = PyCompare(left.complist + right.complist[1:])
>         else:
>             py_and = PyBooleanAnd(left, right)

To tie this into the ongoing, "When should I write a new function?" 
discussion, maybe the right thing here is to refactor all of that mess 
into its own function, so the code looks like:

   if _needs_compare(left, right):
         py_and = PyCompare(left.complist + right.complist[1:])
    else:
         py_and = PyBooleanAnd(left, right)

and then

def _needs_compare(left, right):
   "Decide if we need to call PyCompare"
   return isinstance(left, PyCompare) and \
          isinstance(right, PyCompare) and \
          left.complist[-1] is right.complist[0]

This seems easier to read/understand than what you've got now.  It's an 
even bigger win if this will get called from multiple places.

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

how to format long if conditions Arnaud Delobelle <arnodel@gmail.com> - 2011-08-27 08:08 +0100
  Re: how to format long if conditions Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-08-27 17:24 +1000
    Re: how to format long if conditions Arnaud Delobelle <arnodel@gmail.com> - 2011-08-27 11:24 +0100
    Re: how to format long if conditions Ben Finney <ben+python@benfinney.id.au> - 2011-08-27 22:04 +1000
  Re: how to format long if conditions Hans Mulder <hansmu@xs4all.nl> - 2011-08-27 09:50 +0200
    Re: how to format long if conditions Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-08-27 19:05 +1000
      Re: how to format long if conditions Hans Mulder <hansmu@xs4all.nl> - 2011-08-27 12:51 +0200
    Re: how to format long if conditions "Colin J. Williams" <cjw@ncf.ca> - 2011-08-27 11:16 -0400
      Re: how to format long if conditions Hans Mulder <hansmu@xs4all.nl> - 2011-08-27 17:53 +0200
        Re: how to format long if conditions "Colin J. Williams" <cjw@ncf.ca> - 2011-08-27 17:25 -0400
  Re: how to format long if conditions Roy Smith <roy@panix.com> - 2011-08-27 12:39 -0400

csiph-web