22

What do you see as the pros and cons of using callbacks for domain logic? (I'm talking in the context of Rails and/or Ruby projects.)

To start the discussion, I wanted to mention this quote from the Mongoid page on callbacks:

Using callbacks for domain logic is a bad design practice, and can lead to unexpected errors that are hard to debug when callbacks in the chain halt execution. It is our recommendation to only use them for cross-cutting concerns, like queueing up background jobs.

I would be interested to hear the argument or defense behind this claim. Is it intended to apply only to Mongo-backed applications? Or it is intended to apply across database technologies?

It would seem that The Ruby on Rails Guide to ActiveRecord Validations and Callbacks might disagree, at least when it comes to relational databases. Take this example:

class Order < ActiveRecord::Base
  before_save :normalize_card_number, :if => :paid_with_card?
end

In my opinion, this is a perfect example of a simple callback that implements domain logic. It seems quick and effective. If I was to take the Mongoid advice, where would this logic go instead?

1
  • Some discussion on these topics can be very polarized. Of course, I don't believe that good software design should degenerate into relativism (e.g. "whatever you think is good is good enough".) One key pattern I look for in advice is this: "if you want to achieve [insert goal here] -- and here is why you should care [insert compelling reason] -- a good strategy to consider is: _____."
    – David J.
    Jun 18, 2012 at 20:43

6 Answers 6

32
+50

I really like using callbacks for small classes. I find it makes a class very readable, e.g. something like

before_save :ensure_values_are_calculated_correctly
before_save :down_case_titles
before_save :update_cache

It is immediately clear what is happening.

I even find this testable; I can test that the methods themselves work, and I can test each callback separately.

I strongly believe that callbacks in a class should only be used for aspects that belong to the class. If you want to trigger events on save, e.g. sending a mail if an object is in a certain state, or logging, I would use an Observer. This respects the single responsibility principle.

Callbacks

The advantage of callbacks:

  • everything is in one place, so that makes it easy
  • very readable code

The disadvantage of callbacks:

  • since everything is one place, it is easy to break the single responsibility principle
  • could make for heavy classes
  • what happens if one callback fails? does it still follow the chain? Hint: make sure your callbacks never fail, or otherwise set the state of the model to invalid.

Observers

The advantage of Observers

  • very clean code, you could make several observers for the same class, each doing a different thing
  • execution of observers is not coupled

The disadvantage of observers

  • at first it could be weird how behaviour is triggered (look in the observer!)

Conclusion

So in short:

  • use callbacks for the simple, model-related stuff (calculated values, default values, validations)
  • use observers for more cross-cutting behaviour (e.g. sending mail, propagating state, ...)

And as always: all advice has to be taken with a grain of salt. But in my experience Observers scale really well (and are also little known).

Hope this helps.

1
  • Excellent answer. Lots of details about pro's and con's and use cases, very helpful!
    – xiy
    Dec 16, 2012 at 21:36
9

EDIT: I have combined my answers on the recommendations of some people here.

Summary

Based on some reading and thinking, I have come to some (tentative) statements of what I believe:

  1. The statement "Using callbacks for domain logic is a bad design practice" is false, as written. It overstates the point. Callbacks can be good place for domain logic, used appropriately. The question should not be if domain model logic should go in callbacks, it is what kind of domain logic makes sense to go in.

  2. The statement "Using callbacks for domain logic ... can lead to unexpected errors that are hard to debug when callbacks in the chain halt execution" is true.

  3. Yes, callbacks can cause chain reactions that affect other objects. To the degree that this is not testable, this is a problem.

  4. Yes, you should be able to test your business logic without having to save an object to the database.

  5. If one object's callbacks get too bloated for your sensibilities, there are alternative designs to consider, including (a) observers or (b) helper classes. These can cleanly handle multi object operations.

  6. The advice "to only use [callbacks] for cross-cutting concerns, like queueing up background jobs" is intriguing but overstated. (I reviewed cross-cutting concerns to see if I was perhaps overlooking something.)

I also want to share some of my reactions to blog posts I've read that talk about this issue:

Reactions to "ActiveRecord's Callbacks Ruined My Life"

Mathias Meyer's 2010 post, ActiveRecord's Callbacks Ruined My Life, offers one perspective. He writes:

Whenever I started adding validations and callbacks to a model in a Rails application [...] It just felt wrong. It felt like I'm adding code that shouldn't be there, that makes everything a lot more complicated, and turns explicit into implicit code.

I find this last claim "turns explicit into implicit code" to be, well, an unfair expectation. We're talking about Rails here, right?! So much of the value add is about Rails doing things "magically" e.g. without the developer having to do it explicitly. Doesn't it seem strange to enjoy the fruits of Rails and yet critique implicit code?

Code that is only being run depending on the persistence state of an object.

I agree that this sounds unsavory.

Code that is being hard to test, because you need to save an object to test parts of your business logic.

Yes, this makes testing slow and difficult.

So, in summary, I think Mathias adds some interesting fuel to the fire, though I don't find all of it compelling.

Reactions to "Crazy, Heretical, and Awesome: The Way I Write Rails Apps"

In James Golick's 2010 post, Crazy, Heretical, and Awesome: The Way I Write Rails Apps, he writes:

Also, coupling all of your business logic to your persistence objects can have weird side-effects. In our application, when something is created, an after_create callback generates an entry in the logs, which are used to produce the activity feed. What if I want to create an object without logging — say, in the console? I can't. Saving and logging are married forever and for all eternity.

Later, he gets to the root of it:

The solution is actually pretty simple. A simplified explanation of the problem is that we violated the Single Responsibility Principle. So, we're going to use standard object oriented techniques to separate the concerns of our model logic.

I really appreciate that he moderates his advice by telling you when it applies and when it does not:

The truth is that in a simple application, obese persistence objects might never hurt. It's when things get a little more complicated than CRUD operations that these things start to pile up and become pain points.

1
  • This is the answer that most speaks to what I have synthesized across several perspectives.
    – David J.
    Jun 20, 2012 at 0:26
2

This question right here ( Ignore the validation failures in rspec ) is an excellent reason why to not put logic in your callbacks: Testability.

Your code can have a tendency to develop many dependencies over time, where you start adding unless Rails.test? into your methods.

I recommend only keeping formatting logic in your before_validation callback, and moving things that touch multiple classes out into a Service object.

So in your case, I would move the normalize_card_number to a before_validation, and then you can validate that the card number is normalized.

But if you needed to go off and create a PaymentProfile somewhere, I would do that in another service workflow object:

class CreatesCustomer
  def create(new_customer_object)
    return new_customer_object unless new_customer_object.valid?
    ActiveRecord::Base.transaction do
      new_customer_object.save!
      PaymentProfile.create!(new_customer_object)
    end
    new_customer_object
  end
end

You could then easily test certain conditions, such as if it is not-valid, if the save doesn't happen, or if the payment gateway throws an exception.

2

In my opinion, the best scenario for using callbacks is when the method firing it up has nothing to do with what's executed in the callback itself. For example, a good before_save :do_something should not execute code related to saving. It's more like how an Observer should work.

People tend to use callbacks only to DRY their code. It's not bad, but can lead to complicated and hard to maintain code, because reading the save method does not tell you all it does if you don't notice a callback is called. I think it is important to explicit code (especially in Ruby and Rails, where so much magic happens).

Everything related to saving should be be in the save method. If, for example, the callback is to be sure that the user is authenticated, which has no relation to saving, then it is a good callback scenario.

2

Avdi Grimm have some great examples in his book Object On Rails.

You will find here and here why he do not choose the callback option and how you can get rid of this simply by overriding the corresponding ActiveRecord method.

In your case you will end up with something like :

class Order < ActiveRecord::Base

  def save(*)
    normalize_card_number if paid_with_card?
    super
  end

  private

  def normalize_card_number
    #do something and assign self.card_number = "XXX"
  end
end

[UPDATE after your comment "this is still callback"]

When we are speaking of callbacks for domain logic, I understand ActiveRecord callbacks, please correct me if you think the quote from Mongoid referer to something else, if there is a "callback design" somewhere I did not find it.

I think ActiveRecord callbacks are, for the most (entire?) part nothing more than syntactic sugar you can rid of by my previous example.

First, I agree that this callbacks method hide the logic behind them : for someone who is not familiar with ActiveRecord, he will have to learn it to understand the code, with the version above, it is easily understandable and testable.

Which could be worst with the ActiveRecord callbacks his their "common usage" or the "decoupling feeling" they can produce. The callback version may seems nice at first but as you will add more callbacks, it will be more difficult to understand your code (in which order are they loaded, which one may stop the execution flow, etc...) and test it (your domain logic is coupled with ActiveRecord persistence logic).

When I read my example below, I feel bad about this code, it's smell. I believe you probably do not end up with this code if you were doing TDD/BDD and, if you forget about ActiveRecord, I think you would simply have written the card_number= method. I hope this example is good enough to not directly choose the callback option and think about design first.

About the quote from MongoId I'm wondering why they advice to not use callback for domain logic but to use it to queueing background job. I think queueing background job could be part of the domain logic and may sometimes be better designed with something else than a callback (let's say an Observer).

