Showing posts with label code. Show all posts
Showing posts with label code. Show all posts

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.