So, I came across this article because I am trying to come up with support for putting the smack down on one of my coders and getting him to stop using the ternary operator for everything.
I honestly had no idea that there was even a single coder who thought as I, let alone a group. (though I am a bit miffed that my “source is for humans, not machines” philosophy is not as original as I once thought)
Like pretty much anything in the universe, the issue comes down to approriate usage. The ternary operator is perfect for things like:
String myWords = "You have " + cntItems + cntItems 0 ? “item” : “items” + “in your shopping cart.”
(or the return statements above, THAT is what I call elegant code, as opposed to “how much disconnected crap can I jam on a single 399 column line??”)
It is HORRENDOUS when there are 13 different things, several constructors, a method call or two, a cast and a kitchen sink all sandwiched in a ?:
Again, source is for humnas. Too often, it is used by humans to attept to display how crazy mad cool they are and as a result comes out being utterly useless.
Thanks for the great conversation.