michaelsnowden
michaelsnowden

Reputation: 6192

Naming convention for human readable version of variable

I have a class that has an integer property difficulty. The value is stored as a number between 1 and 3 and is displayed as "Beginner", "Intermediate", or "Expert".

I have a method that converts the integer into one of these more human readable labels, but I'm having trouble coming up with a good name for the method. I prefixed hr_ to the variable's name such as hr_difficulty, but I'm wondering if there's a better convention.

Upvotes: 2

Views: 573

Answers (3)

spickermann
spickermann

Reputation: 106932

I would create a generic method with this signature:

human_option_name(:difficulty)[difficulty]

Because you might need a list of all defficulties anyway (in select options for example) and you may need to include translations later on. That naming is analog to the already existing method human_attribute_name

Upvotes: 1

atomdev
atomdev

Reputation: 321

I think difficulty_in_words will be better as it naturally difficulty in words.

Upvotes: 0

sawa
sawa

Reputation: 168131

To fit well with other parts of the code, particularly how it would look like if it were a method call, I use affix, not prefix e.g., difficulty_s. This implies that you had difficulty, then did something to it, which corresponds to the _s part. hr or "human readable" sounds too specific to me. I think s for "string" is enough.

Upvotes: 1

Related Questions