ruby-style-guide icon indicating copy to clipboard operation
ruby-style-guide copied to clipboard

Sugesstion: method name should make clear how many results are expected

Open dennisvandehoef opened this issue 6 years ago • 1 comments

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?

dennisvandehoef avatar Jun 12 '19 06:06 dennisvandehoef

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.

bbatsov avatar Jun 12 '19 07:06 bbatsov