• Create Account

We're offering banner ads on our site from just \$5!

### #ActualÁlvaro

Posted 18 December 2012 - 01:31 PM

What's the point? It's much more complicated to read with no processing time benefit.

Of course there is no processing time benefit, but I would argue that it is [slightly] less complicated.

if' is used to change program flow conditionally, but here we are only using if' to return one value or another. Using a single return' and the ternary operator expresses this better. The structure of (condition1 ? value1 : condition2 ? value2 : value_else) is identical to a chain of if-else statements, and it's equally readable if you get used to it.

I concede that people are often more familiar with if-else than with chains of ternary operators, so perhaps in practice the other solution is better. But perhaps we can educate code readers a bit too...

Perhaps better formatting would make my code more clear. Wikipedia has this example of very readable use of concatenated ternary operators:

vehicle = arg == 'B' ? bus :
arg == 'A' ? airplane :
arg == 'T' ? train :
arg == 'C' ? car :
arg == 'H' ? horse :
feet;

EDIT: Let's try:
return a.dir	!= b.dir	? a.dir    < b.dir :
a.startx != b.startx ? a.startx < b.startx :
a.starty < b.starty;


### #4Álvaro

Posted 18 December 2012 - 01:31 PM

What's the point? It's much more complicated to read with no processing time benefit.

Of course there is no processing time benefit, but I would argue that it is [slightly] less complicated.

if' is used to change program flow conditionally, but here we are only using if' to return one value or another. Using a single return' and the ternary operator expresses this better. The structure of (condition1 ? value1 : condition2 ? value2 : value_else) is identical to a chain of if-else statements, and it's equally readable if you get used to it.

I concede that people are often more familiar with if-else than with chains of ternary operators, so perhaps in practice the other solution is better. But perhaps we can educate code readers a bit too...

Perhaps better formatting would make my code more clear. Wikipedia has this example of very readable use of concatenated ternary operators:

vehicle = arg == 'B' ? bus :
arg == 'A' ? airplane :
arg == 'T' ? train :
arg == 'C' ? car :
arg == 'H' ? horse :
feet;

EDIT: Let's try:
return a.dir	!= b.dir	? a.dir < b.dir :
a.startx != b.startx ? a.startx < b.startx :
a.starty < b.starty;


### #3Álvaro

Posted 18 December 2012 - 01:30 PM

What's the point? It's much more complicated to read with no processing time benefit.

Of course there is no processing time benefit, but I would argue that it is [slightly] less complicated.

if' is used to change program flow conditionally, but here we are only using if' to return one value or another. Using a single return' and the ternary operator expresses this better. The structure of (condition1 ? value1 : condition2 ? value2 : value_else) is identical to a chain of if-else statements, and it's equally readable if you get used to it.

I concede that people are often more familiar with if-else than with chains of ternary operators, so perhaps in practice the other solution is better. But perhaps we can educate code readers a bit too...

Perhaps better formatting would make my code more clear. Wikipedia has this example of very readable use of concatenated ternary operators:

vehicle = arg == 'B' ? bus :
arg == 'A' ? airplane :
arg == 'T' ? train :
arg == 'C' ? car :
arg == 'H' ? horse :
feet;

EDIT: Let's try:
return a.dir	!= b.dir	? a.dir < b.dir :
a.startx != b.startx ? a.startx < b.startx :
a.starty < b.starty;


### #2Álvaro

Posted 18 December 2012 - 01:30 PM

What's the point? It's much more complicated to read with no processing time benefit.

Of course there is no processing time benefit, but I would argue that it is [slightly] less complicated.

if' is used to change program flow conditionally, but here we are only using if' to return one value or another. Using a single return' and the ternary operator expresses this better. The structure of (condition1 ? value1 : condition2 ? value2 : value_else) is identical to a chain of if-else statements, and it's equally readable if you get used to it.

I concede that people are often more familiar with if-else than with chains of ternary operators, so perhaps in practice the other solution is better. But perhaps we can educate code readers a bit too...

Perhaps better formatting would make my code more clear. Wikipedia has this example of very readable use of concatenated ternary operators:

vehicle = arg == 'B' ? bus :
arg == 'A' ? airplane :
arg == 'T' ? train :
arg == 'C' ? car :
arg == 'H' ? horse :
feet;

EDIT: Let's try:
return a.dir    != b.dir    ? a.dir < b.dir
: a.startx != b.startx ? a.startx < b.startx
: a.starty < b.starty;


### #1Álvaro

Posted 18 December 2012 - 01:27 PM

What's the point? It's much more complicated to read with no processing time benefit.

Of course there is no processing time benefit, but I would argue that it is [slightly] less complicated.

if' is used to change program flow conditionally, but here we are only using if' to return one value or another. Using a single return' and the ternary operator expresses this better. The structure of (condition1 ? value1 : condition2 ? value2 : value_else) is identical to a chain of if-else statements, and it's equally readable if you get used to it.

I concede that people are often more familiar with if-else than with chains of ternary operators, so perhaps in practice the other solution is better. But perhaps we can educate code readers a bit too...

Perhaps better formatting would make my code more clear. Wikipedia has this example of very readable use of concatenated ternary operators:

vehicle = arg == 'B' ? bus :
arg == 'A' ? airplane :
arg == 'T' ? train :
arg == 'C' ? car :
arg == 'H' ? horse :
feet;`

PARTNERS