Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

favor ternary operator over if/then/else/end

  • Loading branch information...
commit 5830b65b6d953ad5cabf161f7246237373928268 1 parent be08fb1
Manuel Arno Korfmann mkorfmann authored
Showing with 8 additions and 9 deletions.
  1. +8 −9 README.md
17 README.md
View
@@ -134,29 +134,28 @@ community.
end
```
-* Favor `if/then/else` over the ternary operator. *if* is an
- expression in Ruby and the resulting code is arguably easier to
- read (albeit not as concise). Remember that _"Programs must be written for
- people to read, and only incidentally for machines to execute."_ (Abelson
- and Sussman)
+* Favor the ternary operator over if/then/else construct.
+ It's more common and obviously more concise.
```Ruby
- # good
+ # bad
result = if some_condition then something else something_else end
- # not so good
+ # good
result = some_condition ? something : something_else
```
+* Use one expression per branch in a ternary operator. This
+ also means that ternary operators must not be nested.
* Never use `if x; ...` - it is deprecated in Ruby 1.9. Use
- `if/then/else` instead.
+ the ternary operator instead.
```Ruby
# bad
result = if some_condition; something else something_else end
# good
- result = if some_condition then something else something_else end
+ result = some_condition ? something : something_else
```
* Use `when x then ...` for one-line cases. The alternative syntax
Please sign in to comment.
Something went wrong with that request. Please try again.