5 ms·
A comment about ternarys: Nested ternarys can be quite readable if they are formatted like nested if-then-elses. Here's an example from some awk code I wrote
by mjcohen 3y ago
A comment about ternarys:
Nested ternarys can be quite readable if they are formatted like nested if-then-elses.
Here's an example from some awk code I wrote about 15 years ago.
vval = ((k > 1 \
? substr(vval, 1, k-1) \
: "" \
) \
substr(vv, length(vpfx)+1, 1) \
vvn \
(k1 <= length(vval) \
? substr(vval, k1) \
: "" \
) \
);
Well, at least I find it readable.
- frodowtf 3y agoWell, I don't.
- tonyg 3y agoYep. Further, ternaries are to cond as switch is to pattern matching. Ternaries can be used to give a chain of boolean expression decisions: a ? b : c ? d : e ? f : g which switch isn't so tidy for.
- o11c 3y agoUnless, of course, you find yourself in one of the languages with broken associativity or precedence. `? :` has to be right-associative, and should have the lowest precedence of any "normal" expression (only assignment and comma expressions are lower). Note however that the middle expression does not care about associativity, though readability suffers if it has a lower precedence than the ternary itself (a ? b = c : d). As far as prettified indentation goes: * if any argument is itself a ternary, force a break before it * if you have to break after `?`, indent * if you have to break after `:`, indent unless the following is a ternary ** Note also that logical if-then-else is not actually the only kind of ternary expression; it's just the only one that C exposes. Common other ternary expressions in some contexts include: bitwise if-then-else (aka select - note that the famous bithacks page lies about xor being cheaper, since it forgets that andnot is a primitive), modular exponentiation, fused multiply-add, bitsearch (unless your type system can supply the needle width), various "foo and right shift" that would otherwise return more than a register (foo = multiply, interleave, lcm, pow) and "foo and left shift" that would return a fraction (foo = div, sqrt, cbrt). Passing in a rounding mode or something can also add an argument to many ops.
- nikita2206 3y agoPeople might not agree with you because they would be looking for a nested ternary and your example only has two adjacent ternaries.
- dissident_coder 3y agothis is something that I would categorize under "horrific"
- metadat 3y agoIt looks like a dialect of chicken scratch. https://www.whinycat.com/cdn/shop/products/chicken_scratch.jpg https://www.whinycat.com/cdn/shop/products/chicken_scratch.j...