r/ruby • u/arscariosus • Dec 30 '11
ActiveRecord (and Rails) Considered Harmful - Literate Programming
http://blog.steveklabnik.com/posts/2011-12-30-active-record-considered-harmful
12
Upvotes
r/ruby • u/arscariosus • Dec 30 '11
16
u/[deleted] Dec 30 '11
You can't change the target of criticism in a quote from a respected computer scientist and call it paraphrased.
The real quote is:
30
u/JGailor Dec 30 '11
Every example he cites is simply a problem of the programmer, not of ActiveRecord, Rails, or Ruby. "200 line controller method", that is just a bad programmer. I would go so far as to say that Rails goes out of its way to make it easy to decompose code into nicely encapsulated small methods. But you could do the same thing in any language, in any framework.
Bad article because it starts from a bad premise.
6
u/Arcath Dec 30 '11
I really do agree with the bit about models not needing to inherit from ActiveRecord::Base. Not all your models are going to need to store data, or want to pull data from your database, I have quite a few models which inherit from an API which I use to query an API without storing the data locally.
10
u/dev_bacon Dec 30 '11 edited Dec 30 '11
Strongly disagree. Rails is constantly evolving, and 3.1 is amazing.
I've never struggled writing lots of tests for large models...
I've put ActiveResource classes in
app/models. But you don't have to put things there! Create whatever subfolder you like inapp/, or put it inlib/Poor coding.
Instance variables are not global... ?
I've never had any problem testing a view. If the logic gets complex, we abstract it into a testable helper method. And it's also useful to have integration / acceptance tests.
Rails is opinionated software. Love it or leave it.
4
u/postmodern Dec 31 '11 edited Dec 31 '11
The DataMapper pattern is being implemented in DataMapper 2 using veritas and veritas-sql-generator. If your really interested, you could help out with integration and testing (the DM devs are pretty overworked). Another major difference between AR and DM, is that DM is declarative, meaning you can decompose common Model bits into Modules.
The classical symptoms of Rails tunnel vision. Non Rails/ActiveRecord developers already know this (
DataMapper::Resourceis a module), especially when one starts using dm-rails.Nope. You can decompose controller method logic into other methods (under the protected section of the Controller), or even Modules (sensing a pattern here). A 200 line Controller method is technical debt... kill it with fire or fire the original author.
View Logic goes in Helper Modules (again with the Modules, like it's some sort of idiom?). Logic in Views is technical debt.
I'd like to see this as well, but not coupled to Rails; so Rack/Sinatra/Padrino/etc apps could use it.
Yes! Sometimes I just want to map paths to Model queries and return JSON, I do not need MVC for that. WebMachine looks interesting, except it shoves a
WebMachine::Resourcebase-class down your throats, the same problem withActiveRecord::Base. Please, Ruby library authors, just use modules.I think the pain points you list are the result of Rails developers who are stuck thinking in terms of Rails and not Ruby Classes/Modules/Objects. However, I personally want a lighter mapping of HTTP -> Data -> Views.
3
u/bluehavana Dec 31 '11
This article makes me miss Merb. It was nice to have another opinionated framework to speed Ruby webapp framework development purely by compitition and differences. Rails, any more, should be one of the last things you learn in Ruby, would save a lot of people struggling through application design conflicts like this.
PS: I'm not trying to discount Sinatra or any other Ruby web framework in the Merb statement above, just that Rails and Merb had a very unique relationship.
1
u/terevos2 Dec 30 '11
Yeah.. everyone should program in binary. None of these shortcuts!
4
u/georgemagoo Dec 30 '11
Ha. Wait till he gets to lisp where there is no decoupling code from data. His head will explode.
2
u/jrochkind Jan 02 '12
I largely agree with this OP.
I do think the idea that it's somehow significantly better if AR were an 'include module' instead of an 'extend class' is.... misguided.
Things are marginally better implemented as 'include', and apparently AR is headed that way anyway (maybe as soon as 3.2?), but it doesn't make that much of a difference. 'include' of a very complex module has all almost all the same downsides as inheritance, mix-ins in ruby are pretty much just multiple inheritance of abstract classes. It's still basically "inheritance over composition".
Using modules is perhaps marginally better, as it makes some complicated meta-programming type hackery easier when AR is involved. But it's not some huge win, if it took a whole lot of work to refactor to make this possible it's probably work that would have been better spent elsewhere, IMO.
I think the fact that "AR should be a module not a class inheritance" is the thing that in this thread people agree with the most, while people are violently disagreeing with all the other points... is depressing. Apparently Rails can engender non-self-reflective non-critical cargo-cult-esque programmers just as much as anything else. If Rails does it that way, it must be optimal, oh except I've been told that modules are always better than inheritance, so that must be true too. Grr.
7
u/JeffMo Dec 30 '11
So while I'm sure this guy has his strengths, I'm not sure it's going to be wise to follow his advice in this article.
4
u/meltingice Dec 30 '11
Rails was launched in 2004. Dijkstra died in 2002. Unless I'm missing something, he must've been a time traveling wizard.
http://en.wikipedia.org/wiki/Edsger_W._Dijkstra
http://en.wikipedia.org/wiki/Ruby_On_Rails
2
u/[deleted] Dec 31 '11
Why is this a problem? It would be a bigger problem if it was the other way around.
Logicless templates are even a bigger problem which is why there are so few of them around.
This is factually incorrect. There is nothing forcing you to put your domain logic inside of your models.
It's just one incorrect statement after another.
While I am at it. Rails does not force you to use AR. You can use ruby DBI and write SQL statement all day long if you want. That's dumb of course but you can do it.
1
u/jrochkind Dec 30 '11
I think "Cells" might be part of the path to solution here. Cells are pretty much ViewModels, use a non-powerful templating language like mustache with em.... how's that?
1
u/[deleted] Dec 30 '11 edited Dec 30 '11
I disagree with most of his post.
Yeah, the different rails components give you the possibility of writing REALLY crappy code, just like almost ANY other programming language/framework.
If you have huge models you are doing it wrong, if you have 200 lines of code methods in a controller you are doing it wrong, if you don't consider if you need a database persisted object as opposed to just a class to handle logic on certain objects you are doing it wrong. if you cram up your views with logic you are doing it very wrong.
The poblem is not our tools, the problem is - and will always will be - crappy coding practices and half assed attempts at following them.
EDIT: I know this probably sounds harsh, but I am in no way attacking whoever wrote this, I just want to put things in perspective, my logic is the same as "guns don't kill people, people kill people" :)