Finally, there is some criticism about how ActiveRecord is used / implemented with Rail from an Object Oriented programming design point of view, this answer contain good information about it and you will find more easily. You may also want to check the datamapper design pattern / ruby implementation project which could be replacement (but how much better) for ActiveRecord and do not have his weakness.

3
  • The particular code example merely moves code out of "before_save" callback into the save method. Ok, you "got me" ... you aren't, technically, using a callback, but in practice you still are. See what I mean?
    – David J.
    Jun 18, 2012 at 19:47
  • Adrien, thanks! The question you referred to, Does the ActiveRecord pattern follow/encourage the SOLID design principles? has a GREAT quote: "This leads to a dilemma. On which side of the line does the Active Record really fall? Is it an object? Or is it a data structure?"
    – David J.
    Jun 20, 2012 at 16:44
  • Jim Weirich, at the end of his SOLID Ruby Talk at the 2009 Ruby Conference, asks the audience: "ActiveRecord objects implement a domain concept and a persistence concept. Does this violate the SRP (Single Responsibility Principle)?" The audience agrees that it does violate the SRP. Jim asks if this bothers them. Many members of the audience say yes. Why? It makes testing harder. It makes the persistence object a lot heavier.
    – David J.
    Jun 26, 2012 at 16:34
1

I don't think the answer is all too complicated.

