Wednesday 24 July 2013

Things I dislike: rules for the sake of rules

G'day:
Rules are great things to have if there's a point to them. However rules just for the sake of it are just dumb. Here's a dumb rule ColdFusion has.

Consider this code:

function f(x){
    var x = "unscoped";
    return {unscoped=x, arguments=arguments.x};
}
writeDump(var=f(x="arguments"), label="f()");

writeOutput("<hr>");

function g(){
    var x = "unscoped";
    return {unscoped=x, arguments=arguments.x};
}
writeDump(var=g(x="arguments"), label="g()");

Here I have two functions: f() and g(). The difference between them is that f() declares an argument, g() does not. In the calling code, I pass the same argument with the same name to both of them.

Within both functions I set a local variable - using the VAR keyword - with the same name as the name of the argument I have declared (in f()), and passed in to both functions.

On Railo and OpenBD (welcome back to my test environment, OpenBD) this code works fine:

Railo:

f()
Struct
ARGUMENTS
stringarguments
UNSCOPED
stringunscoped

g()
Struct
ARGUMENTS
stringarguments
UNSCOPED
stringunscoped


OpenBD:

f() - struct
argumentsunscoped
unscopedunscoped

g() - struct
argumentsarguments
unscopedunscoped

Railo gets it spot on. I'm not sure about the difference in behaviour between the two on OpenBD, but at least it doesn't do this...

The following information is meant for the website developer for debugging purposes.
Error Occurred While Processing Request

X is already defined in argument scope.

Use local to define a local variable with same name.

Which is what ColdFusion does.

My response to this is "so the f*** what?" I don't get an error when I do this:

form.number = "tahi";
URL.number = "rua";
variables.number = "toru";

(or pass the form / URL variable via the appropriate mechanism, and then set the other one in the recieving code). Nor does it complain about this:

arguments.colour = "whero";
local.colour = "kakariki";

It doesn't even complain about this:

var day = "Rāhina";
var day = "Rātū";

(OK, prior to CF9 it would have complained about that too).

Even the issue ColdFusion claims it hasn't isn't really the issue: "X is already defined in argument scope." (what sort of pidgin English is that, btw?). Because as g() demonstrates, having x in the arguments scope isn't actually a problem.

The only problem it is is that some twerp when they were writing the compiler's code parsed decided to institute a pointless rule that one could not have both an argument declaration and a VAR statement with the same name. But as far as runtime is concerned, there is no problem.

Now I have to concede that it's seldom that I want to have a local variable with the same name as an argument, but the occasion does come up occasionally. And indeed it came up for me the other day (whilst testing some odd behaviour I noticed, which will be the topic of another article shortly). But my chief annoyance here is that someone decided to invent a little rule simply for the sake of it.  Growl.

:-|

I'm gonna raise an E/R to get this rule removed... 3600334.

--
Adam