3 ms·
In C, I always put the ternary clauses on their own lines prefixed with the operator. This always makes things readable. int result = condition ? v
by junon 5y ago
In C, I always put the ternary clauses on their own lines prefixed with the operator. This always makes things readable.
int result = condition
? value * 12
: something_else();
and in the case where the condition is sufficiently complex:
int result =
(
some_condition()
&& another_condition()
&& yet_another_condition()
)
? value * 12
: something_else();
For me, at least, this is entirely readable. The unfortunate bit is that there is no formatter in existence (yet) that can handle this for C, or really any other language with similar syntax.
Python's "Black" formatter actually does the best job here, yet the python ternary syntax is still very verbose and strange IMO.
- dragonwriter 5y ago> The unfortunate bit is that there is no formatter in existence (yet) that can handle this for C, or really any other language with similar syntax. Prettier does it fine for JS, which uses C-style ternary syntax. > Python's "Black" formatter actually does the best job here, yet the python ternary syntax is still very verbose and strange IMO. To me, its quite natural when used sensibly, since if you drop everything after the if it is the normal-case value. Though I would slightly prefer if the ternary form was: <default-valur> unless <alternative-condition> then <alternative-value> instead of: <default-value> if <default-condition> else <alternative-value>
- junon 5y agoPrettier does okay, it has some weird edge cases though that make certain code entirely unreadable. But they're rare. I still prefer how Black does things, e.g. splitting complex expressions into multiple lines using parenthesis. > Though I would slightly prefer if the ternary form was: Agreed, I do like that much better too.