If you're intending to build a system with deterministic behavior, callbacks that deal with data-related things such as normalization are OK, callbacks that deal with business logic such as sending confirmation emails are not OK.

OOP was popularized with emergent behavior as a best practice1, and in my experience Rails seems to agree. Many people, including the guy who introduced MVC, think this causes unnecessary pain for applications where runtime behavior is deterministic and well known ahead of time.

If you agree with the practice of OO emergent behavior, then the active record pattern of coupling behavior to your data object graph isn't such a big deal. If (like me) you see/have felt the pain of understanding, debugging and modifying such emergent systems, you will want to do everything you can to make the behavior more deterministic.

Now, how does one design OO systems with the right balance of loose coupling and deterministic behavior? If you know the answer, write a book, I'll buy it! DCI, Domain-driven design, and more generally the GoF patterns are a start :-)


  1. http://www.artima.com/articles/dci_vision.html, "Where did we go wrong?". Not a primary source, but consistent with my general understanding and subjective experience of in-the-wild assumptions.
2
  • Can you elaborate on "OOP was designed with emergent behavior as a best practice"? Is that statement your spin on it -- or is it something that was actually articulated by the founders of object oriented programming? Do you have a reference to share?
    – David J.
    Aug 16, 2012 at 17:56
  • I'm leaning on Trygve Reenskaug here, but he's credible enough. From the reference in the answer: "We can trace much of our failure to capture the end user mental model of doing to a kind of object mythology that flourished doing the 1980s and into the first half of the 1990s. ... The word of the day was: think locally, and global behavior would take care of itself." I've implemented systems with this implicitly in mind, as do others still (esp. Rails).
    – Woahdae
    Nov 11, 2012 at 21:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.