Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
class GroupersController < ApplicationController::Base
def create
@grouper = Grouper.new(leader: current_member)
if @grouper.save
ConfirmedGrouperEmails.new(@grouper).deliver
AssignBarForGrouper.enqueue(@grouper.id)
redirect_to home_path
else
render :new
end
end
end
# app/mailers/confirmed_grouper_emails.rb
class ConfirmedGrouperEmails
def initialize(grouper)
@grouper = grouper
end
def deliver
LeaderMailer.grouper_confirmed(member: @grouper.leader.id).deliver
WingMailer.grouper_confirmed(wings: @grouper.wings.pluck(:id)).deliver
AdminMailer.grouper_confirmed(grouper: @grouper.admin.id).deliver
end
end
@adomokos

This comment has been minimized.

Copy link

commented Mar 4, 2014

Why did you make ConfirmedGrouperEmails statefull? Could you just make the #deliver method a class method and pass @grouper to it as an argument?

I guess the real question is: what is your thinking about making objects statefull vs. stateless?

@jonahx

This comment has been minimized.

Copy link

commented Mar 4, 2014

It's not stateful in any meaningful sense. Yes, it has a member variable, but there's no way to mutate it. It is essentially nothing more than syntactic sugar over deliver(grouper)

@tel

This comment has been minimized.

Copy link

commented Mar 4, 2014

I've never understood this style—though it appears popular. There's seems to be no improvement over deliver(grouper). I feel like it's just making use of the fact that you built a class to organize this component so you "may as well" use the initializer.

That said, I think if it were truly just a function it'd be a bit strange since in Ruby you kind of rely on classes to provide semantic grouping.

@clemens

This comment has been minimized.

Copy link

commented Mar 4, 2014

The advantage is quite simple: At some point there might have to be some kind of state – this is where you'd then either convert all consumers of your ConfirmedGrouperEmails API to use the instantiated version or provide a kind of wrapper to have the class method create an instance and deliver it by calling ConfirmedGrouperEmails.deliver(grouper). This is not premature optimization but a real issue.

Simply put: Don't use class methods unless you really have to. Ruby is an OOP language so use OOP.

@jonahx

This comment has been minimized.

Copy link

commented Mar 4, 2014

In this specific example you're right, there's no improvement. But if you were to add other methods beyond deliver, which also make use of @grouper, then you get the improvement of not having to pass the parameter around constantly. It's not really any different than preferring "hello".upcase to something like uppercase("hello")

@dideler

This comment has been minimized.

Copy link

commented Mar 7, 2014

@tel I think the improvement is readability: .deliver(grouper) comes across as delivering a grouper, while .deliver is read as simply delivering whatever it was called on.

@shime

This comment has been minimized.

Copy link

commented May 6, 2014

@eprothro

This comment has been minimized.

Copy link

commented Oct 13, 2015

@shime 👍

Put another way, passing the argument to a class method is not "exemplary" (a la Sandi Metz). So future development efforts (often by other devs) are likely harmed, since this pattern resists refactoring.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.