Topic: Why aren't partials defined more like functions in rails?

Let me start off my saying this is not a "how do you do..." question, it's a question of why rails works a certain way, and can/should it be designed differently: An armchair discussion, if you will.

I'm no rails guru, but while developing rails apps, I've often found myself wondering why the rendering of partials is so ugly. Specifically, I think it would lead to much better coding practice and more readable code if each partial had a function header, which defined the arguments that must be passed in.

For instance, I pulled the following example from the rails 3.0 documentation:

render :partial => "form", :locals => { :zone => @zone }

Wouldn't it be nice if the _form.html.erb partial were defined as

function(zone) {
[...rhtml partial block...]
}

The problem with the current method is that if a partial is called from many places, there is no compile-time enforcement that the correct arguments are being passed in. So instead you end up with runtime errors that don't pop up immediately in testing.

When I say it's ugly, I mean that it reminds me of perl, where a function declaration looks like this:

sub function_name {
my (arg1, arg2) = @_
}

Or, if it's a function written by an idiot, it looks like this:

sub function_name {
[complicated code]
[complicated code]
[complicated code]
    more_complicated_code = complicated_code + @_[0]
[complicated code]
[complicated code]
    more_complicated_code = complicated_code + @_[1]
[complicated code]
}


The argument parsing in perl has no formalized technique, and there is no compile-time argument checking.

"Nice" languages like ruby, in contrast use:

def function_name(arguments){
}

Not only does this allow type checking. It also makes it easy for the caller to quickly determine what the arguments are that need to be passed in.

To find out the arguments that need to be passed into a rails partial, I sometimes need to read through the whole rhtml, and check which variables are not defined. This experience brings back bad memories of my perl days, when I found myself trying to read through functions that were written by idiots.

If someone has seen this issue discussed before, please post it. Or if you have an answer for why partials are done this way, I'd be very interested to hear.

Last edited by jsarma (2011-07-19 16:30:31)