octave-maintainers
[Top][All Lists]
Advanced

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

Re: formatting ?: operator


From: Daniel J Sebald
Subject: Re: formatting ?: operator
Date: Tue, 25 Apr 2017 11:47:27 -0500
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.7.0

On 04/25/2017 11:35 AM, Rik wrote:
On 04/25/2017 09:19 AM, 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.

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.

It's not necessary to wrap the condition.  My example could have been
written this way:

   x = (some_long_condition || some_other_longer_condition
        ? some + expression - that / is * not + short
        : another + expression - that / is * also + not - short);

I'm curious to know whether you have to do the alignment by hand or if
your editor (vim?) does it for you automatically.


Alas, I just do it by hand.  I checked to see what astyle would do, but it
just aligns the next line to the indent after the '=' sign.

--Rik

I too align by hand because it is usually just a quick one or two tab-presses. However, here is the alignment done by gvim without the use of Tab or Backspace when I turn on both "smartindent" and "cindent":

void junk(void)
{
    x = (some_long_condition || some_other_longer_condition
            ? some + expression = autoindented / per + C
            : another + expression - autoindented);

    y = some_expression_without < brackets
        ? question + mark / aligned + here
        : colon / aligned - here;
}

That is, all the lead characters are moved back and forth depending on context. For example, after "void junk(void)" a CR will align the cursor with 4-space indent, but then a curly bracket '{' moves the character back 4-spaces. Alignment doesn't see quite what I do but it is close. Maybe I'll leave this active for a while and see how it works.

Dan



reply via email to

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