Favor ternary operators #26

Merged
merged 3 commits into from Sep 18, 2011

Conversation

Projects
None yet
3 participants

ghost commented Sep 18, 2011

Since the ternary operator is more commen than if/then/else/end
constructs, I propose to change the rule regarding these to favor the
ternary operator over if/then/else/end constructs.

I hacked a little bash script to compare the usage of both:

for regex in '^.* \? .* : .*$' '^.*if.*then.*else.*end.*$'
do
 echo "/$regex/"
 ack --ruby -cl "$regex" | awk 'BEGIN{ORS="+"; FS=":"}{ print $2 }' |
sed 's/.$//' | bc
done

Comparison

Rails 3.1

/^.* \? .* : .*$/
664
/^.*if.*then.*else.*end.*$/
0

Rubinius 2.0.0pre

/^.* \? .* : .*$/
808
/^.*if.*then.*else.*end.*$/
19

Ruby 1.9.2-p290

/^.* \? .* : .*$/
429
/^.*if.*then.*else.*end.*$/
18
@ralph

This comment has been minimized.

Show comment Hide comment
@ralph

ralph Sep 18, 2011

I also prefer the ternary operator, and never found it hard to read. Usage in common libs seems to second this preference. So +1 from me.

ralph commented Sep 18, 2011

I also prefer the ternary operator, and never found it hard to read. Usage in common libs seems to second this preference. So +1 from me.

@bbatsov

This comment has been minimized.

Show comment Hide comment
@bbatsov

bbatsov Sep 18, 2011

Owner

I'll meditate on that. While I have nothing against the ternary operator in general, I do feel that if/then/else is a more readable construct. I've never doubted the fact that the ternary operator is in wider use, but still - the most popular choice is not automatically the best one...

Owner

bbatsov commented Sep 18, 2011

I'll meditate on that. While I have nothing against the ternary operator in general, I do feel that if/then/else is a more readable construct. I've never doubted the fact that the ternary operator is in wider use, but still - the most popular choice is not automatically the best one...

This comment has been minimized.

Show comment Hide comment
@ghost

ghost Sep 18, 2011

It does not have to be more readable because it looks like an english sentence. The ternary operator offers a clear seperation of both branches, while if/then/else/end looks just like a hotchpotch of english words.
The ternary operator is easier to parse for us humans, because we can differentiate more easily between '?' and ':' than we could between 'if' and 'else'.

ghost commented Sep 18, 2011

It does not have to be more readable because it looks like an english sentence. The ternary operator offers a clear seperation of both branches, while if/then/else/end looks just like a hotchpotch of english words.
The ternary operator is easier to parse for us humans, because we can differentiate more easily between '?' and ':' than we could between 'if' and 'else'.

@ralph

This comment has been minimized.

Show comment Hide comment
@ralph

ralph Sep 18, 2011

I always prefer the ternary operator if the line will still fit into the 80 characters limit. Otherwise I use multiline if/else/end constructs. This seems to be popular, but I didn't do any empiric research like mkorfmann did.

ralph commented Sep 18, 2011

I always prefer the ternary operator if the line will still fit into the 80 characters limit. Otherwise I use multiline if/else/end constructs. This seems to be popular, but I didn't do any empiric research like mkorfmann did.

@DAddYE

This comment has been minimized.

Show comment Hide comment
@DAddYE

DAddYE Sep 18, 2011

Is what I said in my closed pull request, I never saw before (other than coffee) this coding style, so I can't understand how can be some thing like a standard.

+1 ternary operators

DAddYE commented Sep 18, 2011

Is what I said in my closed pull request, I never saw before (other than coffee) this coding style, so I can't understand how can be some thing like a standard.

+1 ternary operators

@bbatsov

This comment has been minimized.

Show comment Hide comment
@bbatsov

bbatsov Sep 18, 2011

Owner

As usual - "vox populi, vox dei". Ternary operator it is :-)

Owner

bbatsov commented Sep 18, 2011

As usual - "vox populi, vox dei". Ternary operator it is :-)

bbatsov added a commit that referenced this pull request Sep 18, 2011

@bbatsov bbatsov merged commit 0fb6387 into bbatsov:master Sep 18, 2011

marocchino pushed a commit to marocchino/ruby-style-guide that referenced this pull request Aug 11, 2015

pocke pushed a commit to pocke/ruby-style-guide that referenced this pull request May 20, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment