Permalink
Browse files

added some clarifications to the dangerous/safe methods rule

  • Loading branch information...
Bozhidar Batsov
Bozhidar Batsov committed Apr 11, 2012
1 parent 32cb8c6 commit 5abeb82e8fc25e015bcd431f59ed4faf536d0b87
Showing with 47 additions and 1 deletion.
  1. +47 −1 README.md
View
@@ -570,7 +570,53 @@ syntax.
should end in a question mark.
(i.e. `Array#empty?`).
* The names of potentially "dangerous" methods (i.e. methods that modify `self` or the
- arguments, `exit!`, etc.) should end with an exclamation mark.
+ arguments, `exit!` (doesn't run the finalizers like `exit` does), etc.) should end with an exclamation mark if
+ there exists a safe version of that *dangerous* method.
+
+ ```Ruby
+ # bad - there is not matching 'safe' method
+ class Person
+ def update!
+ end
+ end
+
+ # good
+ class Person
+ def update
+ end
+ end
+
+ # good
+ class Person
+ def update!
+ end
+
+ def update
+ end
+ end
+ ```
+
+* Define the non-bang (safe) method in terms of the bang (dangerous)
+ one if possible.
+
+ ```Ruby
+ class Array
+ def flatten_once!
+ res = []
+
+ each do |e|
+ [*e].each { |f| res << f }
+ end
+
+ replace(res)
+ end
+
+ def flatten_once
+ dup.flatten_once!
+ end
+ end
+ ```
+
* When using `reduce` with short blocks, name the arguments `|a, e|`
(accumulator, element).
* When defining binary operators, name the argument `other`.

0 comments on commit 5abeb82

Please sign in to comment.