Linked by Thom Holwerda on Mon 24th Sep 2012 15:07 UTC, submitted by MOS6510
Thread beginning with comment 536387
To view parent comment, click here.
To read all comments associated with this story, please click here.
To view parent comment, click here.
To read all comments associated with this story, please click here.
Nitpick: "Trigraphs" -> http://de.wikipedia.org/wiki/Trigraph
You really meant "Ternary Operator", often called "The Ternary [Operator]", because there is only one in both C and C++.
Then: Instead of multiply by -1, just use the unary negation operator.
I disagree about the bad readability of the ternary operator. It is just as readable as an if-statement or class-declarations. The one thing you have to do, like with all other things in C++, you just learn it. That's it.
As with if-statements, while-loops and everything else, you only have to know when to use, and not exaggerate it (again, like with other things in C++).
Apart from that, I totally agree to your last three points.
You really meant "Ternary Operator", often called "The Ternary [Operator]", because there is only one in both C and C++.
Then: Instead of multiply by -1, just use the unary negation operator.
I disagree about the bad readability of the ternary operator. It is just as readable as an if-statement or class-declarations. The one thing you have to do, like with all other things in C++, you just learn it. That's it.
As with if-statements, while-loops and everything else, you only have to know when to use, and not exaggerate it (again, like with other things in C++).
Apart from that, I totally agree to your last three points.
Yes, ternary operator, thanks for pointing it out, I didn't even notice my mistake.
I find ternary operator less readable than if-else block, for anything longer than:
bool ? 0 : 1
In this specific case, I see no reason in condensing function to one line by using ternary operator.
I prefer:
int funct (int num)
{
if (num < 0)
retrun num*(-1)
return num
}
It just takes less time and effort to read that than this:
int funct(int num){return (num<0)?num*(-1):num;}
Again, it is a personal preference, and ternary operator certainly has its place (and I do use it) in C/C++.
As with if-statements, while-loops and everything else, you only have to know when to use, and not exaggerate it (again, like with other things in C++).
Absolutely, that was exactly my point.
Then: Instead of multiply by -1, just use the unary negation operator.
Even better, I actually first read comment by Neolander, and followed what he said (trigraph and *(-1)). I guess lack of sleep kicked in and I didn't notice there was even simpler solution.
BR
Loreia
I think you can write even complex ternary operator statements with only a good format:
return x ? foo :
y ? bar :
z;
(sorry, still don't know how to verbatim here)
I think what we discuss about is really personal preference, because I (and other proponents) find it not harder to read. To us, it really was only a matter of learning 




Member since:
2009-08-13
Nitpick: "Trigraphs" -> http://de.wikipedia.org/wiki/Trigraph
You really meant "Ternary Operator", often called "The Ternary [Operator]", because there is only one in both C and C++.
Then: Instead of multiply by -1, just use the unary negation operator.
I disagree about the bad readability of the ternary operator. It is just as readable as an if-statement or class-declarations. The one thing you have to do, like with all other things in C++, you just learn it. That's it.
As with if-statements, while-loops and everything else, you only have to know when to use, and not exaggerate it (again, like with other things in C++).
Apart from that, I totally agree to your last three points.
Edited 2012-09-25 08:59 UTC