ruby-style-guide
ruby-style-guide copied to clipboard
Sugesstion: method name should make clear how many results are expected
Personally, I think it should be possible to know if a method is expected to return == 1 or >= 1 results. I was the opinion that it is part of the style guide already, but I could not find it today.
Of course, a method ending with an s can still return only one result, but it should be an array.
# bad
def rating
[1, 6, 3, 10, 5]
end
def ratings
10
end
# good
def ratings
[1, 6, 3, 10, 5]
end
def rating
10
end
def ratings
[10]
end
What are your opinions on this?
I completely agree. I'll just add that apart from arrays we have other collections that might be a reasonable result (e.g. hash, set, etc), plus a multi-value return.