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: Wed, 26 Apr 2017 09:31:33 -0700

On 04/25/2017 02:51 PM, John W. Eaton wrote:
> On 04/25/2017 05:44 PM, Rik wrote: > >> 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; > > I was only talking about where to break the _expression_ when it won't all fit on one line.  If it will fit on one line, then I wouldn't split it.

Okay, that's a lot better.

I prefer the version wrapped in parentheses if it is going to be a long piece of code.  It helps me understand that the entire right-hand side is a single object.  For example,

x = y == 1;

I find that this can be ambiguous.  Did the programmer mean for x to be a bool value, or did they simply mistype and were actually trying for multiple assignment?

x = (y == 1);  // interpretation 1
x = y = 1;     // interpretation 2

To avoid ambiguity with the tertiary operator I would use parentheses and write

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

so it is clear that however complicated the right-hand side is, I'm just getting a single value out of it.

--Rik



reply via email to

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