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."
Thread beginning with comment 536387
To view parent comment, click here.
To read all comments associated with this story, please click here.
RE[2]: Sad Truth
by phreck on Tue 25th Sep 2012 08:53 UTC in reply to "RE: Sad Truth"
phreck
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

Reply Parent Score: 2

RE[3]: Sad Truth
by Loreia on Tue 25th Sep 2012 09:20 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

RE[4]: Sad Truth
by phreck on Tue 25th Sep 2012 09:34 in reply to "RE[3]: Sad Truth"
phreck Member since:
2009-08-13

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 ;)

Reply Parent Score: 1