Monday, February 7, 2011

Don't Mix Tags and Script in the Same Component

Every now and then, I'll see some code in an open source ColdFusion project that mixes tag syntax and script syntax in the same component. Personally, I find it really annoying when people jump in and out of cfscript blocks. If the component isn't written entirely in script, then don't use any script - stick with tags.

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.

20 comments:

  1. I 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.

    ReplyDelete
  2. i 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.

    ReplyDelete
  3. I 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.

    Obviously 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.

    ReplyDelete
  4. I guess you're going to hate all my Open Source projects that target CF8 and above....

    ReplyDelete
  5. @Mark,

    I'll try not to look under the hood. :)

    ReplyDelete
  6. I agree with the "Anonymous". I have CFSCRIPT blocks when they are long enough to make my code more readable.

    It 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.

    ReplyDelete
  7. There are some times where I really do prefer using cfscript when I want to do something along the lines of

    args = {
    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

    ReplyDelete
  8. @Emergence/Josh,

    I 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"
    } />

    ReplyDelete
  9. 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
  10. @Ben,

    I would love to see E4X added to cfscript. There are just some things that are much better suited for tags, like queries.

    ReplyDelete
  11. @Tony,

    Yeah, I completely agree with that. Also, CFXML is a winner.

    ReplyDelete
  12. @Ben,

    Not 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?

    ReplyDelete
  13. @Tony,

    CFXML doesn't require any trimming--- but, really, I don't think there is much of a difference.

    ReplyDelete
  14. @Ben,

    I didn't think so, but you've proven me wrong in the past. :)

    ReplyDelete
  15. @Tony,

    I 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?

    ReplyDelete
  16. @Ben,

    Yeah 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.

    ReplyDelete
  17. Ewww - you have to "write" to the output buffer :( Yeah, E4X would be the sweet move right about now.

    ReplyDelete
  18. @Ben,

    Yeah the only useful thing I've found for savecontent in script is to capture the output of a writeDump(). Otherwise it's pretty pointless.

    ReplyDelete
  19. Great thoughts you got there, believe I may possibly try just some of it throughout my daily life.


    ASC Coding

    ReplyDelete

Note: Only a member of this blog may post a comment.