David Sawatske
David Sawatske

Reputation: 111

Bad practice to set parameter variable name of method to the name of the data type in Ruby?

Is it poor practice to name the variable in a method a lowercase version of the class name?

In the case below: string.class ==> String

def title=(string)
  @title = string.split(' ').map.with_index do |word, i|
    i == 0 ? word.capitalize : title_form(word)
  end.join(' ')
end

Upvotes: 0

Views: 67

Answers (2)

ndnenkov
ndnenkov

Reputation: 36110

Depends. You want to be using names as close as possible to the problem domain.

In your case, a more appropriate name for the parameter would be uncapitalized_title, raw_title, unformatted_title, simply title or something similar.

On the other hand, if you were writing a gem that extends String or does something with generic strings, then string is likely the right name.

Upvotes: 3

Alejandro C.
Alejandro C.

Reputation: 3801

It is poor practice, because it's usually not very descriptive. Additionally, since Ruby doesn't actually care that you're passing a string it could be misleading.

Upvotes: 1

Related Questions