Dispatch from the front lines: What I learned today

I spent the last day of 2008 continuing my work to simplify and streamline the render path in Rails. That work involves moving all path generation to  a single place (instead of requiring that various methods throughout the system generate paths and collect the appropriate templates), and creating a single point of entry into ActionView instead of the numerous points of entry that exist now.

While I was doing this work, I noticed something that is worth discussing in more detail. In Rails 2.x, ActionController takes calls to #render, converts them some, and then calls ActionView::Base#render. It calls into ActionView with an arbitrary hash that is more or less the same hash that was passed into ActionController, with a few changes. However, that same ActionView::Base#render method is the public-facing API for calls to render inside Rails views. As a result, any work that modifies ActionView::Base "internals" has an effect on both the public-facing API of ActionView itself as well as the quasi-private API used for inbound communication from ActionController.

"What I learned today" was that when writing code, there can be seemingly very strong (DRY-related) reasons to try to reuse code in a public interface for private purposes. Resist this temptation and create solid, simple, inter-class APIs. Trying to reuse public-facing APIs for private purposes can make future refactoring very difficult, and isn't really worth the (in the moment) gains.

On the bright side, once I deconstructed the problem, it didn't turn out to be very difficult to get in a refactoring groove. Effectively, the plan is to (1) create a simpler inter-class communication method; (2) slowly (very very slowly) start moving existing calls into the public method into the new method.

In case you're still interested, some specifics:

Apologies if I provided too much information; a lot of this is still in flux but I figured it might be interesting to get some detail about the internals of what I'm working on. Again, you can follow along on the action_view branch of my rails fork on github. Tomorrow, I should have more details on Carl, Michael, Daniel and Lori's work. Sneak preview: Lori is already doing a bunch of really kick-ass work on ORM agnosticism. Keep an eye on her blog for details as well.