I've always had this one issue whenever there is another wave of debates in the Ruby community about the Right Way of doing things.
Everyone keeps talking about the patterns, what's good and what's bad, but they almost never show their real production code.
Instead, people come up with simple examples that don't actually help understand the Right Way.
In today's Keep email views out of the models article, DHH provides the following example:
class PeopleController def create @person = Person.create(params[:person]) Notifier.welcome(@person).deliver end end class Admin::PeopleController def create @person = Person.create(params[:person]) Notifier.welcome(@person).deliver end end
Which, as David explains, needs no refactoring - you can just keep the repeating code in two places.
The problem I have with such made up examples: they teach nothing.
Okay, I get it. I can keep two repeating lines of code in different actions. Can I keep three? Four? What is the limit after which I should consider using DRY?
Here is code from a real application:
def create message = Message.create! message_params message = MessageTranslator.new(message).translate! MessageMailer.created(message).deliver MessagePusher.new(message).push! end
Right now it's all in one place. But what do I do when I need to have this code in two places? My first thought is to extract this code into a MessageCreator service object, because, well, having that much repeating code seems like too much.
My next thought is to find out what other bright rubyists are doing, maybe I missed something?
So what do they actually do in real applications?
Who the fuck knows!
People don't show the real code and instead keep using very simple examples.
The examples that are not hard to deal with in a real application. You can't really learn from them.
But I want to learn!
So, I hereby challenge the Ruby community: the next time you want to tell the world about the right way of doing things, start with showing your production code.
That's right, the dreaded 'production code'.
Don't make up examples. Show us what you actually do.
For example, instead of using the notorious 'create a record and send an email' example, David could show a similar but more complex controller from Basecamp. A piece of code that actually made the team argue about the right way.
Then we all will learn something.
You're the 6029th person to read this article.