5 ms·
There are two opposing schools: One (your?) school says readability is promoted by having as much visible structure as possible. The other (mine!) says readabi
by beagle3 15y ago
There are two opposing schools:
One (your?) school says readability is promoted by having as much visible structure as possible. The other (mine!) says readability is promoted by having as much visible context at the same time, as long as the structure is apparent.
so instead of
if (test1)
{
do_something1();
}
else
{
do_something2();
}
I write
if (test1) do_something1();
else do_something2();
4 times as much context on in the same space. Similarly, where (I assume) you'd write:
if (a == 1)
{
c = "hello";
}
else if (b == 2)
{
c = "goodbye";
}
else
{
c = "...";
}
I write:
c = a==1? "hello":
b==2? "goodbye":
"...";
or even as a one liner:
c = a==1? "hello": b==2? "goodbye": "...";
Even if you are not used to this style, I don't think you can claim it is unreadable. Just different.
- natesm 15y agoIn that sort of situation I would definitely prefer the ternary, although nesting them sans (redundant, but explicit) parenthesis isn't something I would do. I also like the somewhat Rubyesque one liners for simplistic if/else pairs. I just didn't want to write a bunch of nonsense code as an example.