It should have been the default behaviour of the if statement. There should have never been a need to make a ternary operator. There should be no real statements, almost everything should be an expression. if (a) b c should itself return the value. A value which you could further use. If the value is unused, there is no harm, the compiler just frees those resources up. Languages which knew how to do it right, just had almost everything returning a value(think Lisp, ML, but there are many others that do it right), and then you could wire the outputs directly. While I do use the ternary operator in C less than if for aesthetic reasons, even if it has the functionality if should have always had. Its syntax looks uglier than the if statement. Default IF behaviour impedes functional dataflow.
Name:
Anonymous2010-01-08 10:18
>>5
What's "?:"? We are talking about return 1 if n < 3 else fibs(n-1) + fibs(n-2)!
>>11
I don't write Haskell, and I do plenty of work for real, sometimes dirty work myself. I'm not going to argue with you, as you have clearly not programmed in a language where almost everything is an expression.
The ternary operator has its uses, that's why it was built into C and kept in Java. Only to be used it the equivalent if statement is less elegant,and never, ever nest them.
>>18
Don't say his name three times! FrozenVoid will appear.
Name:
Anonymous2010-01-08 17:25
>>15 Only to be used it the equivalent if statement is less elegant
In other words, use it whenever possible.
Name:
Anonymous2010-01-08 17:52
>>10
>virtually the same
Sometimes it is but sometimes the ternary operator can cost you a temporary variable or causes strange conversions. http://dis.4chan.org/read/prog/1239221504
>>23
How can you seriously make the argument that a program requiring you to decode nine lines of code is clearer than a single short line of code that replaces repeated function calls with a few constants? Either this is some kind of knee-jerk reaction to the use of the conditional operator, or you just need to learn to program. Either way, IHBT.
Name:
Anonymous2010-01-08 21:10
>>27
Lacks //handles all the common functions,#defines,#ifdefs and #includes
>>28
One is more abstract and has a higher information density. The other is spread out more reducing the cognition required to interpret it.
Name:
Anonymous2010-01-08 22:13
>>7
I agree. I actually created a better approach to the if ... else ... block statements in C. It makes more sense logically, and is much more functional in flow.
flag = cond ? 1 : 0;
for (i = flag; i == 1; i++)
{
// if condition logic...
}
for (i = flag; i == 0; i++)
{
// else condition logic...
}
This approach is superior because it doesn't divert the logic flow like if else blocks, and only uses the ternary operator. The loops make a lot more sense then conditional code, because they basically say "run it for as many times as it is true or false".
>>31
That's patently false. The information density is precisely the same. Both blocks of code complete the same task. No matter how you quantify "density," there is another definition that will just as certainly show that pro to be a con; there is no clear winner because the two blocks are functionally equivalent.
As far as "spreading out," the ternary operation could also be written
echo 'an';
!$plural ?
echo 'us':
echo 'ii';
This is a matter of taste. Some people prefer
if(!$plural) {
echo 'us';
} else {
echo 'ii';
}
To each their own.
The only reason you find the ternary operator as written to require more "cognition" is because you're not used to seeing it. if/else conditionals are encountered much more frequently because it's more flexible. But for matters of simple if then else one liners, it makes sense to use a simpler notation. Just as we use contractions instead of writing do not, could not, should not every time. These are common problems that benefit from simpler solutions.
Name:
Anonymous2010-01-08 23:50
>>31
One has more room to hide mistakes or dirty tricks, making it necessary to carefully read the whole thing, while there's nothing unexpected that could happen with a variable name and two constants. The situation is even worse when the if statements in question set a variable: anything could happen between the if statements and that variable's use. Underusing the ternary operator is bad style for the same reason that overusing goto is bad style.
theres an emplentation of ternatry operation in scheme?
Name:
Kao-Moji2010-01-09 18:01
About two or three days ago someone started a thread about a website called javabat.com where a user could solve small Java exercises in order to learn the language.
I have decided to swing on some of these exercises with nested ternary operations and finals.
public final boolean posNeg(final int a, final int b, final boolean negative) {
return negative ? a < 0 && b < 0 : a < 0 && b > 0 || a > 0 && b < 0;
}
public final int strCount(final String str, final String sub) {
return str.length() == 0 ? 0 : str.startsWith(sub) ? 1 + strCount(str.substring(sub.length()),sub) : strCount(str.substring(1),sub);
}
public final int count11(final String str) {
return str.length() == 0 ? 0 : str.startsWith("11") ? 1 + count11(str.substring(2)) : count11(str.substring(1));
}
public final int countAbc(final String str) {
return str.length() < 3 ? 0 : str.startsWith("abc") || str.startsWith("aba") ? 1 + countAbc(str.substring(1)) : countAbc(str.substring(1));
}
public final int countX(final String str) {
return (str.length() == 0) ? 0 : (str.startsWith("x")) ? 1 + countX(str.substring(1)) : countX(str.substring(1));
}
public final int redTicket(final int a, final int b, final int c) {
return a + b + c == 6 ? 10 : a == b && a == c && b == c ? 5 : a != b && c != a ? 1 : 0;
}
public final int blackjack(int a, int b) {
if (b > a) { b ^= a; a ^= b; b ^= a; }
return a > 21 ? b > 21 ? 0 : b : a;
}