text stringlengths 0 444 |
|---|
# good |
SOME_CONST = 5 |
---- |
=== Predicate Methods Suffix [[bool-methods-qmark]] |
The names of predicate methods (methods that return a boolean value) should end in a question mark (i.e. `Array#empty?`). |
Methods that don't return a boolean, shouldn't end in a question mark. |
[source,ruby] |
---- |
# bad |
def even(value) |
end |
# good |
def even?(value) |
end |
---- |
=== Predicate Methods Prefix [[bool-methods-prefix]] |
Avoid prefixing predicate methods with the auxiliary verbs such as `is`, `does`, or `can`. |
These words are redundant and inconsistent with the style of boolean methods in the Ruby core library, such as `empty?` and `include?`. |
[source,ruby] |
---- |
# bad |
class Person |
def is_tall? |
true |
end |
def can_play_basketball? |
false |
end |
def does_like_candy? |
true |
end |
end |
# good |
class Person |
def tall? |
true |
end |
def basketball_player? |
false |
end |
def likes_candy? |
true |
end |
end |
---- |
=== Dangerous Method Suffix [[dangerous-method-bang]] |
The names of potentially _dangerous_ methods (i.e. methods that modify `self` or the 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. |
[source,ruby] |
---- |
# bad - there is no 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 |
---- |
=== Relationship between Safe and Dangerous Methods [[safe-because-unsafe]] |
Define the non-bang (safe) method in terms of the bang (dangerous) one if possible. |
[source,ruby] |
---- |
class Array |
def flatten_once! |
res = [] |
each do |e| |
[*e].each { |f| res << f } |
end |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.