3 ms·
I think it mostly seems unclear in C because of the syntax. If you could write this in C, would it still seem unclear? int x = if (y == 0) then 7 else 10;
by wtetzner 12y ago
I think it mostly seems unclear in C because of the syntax.
If you could write this in C, would it still seem unclear?
int x = if (y == 0) then 7 else 10;
As opposed to one of these:
int x;
if (y == 0)
x = 7;
else
x = 10;
or
int x = 10;
if (y == 0) x = 7;
It makes everything local, and in my opinion reduces cognitive load.
- hyp0 12y ago(speaking for myself only): I find the ?: syntax clearer... probably because I've known it for so many years. It's the doing several things at once that, for me, makes it unclear. Of your examples, I find the very last one the clearest. Arguably, it's a hack, because it assigns values twice to x - but it feels like the syntax embodies the semantic truth of y==0 being an exception. A pattern matching solution (with a specific and then a general rule) would also embody this, but to me, having the general rule first, with a following exception, seems more intuitive. Disclaimer: "intuitive" is all very subjective, and it's certainly possible that with experience, my intuition could change. But for better or worse, the above is my opinion today! PS: I do think your first example is cool, neat and clever. I understand and feel that aspect of the appeal.