It is currently 13 Sep 2025, 14:57
   
Text Size

Ternary Operator Poll

Post MTG Forge Related Programming Questions Here

Moderators: timmermac, Blacksmith, KrazyTheFox, Agetian, friarsol, CCGHQ Admins

Should we allow Ternary Operation or enforce if-else blocks?

Poll ended at 19 Sep 2011, 14:55

Allow Ternary Operation
7
64%
Enforce if-else Blocks Only
3
27%
What's a Ternary Operator?
1
9%
 
Total votes : 11

Ternary Operator Poll

Postby Rob Cashwalker » 12 Sep 2011, 14:55

Ternary Operator:
Code: Select all
condition ? operation if true : operation if false;
http://en.wikipedia.org/wiki/Ternary_operation

Pros:
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!
The Force will be with you, Always.
User avatar
Rob Cashwalker
Programmer
 
Posts: 2167
Joined: 09 Sep 2008, 15:09
Location: New York
Has thanked: 5 times
Been thanked: 40 times

Re: Ternary Operator Poll

Postby slapshot5 » 12 Sep 2011, 23:34

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
slapshot5
Programmer
 
Posts: 1391
Joined: 03 Jan 2010, 17:47
Location: Mac OS X
Has thanked: 25 times
Been thanked: 68 times

Re: Ternary Operator Poll

Postby Hellfish » 12 Sep 2011, 23:43

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.
So now you're
Screaming for the blood of the cookie monster
Evil puppet demon of obesity
Time to change the tune of his fearful ballad
C is for "Lettuce," that's good enough for me
User avatar
Hellfish
Programmer
 
Posts: 1297
Joined: 07 Jun 2009, 10:41
Location: South of the Pumphouse
Has thanked: 110 times
Been thanked: 169 times

Re: Ternary Operator Poll

Postby friarsol » 13 Sep 2011, 01:03

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.
friarsol
Global Moderator
 
Posts: 7593
Joined: 15 May 2010, 04:20
Has thanked: 243 times
Been thanked: 965 times

Re: Ternary Operator Poll

Postby Max mtg » 13 Sep 2011, 02:20

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.
Single class for single responsibility.
Max mtg
Programmer
 
Posts: 1997
Joined: 02 Jul 2011, 14:26
Has thanked: 173 times
Been thanked: 334 times

Re: Ternary Operator Poll

Postby jendave » 13 Sep 2011, 05:46

Interesting poll since ternary operators have always been allowed. There is not currently nor has ever been a restriction on them.
jendave
 
Posts: 307
Joined: 01 Jun 2008, 07:19
Has thanked: 8 times
Been thanked: 21 times

Re: Ternary Operator Poll

Postby Rob Cashwalker » 13 Sep 2011, 13:26

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?
The Force will be with you, Always.
User avatar
Rob Cashwalker
Programmer
 
Posts: 2167
Joined: 09 Sep 2008, 15:09
Location: New York
Has thanked: 5 times
Been thanked: 40 times

Re: Ternary Operator Poll

Postby DennisBergkamp » 13 Sep 2011, 14:06

I think we currently have some of these in the code anyway (unless they were removed).
User avatar
DennisBergkamp
AI Programmer
 
Posts: 2602
Joined: 09 Sep 2008, 15:46
Has thanked: 0 time
Been thanked: 0 time

Re: Ternary Operator Poll

Postby jendave » 13 Sep 2011, 15:14

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.
jendave
 
Posts: 307
Joined: 01 Jun 2008, 07:19
Has thanked: 8 times
Been thanked: 21 times


Return to Developer's Corner

Who is online

Users browsing this forum: No registered users and 55 guests

Main Menu

User Menu

Our Partners


Who is online

In total there are 55 users online :: 0 registered, 0 hidden and 55 guests (based on users active over the past 10 minutes)
Most users ever online was 7967 on 09 Sep 2025, 23:08

Users browsing this forum: No registered users and 55 guests

Login Form