Liste des Groupes | Revenir à l misc |
On 2024-04-05, Alan Bawden <alan@csail.mit.edu> wrote:That's a key point - and it is not just with the ternary operator, but a general issue. Good spacing, indentation, newlines and layout make code easier to read. But it is vital that there is never any doubt that the layout matches the meaning of the code. If not, then you might be sure what the programmer meant dues to the layout, but you are not sure that the compiler sees it the same way. After all, this looks neat and clear too :Lawrence D'Oliveiro <ldo@nz.invalid> writes:It looks good, undeniably.
>
On Fri, 5 Apr 2024 09:17:37 -0000 (UTC), Muttley wrote:
...
>> a =
>> b ?
>> c ? d : e
>> : f ?
>> g ? h : i
>> : j;
>
> Just use brackets. Saves a lot of pain.
>
a=(b?(c?d:e):(f?(g?h:i):j));
>
A normal programmer would write something like:
>
a = b ? (c ? d : e) :
f ? (g ? h : i) :
j;
>
I.e., she would allow herself to use spaces and newlines, and just
enough parentheses to make the structure clear.
However, I cannot tell at a glance whether or not the nice appearance
isn't telling me some kind of lie. That's an inherent problem with
the ternary operator.
I have to remember that = has lower precedence than ?:. But, ==
has higher precedence. So this careless edit makes it wrong,
even though it still looks just as nice:
a == b ? (c ? d : e) :
f ? (g ? h : i) :
j;
Les messages affichés proviennent d'usenet.