Hi everyone,
You’ll probably see this type of post on every Ruby forum around but I thought I’d bring it up to see what Thoughtbot says
I’ve had this discussion before with my mentor @georgebrock and wanted to get everyone else’s opinion. I’m trying to formulate my own convention around naming service classes based on what Thoughtbotter’s prefer to do.
So how do you name/create your service objects?
Using .call
?
class AcceptInvite
def self.call(invite, user)
invite.accept!(user)
UserMailer.invite_accepted(invite).deliver
end
end
I’m not such a fan of this approach as I don’t think .call
reads well and I don’t tend to inject behaviour via lambdas which this is meant to facilitate. (More on this approach here: http://brewhouse.io/blog/2014/04/30/gourmet-service-objects.html)
Using a class method?
UserSubscription.register(input)
Where you create a class method which delegates to .new
def self.register(input)
new(input).register_subscription
end
Not using a class method?
UserSubscription.new(input).register
…
And then in respect to the larger issue at hand, what naming conventions do you use? Verbs, nouns?
I know quite a few people are against using verbs like UserCreator
. I’m not a fan because you just end up with syntactic vinegar such as UserCreator.call
or UserCreator.create
.
Using just nouns is cool, but it’s not always apparent what you should name things i.e. Registrar
… InviteAcceptor
?
…
The last issue I often see is where service objects often overlap with other types of objects that may use the command pattern or factory pattern? What do you name those types of objects? Do you append Command
or Factory
to them?
I know everyone has their own style of doing this so I’d like to hear how you approach this? My goal is to figure out my own personal convention so I can create some consistency with my service objects.