octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: formatting ?: operator


From: Rik
Subject: Re: formatting ?: operator
Date: Tue, 25 Apr 2017 14:44:46 -0700

On 04/25/2017 10:28 AM, Mike Miller wrote:
> On Tue, Apr 25, 2017 at 12:19:59 -0400, John W. Eaton wrote: >> On 04/25/2017 12:02 PM, Rik wrote: >> >>> I really like having the '?' and ':' line up vertically to show that there >>> is alternative A and alternative B. >> >> I agree, which is why I might write >> >>   retval = (lower >>             ? octave::math::gammainc (x, a) >>             : 1.0 - octave::math::gammainc (x, a)); >> >> even if the condition and the first _expression_ will fit on a single line. > > Back to the original topic, I agree, and it looks like those that have > chimed in agree that it should look either like this > >     retval = (lower >               ? octave::math::gammainc (x, a) >               : 1.0 - octave::math::gammainc (x, a)); > > or this > >     retval = lower >              ? octave::math::gammainc (x, a) >              : 1.0 - octave::math::gammainc (x, a); > > but not this > >     retval = lower ? octave::math::gammainc (x, a) >                    : 1.0 - octave::math::gammainc (x, a);

Actually, I prefer this last form, but if people really want the long form then I will live with it.  For me the tertiary operator is about a quick if/then/else operation.  If it turns out to be three lines for every situation then it might be clearer to just write out the logic explicitly.

if (lower)
  retval = octave::math::gammainc (x, a)
else
  retval = 1.0 - octave::math::gammainc (x, a);

A quick absolute value function also looks unnecessarily long when written in the new style

abs_x = x < 0
        ? -x
        : x;

--Rik

> > > The only sticking point seems to be whether we should mandate > parentheses around the entire _expression_ or not, right? And the main > purpose of the outer parentheses is to get emacs to indent correctly? >



reply via email to

[Prev in Thread] Current Thread [Next in Thread]