[thelist] RE: What is the ColdFusion equivalent to eval()?

jeff jeff at members.evolt.org
Wed Apr 25 21:13:48 CDT 2001


ray,

:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
: From: rcamden at macromedia.com
:
:  Jeff, you and I have argued in the past
: about best practices, so this should come
: as no surprise to you. :)
:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

none whatsoever.  the specific details of best practice philosophy are often
based on opinion derived from experience.  as our experiences obviously
differ, so will our opinions on the specific details.  with that in mind,
here's where i stand on the issues.

:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
: why do you feel that the 'left hand method'
: is sloppy.
:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

1)  it looks like a string, not a variable name.
    if for no other reason than it messes up the
    color coding in the editing environment.

2)  variable names should be made up of constant
    values (ie, things that are known and
    controlled), not values created on the fly --
    that's what things like index and associative
    arrays are for.

3)  creating variables of unknown name makes for
    much more difficult debugging as they're not
    present in a scope that can be tested (and no,
    the variables scope isn't a struct so it doesn't
    report all the unscoped variables that are
    present within it).

4)  many stricter development environments will
    not permit an evaluation on the left hand side
    unless you're evaluating the entire line.

:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
: To me, it makes a lot more sense, and is
: more readable, the setvariable.
:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

agreed.  however, for reasons 2 & 3 above, i find the use of setvariable()
to also go against my idea of best practices.  if you need to create
variables on the fly, they probably will have a common name with a number
attached to differentiate them.  in that case you're far better off creating
an array with various indexes.

for similar reasons, i caution against the use of the evaluate() function as
well.  with the ridiculous exception of working with queries, all other
elements can be accessed via bracket notation (when you avoid the temptation
of creating variables on the fly).


<rant>

why allaire never bothered to make a query behave like a struct is beyond
me.  for example, to access the values of a column retrieved from the
columnlist:

<cfloop query="getdata">
  <cfloop list="#getdata.columnlist#" index="column">
  #getdata[column]# -
  </cfloop>
  <br>
</cfloop>

it complains with the following misleading error:

   Expression result cannot be converted to a string
   Expressions used inside tags like CFOUTPUT, CFQUERY,
   CFMAIL, etc. must evaluate to a value that can be
   converted to a string for output or dynamic text
   accumulation purposes. Complex objects, such as
   queries, arrays, and COM/DCOM objects, cannot be
   represented as strings.

so, a quick test to see if it's a struct or an array fails.  just for kicks,
#Len(getdata[column])# returns this slightly more illuminating error:

  Cannot convert given value to a struct. Please
  check the validity of the expression

furthermore, why i can't access individual records as array indexes is
beyond me.  i should be able to do something like this:

<cfloop from="1" to="#getdata.recordcount#" index="row">
  <cfloop list="#getdata.columnlist#" index="column">
    #getdata[column][row]#
  </cfloop>
  <br>
</cfloop>

or on a resultset like this:

id name       hex
-- ----       ---
 1 red        #ff0000
 2 blue       #0000ff
 3 green      #00ff00
 4 yellow     #ffff00
 5 teal       #00ffff
 6 purple     #ff00ff
 7 black      #000000
 8 white      #ffffff

i ought to be able to access a value like this:

#getdata['hex'][6]#

and have it return the value in the hex column of the 6th row of the
resultset (#ff00ff)

</rant>


:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
: Secondly, why do you think this is
: nonstandard? As far as I know it isn't.
:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

something i didn't bring up because i recall being called on it last time i
mentioned it, but will mention it again now is something i vaguely recall
someone encountering a memory problem associated with using quoted variable
naming convention.  they changed it to the setvariable() convention and
their memory problems were solved.  then again, maybe it was using the
setvariable function in a <cfset> without assigning it to a temporary
variable (ie bad = <cfset setVariable("foo#num#", "bar#num#")>, good =
<cfset t = setVariable("foo#num#", "bar#num#")>).  i wish i could find the
article on allaire about their recommended best practices, but the site's
search functionality is badly broken right now.

aside from this, i would still caution against the use of either method for
readability and code manageability reasons.

just my 2¢,

.jeff

name://jeff.howden
game://web.development
http://www.evolt.org/
mailto:jeff at members.evolt.org






More information about the thelist mailing list