POV-Ray : Newsgroups : povray.off-topic : Programming style question - specifically Python : Re: Programming style question - specifically Python Server Time
29 Jul 2024 20:25:55 EDT (-0400)
  Re: Programming style question - specifically Python  
From: Warp
Date: 26 Aug 2011 10:48:39
Message: <4e57b247@news.povray.org>
Darren New <dne### [at] sanrrcom> wrote:
> On 8/22/2011 8:04, Warp wrote:
> > Darren New<dne### [at] sanrrcom>  wrote:
> >> bool_b = bool_a ? min(...) : false
> >
> >    That seems to have redundancy. How about:
> >
> > bool_b = bool_a || min(...);
> >

  Btw, I made a mistake above. Obviously it should have been:

bool_b = bool_a && min(...);

  (Yes, I understand the irony that you not catching this mistake is a
tell-tale sign of the obfuscatory nature of the expression. I still prefer
it, though.)

> More efficient perhaps, but less readable IMO. You don't really want to set 
> bool_b to the value of bool_a, so this I would consider misleading. There's 
> an implication that the value of bool_a propagates, whereas in actuality 
> it's more a gating value.

  You are not setting bool_b the value of bool_a. You are making a boolean
'and', and assigning the result of that to bool_b. The shortcut evaluation
of operator && is being (ab)used here to avoid evaluating the min(...)
expression in case bool_a is false. The advantage of this is that it
avoids the redundancy of the "else false" branch. (It's a bit like that
annoying "else break;" at the end of a loop, only in this case it's easily
avoidable.)

  This is, btw, a common tactic used in unix shell scripts. There the
&& operator can be used for a very similar purpose: If the command at the
left hand side succeeds, the command at the right hand side is executed.
The value of the expression is that of the first command that didn't
succeed, or success if they all succeeded.

  This means that rather than having to do something like

    if [ command1 ]; then if [ command2 ]; then command3; fi; fi

you can do it like:

    command1 && command2 && command3

  This is handy to execute several commands in succession, but have the
entire thing immediately stop if any of the commands fails. (This in
contrast with "command1; command2; command3" which will not stop if any
of those fails.)

-- 
                                                          - Warp


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.