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;
}
Name:
Kao-Moji2010-01-09 18:09
public final int luckySum(final int a,final int b,final int c) {
return a == 13 ? 0 : b == 13 ? a : c == 13 ? a + b : a + b + c;
}
public final int intMax(final int a, final int b,final int c) {
return a > b ? a > c ? a : c : b > c ? b : c;
}
public final int max1020(int a,int b) {
if (b > a) { b ^= a; a ^= b; b ^= a; }
return inRange(a) ? a : inRange(b) ? b : 0;
}
private final boolean inRange(final int n) {
return n >= 10 && n <= 20;
}
public final String endUp(final String str) {
int bounds = str.length() - 3;
return str.length() < 4 ? str.toUpperCase() : str.substring(0,bounds) + str.substring(bounds).toUpperCase();
}
public final int caughtSpeeding(int speed, final boolean isBirthday) {
if (isBirthday) speed -= 5;
return speed <= 60 ? 0 : speed >= 61 && speed <= 80 ? 1 : 2;
}
public final int dateFashion(final int you, final int date) {
return you <= 2 || date <= 2 ? 0 : you >= 8 || date >= 8 ? 2 : 1;
}
Name:
Anonymous2010-01-09 21:45
>>40,41
If you indented those in the right way, they look almost like Lisp without the parens.
Name:
Anonymous2010-01-10 0:38
>>42 Lisp without the parens.
You mean we've finally fixed Lisp?
final final final final final final final final final final final final public final public public final public final final final final final final public final final final final final final final final final final final final final public final public public final public final final final final final final public final final final final final final final final final final final final final public final public public final public final final public final final final final final final final final final final final final final public final public
Name:
Anonymous2010-01-10 1:08
>>44 final define final fib x public
final if final < x 1 public
1
final + final fib final - x 1 public public final fib final - x 2 public public public public public
Name:
Anonymous2010-01-10 1:24
final is the single worst thing in Java if everything else is rendered "tolerable."
I strongly prefer "boiu" because it sounds good in my head.
Name:
Anonymous2010-01-12 12:06
>>47 Performance enhancement is almost always a bad reason to compromise good object-oriented design principles
This is what ENTERPRISE programmers believe.
I am ashamed and appalled, /prog/, that you would be getting your information from anywhere other than the the source. Read your BB&C. The standard is ∬ubio∬.
Sometimes it is but sometimes the ternary operator can cost you a temporary variable or causes strange conversions.
Not JAVA.
Name:
Anonymous2010-03-21 23:28
all the solutions are online.
Name:
Anonymous2010-03-22 16:01
It should technically be obui, as no respectable HTML programmer would put a span inside a formatting tag, also b is always the first for some reason. But buio is fun to say in your head and is easiest to type on a AZERTY keyboard.
>>63
Actually, the obui and buio standards are incompatible, as you can see. For a truly enterprise format, even the strictest HTML Programmer has but no choice!
>>64
Oh, of course! Silly me. But it is still strange how the u and o parts are not italicised by i, isn't it! Observe the parallelity of the lines in the following: biuo buoi
Astounding!!!
>>72,75 false_expr would also be evaluated if true_expr evaluated as false. But it works for stuff like print (debug && "Couldn't desecrate the mangina on line 45!" || "Something bad happened.")
>>82
Perl has a lovely way of solving that. You could also just use nesting.
As much as duplication of facilities is a tiresome path (see Perl), I really think S-expressions should show up in languages that don't strictly need them. The convenience of the ternary op could be unified with regular if/else blocks if done in S-expression form.
I wanted to do something like foo = 4 (bar ? + : -) 2; and it doesn't work. Is there any clever way of making it work without putting the whole addition and subtraction inside the operator?
>>92 foo = 2 + (!bar) << 2;
I like it how you posted a solution that applies only to the particular case of addition/subtraction even though they were an obvious example.
>>94
Whatever you meant when you said that, it's easy to simplify whatever similar mathematical expression into that form.
Name:
Anonymous2010-07-09 11:46
opium dreams
Name:
Anonymous2010-07-09 11:51
people who `optimize" their programs into a nests of ternary expressions, don't know 'bout CPU Branch Predictors and penalty for not guessing the correct jump.
In fact most optimized program is one with least branches(jumps).