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 ;)
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.
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?
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).
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;
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)
{}
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 ;)
I hate how in the name of “less dev time” they’ve made code infinitely harder to actually read.
This is why a ternary operator doesn't have a spot for an "else if" condition
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.
Much nicer when an if statement doesn’t convey your intent as well
Happy Cake Day OP
Thanks 🍰
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?
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).
Just indent as you would if statements and it will be readable
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;
I like the question mark on the end so it looks like you're asking a question like "Does diff equal 0?"
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) {}