Permalink
Browse files

Add requirement of parens when using assignment in a conditional.

Closes #53.
  • Loading branch information...
1 parent 299d937 commit d06c70d9dd7517e21e1f9077e8d35297f63d7ee1 Amiel Martin committed Jan 6, 2012
Showing with 17 additions and 2 deletions.
  1. +17 −2 README.md
View
@@ -338,7 +338,9 @@ You can generate a PDF or an HTML copy of this guide using
end
```
-* Don't use parentheses around the condition of an `if/unless/while`.
+* Don't use parentheses around the condition of an `if/unless/while`,
+ unless the condition contains an assignment (see "Using the return
+ value of `=`" below).
```Ruby
# bad
@@ -350,6 +352,11 @@ You can generate a PDF or an HTML copy of this guide using
if x > 10
# body omitted
end
+
+ # ok
+ if (x = self.next_value)
+ # body omitted
+ end
```
* Omit parentheses around parameters for methods that are part of an
@@ -448,10 +455,18 @@ You can generate a PDF or an HTML copy of this guide using
- 2
```
-* Using the return value of `=` (an assignment) is ok.
+* Using the return value of `=` (an assignment) is ok, but surround the
+ assignment with parenthesis.
```Ruby
+ # good - shows intented use of assignment
+ if (v = array.grep(/foo/)) ...
+
+ # bad
if v = array.grep(/foo/) ...
+
+ # also good - shows intended use of assignment and has correct precedence.
+ if (v = self.next_value) == "hello" ...
```
* Use `||=` freely to initialize variables.

0 comments on commit d06c70d

Please sign in to comment.