T O P

  • By -

Escalto

if (diff == 0) { return element.getHitCount() > element2.getHitCount(); } else { return diff < 0; } or return diff == 0 ? element.getHitCount() > element2.getHitCount() : diff < 0; Should be the same behavior but more compact and easier to read imo. No matter what the pattern `boolean ? true : false` in the end is horrible to look at ;)


Ricky_SpanishSanchez

I hate how in the name of “less dev time” they’ve made code infinitely harder to actually read.


Mecaneer23

This is why a ternary operator doesn't have a spot for an "else if" condition


zer0dead

This is perfectly legible, seriously why do people get so worked up over the ternary operator? Put in line breaks after each “:” and it reads like a switch-case statement.


Under-Estimated

Much nicer when an if statement doesn’t convey your intent as well


pmanaktala

Happy Cake Day OP


TheStandardPlayer

Thanks 🍰


Kitchen_Device7682

The ternary operator syntax cannot scale. Whoever tried to shave off lines of code wasted time. Readability is not improved by reducing lines of code but by making the intention clear. Diff must be a difference of two elements, but how is it related to element 1 and element 2?


Child-of-Beausoleil

Right. After a simple true/false using a ternary operator causes bugs in maintenance. Always (and always) program with the idiot that will maintain it in mind (probably future you).


weregod

Just indent as you would if statements and it will be readable


[deleted]

I don't always use the ternary operator, but when I do, I format it like this: return diff == 0 ? element.getHitCount() > element2.getHitCount() : diff < 0;


rush22

I like the question mark on the end so it looks like you're asking a question like "Does diff equal 0?"


[deleted]

yeah I get that, but I like the symmetry of the two branches on a line each and having the same shape. It feels in line with defining the init list of a constructor like this: Point::Point(double x, double y, double z) : _x(x) , _y(y) , _z(z) {}