On Code Generators

I found Eric Meyer’s “Flummoxed By Frameworks” somewhat comforting. Over the past 5 years I’ve fiddled with a number of frameworks. Some made more sense than others. But they web projects don’t pay the bills around here, and my interest in the related project invariably wanes.

Anyway, I found the Rails tutorial to be pretty interesting. The kind of thing that makes you want to build something! There was a significant amount of hand-waving, but I suppose that’s necessary for a tutorial.

What makes me uneasy about these frameworks (Rails in particular) is auto-generated code. It’s a significant step in the design process: design, generate code, repeat. Two of the half-dozen or so steps in the Ruby tutorial are:

3 – Create a new controller using the “script/generate controller” generator (run with no arguments for documentation).
4 – Create a new model using the “script/generate model” generator

These steps basically generate code (a set of functions) for you to fill in the logic of your application. What happens when someone wants to edit field X? Or call an action on object Y? You answer these questions and viola (by programming), your application works!

But what happens when you didn’t think of everything the first time? Believe it or not, it’s a pretty regular thing to design something one way the first time, but as you go about implementing it decide that things would be much easier to do (or make more sense) if you laid them out this way. You might need to add an action, or change a field/object name. What then? Do I re-generate (and does this wipe out my existing code?), or are the generators only useful for the first whack at a component?

The result is that I get delayed in starting the application because I want to take full advantage of the generator. Furthermore, while the generator is snazzy and all, and makes using the framework look super-easy in the tutorial, using the generator doesn’t teach me how to add actions and fields in the future.

Surely these are not real issues for seasoned Rails programmers. Perhaps the tutorials would do well to introduce generators as one of the last steps: “Remember all of that setup we did before? We can get started a lot faster by using Generators. Let’s see how that’s done…” and so on. (I’m pretty sure it’s possible to write Rails apps without using a generator… at least after the initial Rails app generator.)

One Response to “On Code Generators”

  1. Shawn Veader Says:

    Rails is very nice and the generated code done by the controller and model generators is very trim. (ie: mostly just stub code). However, if you use ./script/generate scaffolding ObjectName then it will generate a bunch of code that you’d have to fix to make it work. Some beginners prefer this method as they see the whole MVC notion and are more comfortable with fixing/changing an existing item rather than building from scratch. That being said, I never use anything but the controller, model, migration and sometimes plugin generators. All that is generated is blank stub files and test files that associate with whatever you create. Drop me a line if you want more insight.

Leave a Reply