[thelist] CF: Style or Spec?

jeff jeff at members.evolt.org
Sun Nov 12 20:53:05 CST 2000


frank,

:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
: From: Frank
:
: I've been looking at other people's work, and I
: often see something like this:
:
: 	<cfif #this# EQ #that#>
:
: whereas I have a tendency to use
:
: 	<cfif this EQ that>
:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

the second method is the correct method and by far the easiest to read.
while it seems generally true that you don't have to hash cf variables when
they're used within cf tags, this is not always the case.  the exception is
when the attribute of a tag is expecting a string, number, or list and
you're passing that in with a variable.  in order for it to evaluate it to
the variables value you'd have to hash it up.  on the contrary, the rule
holds fast all the time with <cfif>, <cfelseif>, and <cfset>.  all the rest
of the tags are a toss-up.  you're best figuring out what each tag is
expecting and committing that to memory.

fwiw, omitting the double-quotes from around a cf tags attribute value will
not automatically make the cf server parse it as a variable.  for example,
the code below may not look identical, but it will be executed by the server
in exactly the same fashion.

<cfparam name="user" default=StructNew()>

<cfparam name="user" default="StructNew()">

of course the hope when using the above code is that the variable, user,
will be initialized as a structure.  however, it will actually get
initialized with the string "StructNew()" as it's value.  the proper way,
and the only way to get it to work, is to wrap the cf function in hashes.

<cfparam name="user" default="#StructNew()#">

another example where hashes are necessary

<cfloop from="1" to="#ListLen('a','b','c','d','e','f','h','i')#" index="i">

:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
: It's been my experience that a value within a tag is
: generally evaluated, even without the use of pound
: signs. I tend to omit the pound signs to favour legibility.
:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

proper use is to omit hashes where not necessary.  be careful where you omit
them though.

:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
: Which of the two are a better style?
:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

better style (and the correct style) is to omit hashes.  it also shows that
you're a more experienced cf programmer and understand the parser's
requirements.  newbies will generally hash up everything.

:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
: Are there definite advantages to including the pounds?
:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

only where they're absolutely necessary.

:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
: Does not/including the pounds a violation of spec?
:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

no, if anything i think the cf server will get more particular about syntax
in the future.

hope this helps,

.jeff

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





More information about the thelist mailing list