Last Updated: March 02, 2016
·
340
· bodacious

Method naming tip: Keep implementation details out.

Good:

def store_user_token
  cookies[:user_token ] = { value: user.token, expires: 1.hour.from_now }
end

Bad:

def store_user_token_in_cookie
  cookies[:user_token ] = { value: user.token, expires: 1.hour.from_now }
end

Discussion:

Keep implementation details (how you're doing x) out of method and class names.

In the above example, you might decide in future to change how you're storing the user token, from a cookie store to a cache store:

def store_user_token_in_cookie
  Rails.cache.fetch([user, :token], expires_in: 1.hour.from_now) { user.token }
end

The method name is now misleading and would have to be updated.

2 Responses
Add your response

I think this is similar to a discussion on how code should be self documenting and not rely on a lot of comments within the body of the method. As the implementation changes, then your comments end up suffering from "comment rot".

So I guess this is method name rot! Thanks for sharing, great tip.

over 1 year ago ·

Well said @markitoma. Thanks for reading :)

over 1 year ago ·