how to use bool

tinnews at isbd.co.uk tinnews at isbd.co.uk
Fri Jan 4 17:31:39 CET 2008


Chris Mellon <arkanes at gmail.com> wrote:
> On 03 Jan 2008 16:09:53 GMT,  <tinnews at isbd.co.uk> wrote:
> >
> > jimgardener at gmail.com wrote:
> > > hi, i have some code where i set a bool type variable and if the value
> > > is false i would like to return from the method with an error msg..
> > > being a beginner I wd like some help here
> > >
> > > class myclass:
> > >      .........
> > >     def  mymethod(self):
> > >              success=True
> > >              msg="all validation OK"
> > >              success=validateSthing()
> > >              if(success==False):
> > >                    msg="sthing failed"
> > >                    return (success,msg)
> > >
> > >              dosomeprocessing()
> > >              .....
> > >              success=validateSthingelse()
> > >              if(success==False):
> > >                    msg="sthingelse  failed"
> > >                    return (success,msg)
> > >              domoreprocessing()
> > >               ....
> > >                return(success,msg)
> > >
> > > i would like to know if this way of doing this is OK..I have need of
> > > many kinds of validations in this ..is there a better way of doing
> > > this ?
> > >
> > With my philosophical programming hat on the first thing I'd say (as a
> > fairly beginning python programmer) is "avoid multiple returns from a
> > function/method if at all possible".  They breed all sorts of problems
> > and errors, in particular if there's any clearing up to do you have to
> > do it in lots of places (or you forget it in some places).
> >
> 
> This advice is highly controversial, and in the presence of exceptions
> it is, at best, voodoo coding. Since your function can exit at any
> point whether you do it intentionally or not, if you have crucial
> cleanup it's best to write your code in a way that does it correctly
> even if you return early. Following this style also often leads to odd
> contortions, like extra layers of indentation, and a proliferation of
> temporary flags and value-holders that aren't necessary if you write
> the code in a more straight forward manner.
> 
OK, I agree, I had my C hat on (no exceptions).

On the other hand if you end with lots of levels of indentation going
this way it suggests to me that maybe breaking up into more functions
would be a good idea.


> Make your decisions on a case by case basis of complexity,
> readability, and reliability instead of following pronouncements from
> on high (especially decades old pronouncements made in a different
> context). Forcing a single return site in the code below adds
> complexity, arguable harms readability, and provides *zero* benefit in
> the code at hand.
> 
> > So:-
> >
> >     def  mymethod(self):
> >         msg="sthing failed"
> >         success=validateSthing()
> >         if success:
> >             dosomeprocessing()
> >             .....
> >             success=validateSthingelse()
> >             if success:
> >                 domoreprocessing()
> >                 ....
> >                 msg="all validation OK"
> >         return (success,msg)
> >
> > I've lost the different messages for different errors but you get the
> > idea.
> >
> >
> > "if success:" rather than "if (success==True)", more readable.  For
> > the opposite "if not success:".
> >
> >
> >
> > --
> > Chris Green
> >
> > --
> > http://mail.python.org/mailman/listinfo/python-list
> >

-- 
Chris Green



More information about the Python-list mailing list