rubyoopmethodsstate-machinemethod-names

Which of these approaches to method naming is better and why?


In my app, we have Users who can perform actions on one another - like poking on Facebook.

I'm writing the methods just now and am not really sure which approach to take. I mean, I know they're both acceptable but is there a more idiomatic approach?

Option 1

if @current_user.may_poke?(@other_user)
  @current_user.poke!(@other_user)
end

Option 2

if @current_user.may_poke?(@other_user)
  @other_user.poke!(@current_user)
end

The first option reads better in English, almost perfectly as a sentence. The second option makes more sense in terms of method naming, "poke" is a method being performed on the @other_user. The @current_user is just an argument to provide extra info - who did the poking.


Solution

  • I'd stick with Option 1 there.. if you follow the logic of the conditional, then you're asking "If current_user may poke other_user, then have current_user poke other_user". Option 2 doesn't make much sense when thought of in those terms.

    Also.. matz, the author of Ruby, states that "The bang [exclamation point] sign means "the bang version is more dangerous than its non bang counterpart; handle with care"." ( http://www.ruby-forum.com/topic/176830#773946 ). I'd probably just use poke for that method name instead of poke!.