Page 1 of 1
Ternary Operator Poll

Posted:
12 Sep 2011, 14:55
by Rob Cashwalker
Ternary Operator:
- Code: Select all
condition ? operation if true : operation if false;
http://en.wikipedia.org/wiki/Ternary_operationPros:
Single line of code for basic functionality for example:
Sometimes I've needed a final variable, but I need it assigned based on a condition. A ternary operator is much simpler than an if-else with a temp variable.
Cons:
Confusing to new programmers
Simpler to modify if-else block in future
Discuss and Vote!
Re: Ternary Operator Poll

Posted:
12 Sep 2011, 23:34
by slapshot5
I voted allow because of this:
Sometimes I've needed a final variable, but I need it assigned based on a condition. A ternary operator is much simpler than an if-else with a temp variable.
It's just cleaner. Outside of this, I've never had use for them.
-slapshot5
Re: Ternary Operator Poll

Posted:
12 Sep 2011, 23:43
by Hellfish
I voted allow. I do agree with Con #1 but IMO it is not *that* complicated to learn to use, once you stumble upon it. As for Con #2, it's a couple of seconds worth of cut & pasting at most to convert it if need be, it doesn't bother me.
Re: Ternary Operator Poll

Posted:
13 Sep 2011, 01:03
by friarsol
I voted allow as well. I think Con # 1 isn't a valid concern. New programmers don't know a lot of stuff, but that shouldn't be a reason to limit how others program. When I was a new programmer I loved stumbling on new ways to make my code more concise.
Re: Ternary Operator Poll

Posted:
13 Sep 2011, 02:20
by Max mtg
I voted allow, because... it was my idea originally to review checkstyle limitations.
Con #1: new progs would learn it on sight
#2: don't be lazy. If the condition clause seems to be too long, declare a boolean for it.
Re: Ternary Operator Poll

Posted:
13 Sep 2011, 05:46
by jendave
Interesting poll since ternary operators have always been allowed. There is not currently nor has ever been a restriction on them.
Re: Ternary Operator Poll

Posted:
13 Sep 2011, 13:26
by Rob Cashwalker
OK, who voted "What's a Ternary Operator?"... show yourself!
Dave, I wouldn't have given it much consideration either until it was mentioned that it could be restricted.... Then I thought about it and read up on the topic. And read it again, because it still wasn't clear. Then actually looking for it in code, I kinda got it... So if I'm having this much trouble wrapping my head around it (and I'm a fairly experienced programmer) then at least one other person must be confused too.
If it's the sort of thing that Checkstyle can flag, then flag it.. doesn't mean it MUST be changed... if someone needs it in the code, then can't they add a @suppress code or something?
Re: Ternary Operator Poll

Posted:
13 Sep 2011, 14:06
by DennisBergkamp
I think we currently have some of these in the code anyway (unless they were removed).
Re: Ternary Operator Poll

Posted:
13 Sep 2011, 15:14
by jendave
Rob Cashwalker wrote:If it's the sort of thing that Checkstyle can flag, then flag it.. doesn't mean it MUST be changed... if someone needs it in the code, then can't they add a @suppress code or something?
Up until a few days ago, checkstyle would flag it. The official Sun style guide allows them so I removed the flag per Max's request. @
Suppress can be used but that is ugly in my opinion.