Posted on

Simplifying Code

Often I run across code like this:


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

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


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

2 thoughts on “Simplifying Code

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

Leave a Reply