Some people think that since writing in script is cleaner, it's cleaner to write part of the function in script than write it using tags. However, if you're declaring your component and functions in tags, switching to script inside the function actually decreases your code readability in my opinion. Take the following hypothetical example, which is sadly all-too-common in certain open source projects:
<cffunction name="setFoo" access="public" output="false" returntype="void">
<cfargument name="foo" required="true" type="string" />
<cfscript>
variables.foo = arguments.foo;
</cfscript>
</cffunction>
<cffunction name="getFoo" access="public" output="false" returntype="string">
<cfscript>
return variables.foo;
</cfscript>
</cffunction>
I find this style of coding ridiculous.
Amen.
ReplyDeleteI recall seeing a lot of this in ColdFusion 5 because it had been reported in some places that using long blocks of CFSETs were faster in script that in tags, so people would open a script block, set a slew of variables, and then jump out again. With newer versions of ColdFusion it all gets compiled down to byte-code, so it no longer really matters from a speed standpoint (not to mention server processors are a lot faster now anyway). I agree jumping in and out is probably not the best approach.
ReplyDeletei don't see really a probelm with mixing cfscript with tags. often i do all variables declaration in a cfscript block and than us the mighty cfquery tag... can't see what's wrong wtih that.
ReplyDeleteI agree that it looks ridiculous when there is just a single statement in the CFSCRIPT block, as in your example, however I think if you have a large chunk of logical processing it can be more readable as CFSCRIPT.
ReplyDeleteObviously in that case writing the component entirely in script would be ideal, but if you're stuck with an old version of CF, I don't see anything wrong with mixing and matching in some circumstances.
I guess you're going to hate all my Open Source projects that target CF8 and above....
ReplyDelete@Mark,
ReplyDeleteI'll try not to look under the hood. :)
I agree with the "Anonymous". I have CFSCRIPT blocks when they are long enough to make my code more readable.
ReplyDeleteIt doesn't make sense to have a CFSCRIPT block for a single line of code, but a long stretch of logical code can (IMO) be more readable as a result of CFSCRIPT.
There are some times where I really do prefer using cfscript when I want to do something along the lines of
ReplyDeleteargs = {
start = 1,
per = 10,
order = "name",
sort = "asc"
}
instead of having to do
args = {}
args.start = 1
args.per = 10
args.order = "name"
args.sort = "asc"
anything for less typing, otherwise I do tend to agree
@Emergence/Josh,
ReplyDeleteI think you're talking about using the implicit struct/array notation, which is definitely a nice shortcut, but still doesn't require you to use cfscript.
<cfset args = {
start = 1,
per = 10,
order = "name",
sort = "asc"
} />
It will be interesting to see what happens if the E4X ever gets added to the language. That will take things in the other direction - mostly script and then "break" into tags when necessary.
ReplyDelete@Ben,
ReplyDeleteI would love to see E4X added to cfscript. There are just some things that are much better suited for tags, like queries.
@Tony,
ReplyDeleteYeah, I completely agree with that. Also, CFXML is a winner.
@Ben,
ReplyDeleteNot sure why, but I usually wrap my xml string in a cfsavecontent then use xmlParse rather than using cfxml. Aside from being slightly less code, is there any other benefit to using cfxml?
@Tony,
ReplyDeleteCFXML doesn't require any trimming--- but, really, I don't think there is much of a difference.
@Ben,
ReplyDeleteI didn't think so, but you've proven me wrong in the past. :)
@Tony,
ReplyDeleteI don't know all that much about CFScript - I know CF9(.0.1??) added saveContent as a script-based tag. Can you currently put XML inside of that?
@Ben,
ReplyDeleteYeah it's kinda clunky though and not any better than just setting it to a basic string since you need to output your string inside the savecontent.
savecontent variable="xml"{
writeOutput("<foo>bar</foo>");
}
var xml = "<foo>bar</foo>";
If I was doing a lot of work with generating XML, I'd probably use a tag-based component.
Ewww - you have to "write" to the output buffer :( Yeah, E4X would be the sweet move right about now.
ReplyDelete@Ben,
ReplyDeleteYeah the only useful thing I've found for savecontent in script is to capture the output of a writeDump(). Otherwise it's pretty pointless.
Great thoughts you got there, believe I may possibly try just some of it throughout my daily life.
ReplyDeleteASC Coding