[Bf-committers] Style issue

Branan Riley branan at gmail.com
Fri Apr 14 20:22:28 CEST 2006


if (get_res_1() && get_res_2() && get_res_3()) {
}

is completely good so long as none of those change anything. If they do
change something, then either

if(get_res_1())
{
        if(get_res_2())
        {
                if(get_res_3())
                {
                        ...
                }
        }
}

or

r1=get_res_1()
r2=get_res_2()
r3=get_res_3()

if(r1 && r2 && r3)
{
        ...
}

would be correct, depending on the behavior you want.

Although the standard specifies how side effects are supposed to occur when
using boolean operators, some compilers don't follow this. I know that GCC &
MSVC do (I've tested them myself), but if someone has an obscure compiler
that doesn't follow the standard, it could break.

Branan

On 4/14/06, Martin Poirier <theeth at yahoo.com> wrote:
>
>
>
> --- Alexander Ewering <blender at instinctive.de> wrote:
>
> >
> > On Fri, 14 Apr 2006, Davide Vercelli wrote:
> >
> > > r1 = get_res_1();
> > >
> > > if (r1) {
> > >  /* do stuff */
> > >  r2 = get_res_2();
> > >  if (r2) {
> > >    /* do stuff */
> > >    r3 = get_res_3()
> > >    if (r3) {
> > >      /* and so on indenting... */
> > >    }
> > >  }
> > > }
> > >
> > > On the other hand I prefer to organize my code
> > like this:
> > >
> > > r1 = get_res_1();
> > > if (!r1)
> > >  return; /* or break, or continue */
> > >
> > > r2 = get_res_2();
> > > if (!r2)
> > >  return;
> >
> > Both styles are used - for example, at the beginning
> > of draw_object(), the
> > second style is used..
> >
> > Also, there's of course always this, which is my
> > preferred style in simple
> > situations :)
> >
> > if (get_res_1() && get_res_2() && get_res_3()) {
> > }
>
> Unless you can be 100% sure all compiler garranty (or
> turn off) short circuit evaluation, the execution of
> your expression is undefined.
>
> Martin
>
> __________________________________________________
> Do You Yahoo!?
> Tired of spam?  Yahoo! Mail has the best spam protection around
> http://mail.yahoo.com
> _______________________________________________
> Bf-committers mailing list
> Bf-committers at projects.blender.org
> http://projects.blender.org/mailman/listinfo/bf-committers
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.blender.org/pipermail/bf-committers/attachments/20060414/00533879/attachment.html


More information about the Bf-committers mailing list