This is more of a theoretical question, but at what point does a project become large enough that concerns like this begin to truly matter? When doing a small, limited-use project, it seems often that trying to follow "best practices" like avoiding fat models would be more trouble than it's worth. And yet I've worked on larger enterprise-scale projects that have most certainly benefited from following this and other…
I don't have any research, just a rule of thumb: refactor when it hurts. In my experience, it's easy to go too far in the other direction, trying to craft perfectly generic code on the first pass. That doesn't work very well. It's important to have a meta-awareness while programming - how firm is your grasp on the code? How confident are you that your changes will do what you expect? At some point, you feel that gras…
Ways to Decompose Fat ActiveRecord Models
11–20 of 21 posts
Re: Ways to Decompose Fat ActiveRecord Models
#12I like Ruby on Rails, but "coming" from a Java background I find it odd to have lots of stuff in the models instead of using utility classes or services.
instead having operation on object doing this and clearly encapsulate behavior and hopefully data as well
Re: Ways to Decompose Fat ActiveRecord Models
#13This is more of a theoretical question, but at what point does a project become large enough that concerns like this begin to truly matter? When doing a small, limited-use project, it seems often that trying to follow "best practices" like avoiding fat models would be more trouble than it's worth. And yet I've worked on larger enterprise-scale projects that have most certainly benefited from following this and other…
class Retailer
...
def metrics
RetailerMatrics.new(self)
end
end
class RetailerMetrics
The biggest danger with any of the patterns from the article is that you start using them before you actually need them. Then you just end up with a lot of complexity for no benefit.Re: Ways to Decompose Fat ActiveRecord Models
#14Ok, time to embarrass myself due to lack of Ruby/programming knowledge. A few questions that perhaps someone can clear up as from my OO perspective this, to me, is all over the place. I must admit I'm only just getting started on Ruby. Firstly, that doesn't look like the strategy pattern to me, isn't it back to front? And even if it were, what the hell are you doing? You do not pass the user to the authenticator, you…
* RE: Strategy pattern. My understanding of the strategy pattern is it simply refers to "algorithms are encapsulate and can be selected at runtime". (http://en.wikipedia.org/wiki/Strategy_pattern)
* RE: New class per object query. Agreed that grouping these can make sense. Had to keep the example brief.
* RE: Events. That's another approach -- thanks for the suggestion.
* RE: "View Model" vs. "View". I had it as "View Model" in the original draft and got feedback from reviewers that they are usually called just "Views". :-) I've heard it both ways .
Thanks for the questions!
-Bryan
Re: Ways to Decompose Fat ActiveRecord Models
#15This is more of a theoretical question, but at what point does a project become large enough that concerns like this begin to truly matter? When doing a small, limited-use project, it seems often that trying to follow "best practices" like avoiding fat models would be more trouble than it's worth. And yet I've worked on larger enterprise-scale projects that have most certainly benefited from following this and other…
I try to follow a gradual approach to decomposition. When I start to get a lot of related methods I wrap them in a module, but still within the model definition(I just include the module immediately after definition). From there I might just move it to a separate file as is or perhaps I turn into a decorator and add an accessor for it on the model e.g. class Retailer ... def metrics RetailerMatrics.new(self) end end…
Re: Ways to Decompose Fat ActiveRecord Models
#16Perhaps it's my lack of training or the projects I work on, but I've never seen a Rails model so large/cumbersome as to justify even one of these techniques. Is this a case of fixing the wrong problem?
There are models which are overly large (the project started as a Rails 1.2 project now at 3.0). The project has moved through different hands and the cruft and bloat has built up.
I look forward to applying some of Bryan's points to these models.
Re: Ways to Decompose Fat ActiveRecord Models
#17Re: Ways to Decompose Fat ActiveRecord Models
#18Cool stuff. @brynary, how do you organize your app? Are all seven techniques dumped into app/models or do you keep each technique in its own directory?
http://blog.codeclimate.com/blog/2012/02/07/what-code-goes-i...
But really, in short, don't worry too much about it. Plan on reorganizing once or twice as you find what works for you.
-Bryan
Re: Ways to Decompose Fat ActiveRecord Models
#19Earlier quoted context omitted.
I don't have any research, just a rule of thumb: refactor when it hurts. In my experience, it's easy to go too far in the other direction, trying to craft perfectly generic code on the first pass. That doesn't work very well. It's important to have a meta-awareness while programming - how firm is your grasp on the code? How confident are you that your changes will do what you expect? At some point, you feel that gras…
That's a great summary. It's easy to lose that meta-awareness, even if you're an experienced coder. You are holding a hammer, and the nail isn't going in, so your first instinct it "hammer harder". But usually it means you need a screwdriver.
Re: Ways to Decompose Fat ActiveRecord Models
#20What if your entities were just objects that held data and did validation, but you let use case objects determine the behavior of your system beyond that? Data persistance at that point is literally persisting your entities to the DB.
Then you use the DB much more like you would a filesystem - to retrieve and save data. It doesn't determine your model, it just stores and retrieves your data.
So, you end up with 3 types of things in this system... entities, use cases, and data gateways.
Your data gateways can still use AR if you want, or something else, it doesn't matter.
This isn't my idea, Uncle Bob lays it out better than I can here: http://blog.8thlight.com/uncle-bob/2012/08/13/the-clean-arch...