Linked by Thom Holwerda on Mon 24th Sep 2012 15:07 UTC, submitted by MOS6510
General Development "I've been programming professionally for about 3 years at this point, and I've noticed some interesting patterns in other programmers I've worked with. One of the key differentiators among programmers is motivation. I'm not referring to an individual's passion to simply be successful in their career, but rather the type of work they want to pursue. The thing they want to do with computers every day, the types of problems they are interested in solving."
Permalink for comment 536391
To read all comments associated with this story, please click here.
RE[3]: Sad Truth
by Loreia on Tue 25th Sep 2012 09:20 UTC in reply to "RE[2]: Sad Truth"
Loreia
Member since:
2012-01-17

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.


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

Reply Parent Score: 2