Book Review: Fearless Rails Refactoring

This is my short review of the book Fearless Rails Refactoring, you can also check my notes taken from it here: Clippings from Fearless Rails Refactoring - dsdshome.

It's all about Single Responsibility

When I first started working as a Rails developer, a senior developer told me this idea of Service Object (and also Form Object, Query Object). And I was really really confusing about it. I spent a lot of time searching for this name1 and got few tutorials about it (apparently, it's not in the official guidelines).

After about a year of struggling, I think I finally understand it now: Service Object is just Pure Old Ruby Object (PORO).

  • Put and only put your model related logic in your ActiveRecord classes
  • Put and only put your request related logic in your ActionController classes
  • Put everything else in pure old ruby objects (service objects, form objects, you name it)

From this pattern, we can also see that the idea of Service Object is actually only about Single Responsibility Principle (SRP).

  • You need to define your class's responsibility.
  • No matter if it's inherited from ActiveRecord or ActionController, this inheritance is just an implementation detail.

And I think this book explained this idea pretty well. This book also provides many real-world examples to help your understand this idea better.

Although I think some of the examples can still be refactored, it's still a great book about Service Object.

If you are like me (one-year-ago), wondering what's Service Object and how to write them, please do read this book.