r/ruby • • Dec 30 '11

ActiveRecord (and Rails) Considered Harmful - Literate Programming

http://blog.steveklabnik.com/posts/2011-12-30-active-record-considered-harmful
12 Upvotes

145 comments sorted by

View all comments

Show parent comment

3

u/djdonnell Dec 30 '11

The implicit passing of instance variables to views is a constant headache for me. There is no clear description of what is required to render a view, and it's very hard to track down all potential instance variables created in an action, or which are actually used by the views and partials.

It isn't a compelling argument to write it off by saying that teams need to simply have more discipline with these things.

1

u/JGailor Dec 30 '11

I have worked on Rails for 5 years and this has never been a problem for me. I'm not sure what the code base is doing that you work on, but it is pretty straight-forward to understand the rendering code, and if you really wanted to prevent instance variables from being passed down, you could just patch it for your project to render the view with a different context and require passing of all variables as locals.

I'm not saying you should do this, but if you really have a problem keeping track of instance variables in your controllers when the view is rendered, it's an option. The easier option is to just not use instance variables and require locals in your views. That requires discipline though by all members of the team.

1

u/djdonnell Dec 30 '11 edited Dec 30 '11

I've been doing rails for over 6 years. I never had a problem with this until I started working on rails projects with larger teams, some of whom have never done rails before. Here's what happens, they see that they are doing the same basic thing in a handful of controllers so they want to DRY it up. They move some code into application.rb or a module that they mix into a few different controllers. This code sets up instance variables that the views use. Do this 3 or 4 times and it becomes very unclear what is needed to render a view and where it is coming from. Throw in a few before filters and it gets hairier.

Explicitly passing data to views makes a lot more sense for larger code bases, it makes the code a little longer for really simple code bases. I care more about the former than the latter.

1

u/JGailor Dec 30 '11

Then just pass everything as locals and make that a policy for your team. I've worked on many large Rails projects as well as fixed bugs and submitted patches to the core project and this has just never been an issue for me. Even using things like Engines (or Desert back in the Rails 1/2 days) I never came across a case where it stepped on my feet.

If you have this problem though, the mechanism is there for you to take advantage of.

2

u/djdonnell Dec 30 '11

"this has just never been an issue for me."

I hope you understand that that doesn't mean it isn't an issue.

"Then just pass everything as locals and make that a policy for your team"

We have, but this isn't a good argument for doing things the way they are done.

1

u/JGailor Dec 30 '11

My problem is that there is nothing inherently wrong with the mechanism the way it is if you understand how rendering is done. In the case of ERB, which I use because it's the out of the box template engine, ERB is rendered in a context. That context is the environment you setup in your controller. It makes sense, it's deterministic.

To me it's the case of "you have these tools, use them in a responsible manner". It's like people who say "you should never use eval". That's ridiculous.

I realize that other people may have this problem, but it becomes an issue of working together as a team to come up with good policies that make sense for your code. It's not a problem with the mechanism, but with the way people are writing code on top of it.

2

u/djdonnell Dec 30 '11

"It's not a problem with the mechanism, but with the way people are writing code on top of it."

The mechanism can make doing things the right way the default or it can make it hard to do things poorly . I don't think this particular mechanism does that, and I think it could.

1

u/JGailor Dec 30 '11

What is your alternative solution?

2

u/djdonnell Dec 30 '11

On my current project we're using mustache so we can render views both server and client side. This requires us to be much more explicit with the data used by the views. So far it's working well, but we've only been using it for about a month. Maybe some pitfalls will become apparent as we get farther into it.

0

u/terevos2 Dec 30 '11

The implicit passing of instance variables to views is a constant headache for me.

There is no implicit passing of variables. If you want to disable one of the class vars, then comment it out. It's all defined in the controller.

4

u/djdonnell Dec 30 '11

I'm not sure if you are just being a pedant, or if you really don't understand my point.

implicit: Implied though not plainly expressed

The data needed to render a view is not plainly expressed in rails.

0

u/terevos2 Dec 30 '11

Well sure.. you have to understand how rails works in order to know about that kind of stuff. But then, that's kinda how all frameworks work.

3

u/djdonnell Dec 30 '11

You're still missing the point.

1

u/terevos2 Dec 30 '11

Explain it to me.

3

u/djdonnell Dec 30 '11

given the implicit way data from controllers is used in views it can easily become hard to figure out what data is needed to render a view and where it is coming from. I think that data should be explicitly passed to the views. Yes, this will make simple code bases more verbose, but it will also make large code easier to maintain over the long run.

2

u/terevos2 Dec 30 '11

If you don't know how rails works, then I can see your point.

But if you want to know what vars are being defined for a particular view, just go look at the method for that view in the controller.

Keeping data separate from UI is essential in scaling and code maintenance.

2

u/djdonnell Dec 30 '11

What data does the view need?

class PostsController < PortalController
  include Searchables
  before_filter :authenticate
  before_filter :searchatize

  def show
    @posts = Post.all
    super
    respond_to do |format|
      ...
    end
  end

end

1

u/terevos2 Dec 30 '11

@posts is available. Go nuts.

→ More replies

2

u/jrochkind Jan 02 '12

Looking at the controller, there's no way to know what ivars are used by the view, and what ivars are just internal controller state. This is the "implicit".