[thelist] cfparam and bracket notation

Joshua OIson joshua at alphashop.net
Thu May 10 09:35:26 CDT 2001


Jeff,

> fwiw, i think that using hashed variable and the dot syntax is dirty (you
> can't access a scoped variable in this fashion, why should you be able to
> set it in this fashion) and hard to read.  any good programmer knows that
> you shouldn't have an unknown in the declaration of a variable (ie, a
> dynamically generated portion of a variable name on the left side of the
> assignment operator (=)).  if you absolutely have to create the variables
> this way, then may i suggest you use bracket notation instead.

> <cfparam name="form[#element#]" default="">

This should be:

<cfparam name="form['#element#']" default="">

Notice the addition of the single quotes around #element#.  Otherwise it
assumes that whatever #element# evaluates to, say 'foobar' is the name of a
variable.  Because this is the case, it is apparent to me that behind the
scenes they are using a technique similar to the evaluate function to make
the assignment of the variables.

Which brings us to another possibility.  Since the statement is "evaluated",
or at least it seems it is, will this work?

<cfparam name="form[element]" default="5">

The answer is no.  It doesn't throw and error and it doesn't make an
assignment (at least in CF Server 4.5.2).  IMHO, bracket notation in
cfparams is losing credibility since it's not functioning consistently with
what we'd expect.

<snip>

> fwiw, i think that using hashed variable and the dot syntax is dirty (you
> can't access a scoped variable in this fashion, why should you be able to
> set it in this fashion) and hard to read.

I do not agree with your insistance that the dot-hash notation is incorrect
in every case--especially within cfparams.  The brackets in this case case
lead to "hard to read" code in this case since you *must* use a hash AND the
ticks.

<cfparam name="form.#element#" default="">

vs.

<cfparam name="form['#element#']" default="">

The former is few keystrokes, few layers of complexity, and in my opinion
will lead to fewer mistakes while coding.  Additionally, since there is no
*benefit* to using brackets, the programmer is now using exactly one format
for all cfparams, which is easier to maintain later if the programmer has to
change between dynamic cfparams and static cfparams.

-joshua










More information about the thelist mailing list