Close and Go BackBack to Viget

The Problem with Scaffolding

Ben Scofield
Ben Scofield, Development Director, February 06, 2008 3

A couple of years ago, DHH‘s fifteen-minute blog screencast introduced Rails to the world. This wonder was accomplished in no small part because of Rails’ scaffolding - code and markup generated by the framework to handle the most common tasks.

Scaffolding - by joelogon

Since that debut, scaffolding in Rails has changed substantially. Dynamic scaffolding (where the generated code lived in memory instead of on the file system), for instance, was pulled out of the core framework a year ago, while static scaffolding (where actual files are created) was updated to reflect the RESTful principles that the community as a whole has been moving towards. Even while the core team made these changes, however, a tension over the goals of scaffolding has emerged.

Cross-Purposes

So what are these goals? Well, on one side, there’s the production-ready faction. These people want scaffolding to resemble Django‘s admin interface, where it’s ready to support a live site out of the box. When they are (inevitably) let down, they opt for or create alternatives like Streamlined and ActiveScaffold.

On the other side, you’ve got the educational faction. Proponents of this want scaffolding to teach new developers best practices for writing Rails code. They’re apt to be big fans of documentation and tutorials, as well, and to be the right people to turn to if you want to know the why of something - like why RESTful controllers have seven actions.

As it turns out, production-ready code, even when it follows best practices, is often not the best way to learn those practices. The problem is that the best practices the educational faction wants to instill underdetermine production code - any application capable of supporting a live site is likely to optimize things, move things around, and in general obscure much of its implementation of best practices behind structures that make it better at supporting a live site (and often easier to maintain).

A Modest Proposal

Given all that, here’s a suggestion: separate the two goals. Abandon the idea that scaffolding can or should be both useful and educational, and rip out everything that detracts from one or the other purpose. My suggestion (for what it’s worth) is to ensure that scaffolded code is in fact production-ready, and is solid for the basic case.

This doesn’t mean that we completely abandon the educational aspect, however. Instead, we should create a sample application expressly tailored to helping new Rails developers learn how best to write their code. It could be distributed via gem or plugin (with a generator or as a resourceful plugin), but it should be separate from Rails core, and easy to find and install.

In the end, clarifying a single purpose for scaffolding will better serve both factions, and the community as a whole.

(photo from joelogon on Flickr)

Trackback: culann.com » Blog Archive » About scaffolding on 02/06 at 08:26 AM [...] a couple of posts and a question directed to the Rails-Core mailing list, it finally looks like the core [...]----- [...] For everyone who ever wondered why their patch adding a form partial to the scaffolding in Rails didn’t get accepted, here’s my take on it. [...]-----
Gil said on 03/07 at 05:18 PM

Good point. One of the things I’ve felt wary about with Rails is that it’s so opinionated about what’s right that developers learning Ruby/Rails as their first language might miss out on clear examples of tradeoffs and bad practices.

Tony Pitale said on 03/12 at 04:59 AM

I was always under the impression that scaffolds were never meant for production. Scaffolds were there to prop up the code that had not been written, yet.

I do like the admin interface in django. It’s clever and it may remove the need for scaffolding during development. However, having a built-in admin interface as a core part of rails should be out of the question. A plugin would be more appropriate, and handy. Many sites have an admin interface that is essentially CRUD and fairly standard.

The solution for scaffolds, I think, is a combination of your idea and a little from the education folks. Rails exists to speed up development, to make it easy and flexible. The fact is, new developers do learn from every bit of code they see. If the form scaffolds, for example, are using form_tag when they should be using form_for, that may instill poor practices. So, the scaffolding needs to keep up with the metaphorical Jones’, when it comes to best Rails’ practices.

However, the scaffold is not meant to be all things to all people. Like you said, it is a basic case and it should remain so.

Ben said on 03/13 at 03:32 AM

Actually, a while after I wrote this, I started a new discussion on the Rails-Core mailing list on the same topic. The conclusion was that scaffolding should be entirely educational, and the core team has indicated that they’ll accept patches that support that goal.

We're the Developers

at Viget Labs. We write about web development trends, tips, best practices, industry events, and our projects — all with an emphasis on Ruby on Rails.

Recent Comments

Tony,

I understand and agree that the back-end shouldn’t output code (html code), and only content. The templates (aka views) should do the trick, but instead of having lot’s of if/else conditionals inside the view, you may just output the following content.

No information available

The template would loop in an array and put all the <li>’s inside the <ul>.
I don’t see anything wrong, nor...