Coding Guidelines: Instance variables in views

David Lutterkort lutter at redhat.com
Fri Aug 17 21:12:49 UTC 2012


On Thu, 2012-08-16 at 11:05 -0400, Matt Wagner wrote:
> Hi folks,
> 
> I've noticed that we've been objecting to patches using instance
> variables to pass information to views, so I went to see our coding
> guidelines page[1] to read about why.
> 
> It makes very clear that it's bad, but it doesn't really explain the
> right way. I'd hoped that the Coding Guidelines page would be more of a
> guide on how to do things the right way.
> 
> I've really only found one blog post[2] that addresses the issue, and it
> doesn't really give a great example. It still uses an instance variable
> in the controller, but inside of a view it passes it as a local
> variable. It's more specific, sure, but it's not abundantly clear how
> that actually makes anything better, or why an instance variable from
> the controller to the view is okay but a view to a partial is not okay.

I've seen that statement in the coding guidelines, and have simply been
mystified by it. It seems to take a reasonable rule (reusable partials
shouldn't rely on instance variables) to an absurd extreme (never use
instance variables to pass data to a view)

The dividing line should be whether a particular view/partial is
reusable, or meant to be reusable. As long as a view is only used from
one place, I don't see anything wrong with using instance variables.
Note that that doesn't mean partials should never use instance variables
- there's more reasons than reuse to split part of a view into a partial
(AJAX updates, for example)

David

> [1]
> https://www.aeolusproject.org/redmine/projects/aeolus/wiki/Coding_Guidelines
> 
> [2]
> http://rails-bestpractices.com/posts/27-replace-instance-variable-with-local-variable






More information about the aeolus-devel mailing list