Hacker News new | ask | show | jobs
by kupo 4493 days ago
Though I also recommend this book, their example code teaches bad habits (such as assignment within conditionals and horrendous compound statements) which should be avoided in any sort of production code.
1 comments

Do you have any examples? I just flipped through the book and couldn't find any. Accelerated C++ was my first ever programming book. It was quite a doozy to get through at the time, but I feel like I'm forever indebted to it. Koenig's attention to detail and ability to explain what the machine is actually doing and the consequences in your code is still unmatched as far as I can tell.
median = size % 2 == 0 ? (homework[mid] + homework[mid-1]) / 2 : homework[mid];

In my opinion the ternary operator is misused here when an if/else would have been clearer.

Well, you omitted the all important const qualifier which would be a decent reason to use the ternary operator.
I actually like using the ternary operator in this sort of assignment, so I only need to type the variable name once instead of 3 times. The only real problem in your example is that everything is in one line. It woulf be better to indent that and to add some extra parenthesis to make the precedence clearer.
Ah yeah, that is one beefy line of code. I can agree with that.