> > Using SetVariable is better
> >
> > <cfset setvariable("Edname" & numvar, "test")>
>
> I disagree.
>
> Why is it "better"? SetVariable is just another redundant function to
> learn, another syntax to look up. I believe there actually IS some
> circumstance where you MUST use it, but I just can't recall when.
>
> <CFSET "edname#numvar#" = "test"> does exactly what you would
> expect and hope. It's simple (as in Occam's Razor simple), elegant
> and effective. Why do string concatenation using ampersands instead
> of just embedding variables inside your strings? Is it a 'classier'
> technique, perhaps?
I'd disagree with you, here.
In any programming language, you want to avoid "tricks" of the language. The
fact that you can evaluate a string automatically on the left side of a
CFSET is exactly that - it works, but there's no reason to expect it to
continue working in future versions of CF. It's a bit counterintuitive as
well, given that you can't do that in most programming languages that I've
seen.
On the other hand, the SetVariable function is explicitly designed to create
dynamically-named variables. That'll be supported in future versions. In
addition, when you're reading the code and you see "SetVariable", you
instantly know what's going on, while you might not notice variables named
dynamically using string concatenation.
To follow this to its extreme, I'd argue that you should place a variable on
the left side of the CFSET, which Justin's example is missing:
<cfset SetVariable("Edname" & numvar, "test")>
should be
<cfset rs = SetVariable("Edname" & numvar, "test")>
Now, this isn't required either, in the current version of CF, but having a
variable on the left side of the assignment statement is intrinsic to the
idea of using an assignment statement!
As far as whether you're better off using ampersands for string
concatenation or simply evaluating variables within strings, that's up to
you. Six of one, half-dozen of the other. I prefer using ampersands, but I
make no claim of superiority for that method. I find it personally easier to
read.
In addition, I think you're misusing Occam's Razor, or the principle of
parsimony, which refers to the explanatory power of simplicity, rather than
the functional power of simplicity:
"one should not increase, beyond what is necessary, the number of entities
required to explain anything"
Basically, all it means is that if you can explain something with one step,
or with two steps, the explanation with one step is more likely to be
correct, all other things being equal. In any case, as a computer
programmer, you use complexity every day to make things simpler for others -
that's what programming is all about. Of course, there's room for
disagreement here, but I felt the need to make that philosophy major
worthwhile, and I hate to see epistemology gone bad.
Dave Watts, CTO, Fig Leaf Software
http://www.figleaf.com/
voice: (202) 797-5496
fax: (202) 797-5444
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Structure your ColdFusion code with Fusebox. Get the official book at
http://www.fusionauthority.com/bkinfo.cfm
Archives: http://www.mail-archive.com/[email protected]/
Unsubscribe: http://www.houseoffusion.com/index.cfm?sidebar=lists