Tuesday, August 14, 2007

bad / pointless code of the day

So can you spot the bad code here?

<cfparam name="attributes.module_id" default="0">

<cfparam name="attributes.method" default="">

<!--- get all privileges except read --->

<cfif not comparenocase(attributes.method, "Submit")>

<!--- deactivate all locked modules --->

<cfinclude template="qry_deactivate_module.cfm">

<cfif isdefined("attributes.module_id") and attributes.module_id neq 0>

<cfloop list="#attributes.module_id#" index="i">

<cfset module_comments= evaluate("comments_"&"#i#")>

<cfinclude template="qry_insert_lock_module.cfm">

</cfloop>

</cfif>

<cflocation url="index.cfm?fuseaction=#attributes.fuseaction#">

</cfif>



A couple of friends and I like to compete for who has come across the worst code of the day which started an interesting little conversation about how we make use of <cfparam tags. I personally avoid them for as general rule. I was actuly surprised to find out that
<cfparam name="comments_#module_id#" default="">
is syntactically correct.

Since CF6 I've preferred to build an object in the session scope to store these elements and access it via a cfc. In the past couple of months I've moved more towards a more Object Oriented Approach. More to follow I'm sure.

By the way the bad code would be first testing if a variable named 'attributes.module_id' exists and if it doesn't sets a default value using the cfparam tag; then 5 lines later checking to see if that same value isDefined.

No comments: