Skip to content

Instantly share code, notes, and snippets.

@joakimk
Last active December 19, 2015 22:48
Show Gist options
  • Save joakimk/6029651 to your computer and use it in GitHub Desktop.
Save joakimk/6029651 to your computer and use it in GitHub Desktop.
The functional core, imperative shell pattern as I see it. I use this quite a bit. Makes the code simpler to understand and test.
# Functional core
# Tested with unit tests. Mostly simple state based tests (like: invoice.gross_amount.should == 200).
class BuildInvoice
def build
# build invoice in memory..
end
end
# Imperative shell
# Usually just tested with a single test to ensure it stores data in the database
class InvoiceCreator
def self.create(order)
# load needed data from db
invoice = BuildInvoice.new(order, ...).build
# save changes to db
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment