Skip to content

Commit

Permalink
added some clarifications to the dangerous/safe methods rule
Browse files Browse the repository at this point in the history
  • Loading branch information
Bozhidar Batsov committed Apr 11, 2012
1 parent 32cb8c6 commit 5abeb82
Showing 1 changed file with 47 additions and 1 deletion.
48 changes: 47 additions & 1 deletion README.md
Expand Up @@ -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`.
Expand Down

0 comments on commit 5abeb82

Please sign in to comment.