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 09:02:38 -0700

On 04/24/2017 10:34 PM, John W. Eaton wrote:
> While we're on the subject of formatting and whitespace, could we also
> choose a convention for the ?: operator when it is longer than one line?
>
> For example, I typically write them like this:
>
>   x = ((some_long_condition || some_other_longer_condition)
>        ? some + expression - that / is * not + short
>        : another + expression - that / is * also + not - short);
>
> By including the extra parens around the whole expression, Emacs knows
> how to indent this as above (display with a fixed-width font).  I've
> noticed that we now have some files that use this convention instead:
>
>   retval = lower ? octave::math::gammainc (x, a)
>                  : 1.0 - octave::math::gammainc (x, a);
>
> This looks fine, but I don't know how to get Emacs to do this for me. I'd
> rather be able to just hit TAB and have something reasonable happen.  If
> I ask Emacs to indent the above, I get
>
>   retval = lower ? octave::math::gammainc (x, a)
>     : 1.0 - octave::math::gammainc (x, a);
>
> If I surround the expression with (), then Emacs will indent it as
>
>   retval = (lower ? octave::math::gammainc (x, a)
>             : 1.0 - octave::math::gammainc (x, a));
>
> which is OK, but not really great.  By chance, this one lines up neatly,
> but that's not generally what will happen.  So that's why I would
> normally try to get the expression on one line or split before the ? and
> possibly the : if both expressions won't fit on one line.
>
> jwe

I really like having the '?' and ':' line up vertically to show that there
is alternative A and alternative B.  It seems verbose to wrap both the
entire line and the test condition in parentheses, but I'm not strongly
opposed if you want to use that convention.

--Rik



reply via email to

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