jump to navigation

NOTE: The spam filter is being unusually aggressive. If you comment does not immediately appear, it has simply been placed in moderation and I will approve it as quickly as possible. Thank you for your patience.

"Murphy was an optimist!"

Simplifying Code August 11, 2010 9:55 pm

Posted by Doug McCaughan in : ColdFusion, Programming, Technology
, trackback

Often I run across code like this:

<cfif ArrayIsEmpty(Cart.Products)>
     <cfset HasCart = False>
     <cfset HasCart = True>

Frankly, that makes me twitch a little. This has the same result:

<cfset HasCart = NOT ArrayIsEmpty(Cart.Products)>

Comments after advertisement


1. flyingboz - August 12, 2010

but the ‘twitchy’ example is readable / easily understood by the next idiot down the line; your one-liner is unintelligible and does not convey the same clarity of thought to the uninitiated.

If you have to write code like that for the speed delta, comment it and go on, otherwise I prefer the former.

2. Doug McCaughan - August 13, 2010

Man akismet is not loving your comments. Everyone one of them I’ve had to moderate. I must change something.

I agree with you on readability speed and comments. I’ll frequently opt for “less efficient” if it means more maintainable down the line. In the actual implementation of the above code, I still think I prefer the one liner but I agree with your point.