4 ms·
return if (value >= radix) error.InvalidChar else value; That is just painful to parse. Suffix-If sometimes makes sense and simplifies things, but it always ma
by web007 10y ago
return if (value >= radix) error.InvalidChar else value;
That is just painful to parse. Suffix-If sometimes makes sense and simplifies things, but it always makes code flow more difficult to understand.
- perfunctory 10y agoI don't think this has anything to do with "suffix-if".
- majewsky 10y agoYeah, that's just if as an expression, replacing the ?: operator. Compare to return (value >= radix) ? error.InvalidChar : value;
- naasking 10y agoI actually prefer a ternary arrangement that lets you chain arbitrary such expressions like a truth table: return (value >= radix) ? error.InvalidChar: (comeCondition) ? error.OtherError: ... value;
- drjeats 10y agoI like doing that too, but the Zig syntax doesn't prevent that kind of arrangement, just makes it a bit wider. return if (value >= radix) error.InvalidChar else if (comeCondition) error.OtherError else ... value; or maybe this, but it seems uglier to me: return if (value >= radix) error.InvalidChar else if (comeCondition) error.OtherError ... else value;
- web007 10y agoTrue, which only makes my poorly-targeted point on bad parsability. Reading the code requires backtracking around a conditonal control statement, and in a format more verbose than ternary. Most places I've worked have bans or limits on ternary to single boolean cases for the same reason: It's hard for people to understand the control flow of operations around conditional assignment. A new language that lets you perpetuate painful code isn't helping as much as it could. It's much simpler for people to parse the more verbose case with an IF and two return blocks, and the compiler shouldn't care since it can generate the same code either way.
- gliptic 10y agoI think that is quite subjective. I prefer this version with one return. I don't want more returns than necessary, and I want them as far up in the nesting as possible so they are easy to see. Returns are, after all, control-flow altering.
- deleted 10y ago[deleted]