|
Hi,
I'm new to OO, following code is an
excerpt from an article about OO or CFC for that matter from one of
the best known gurus in this community, my curiosity in this case is not really
about OO but rather code tightness or efficiency,
<cffunction name="feed" access="public" returntype="void" output="false">
<cfargument name="animal" type="Animal" required="true" />
<cfswitch _expression_="#arguments.animal.getFerocity()#">
<cfcase value="friendly">
<cfif arguments.animal.getFoodType() is "meat">
<cfset arguments.animal.eat("meat pellets") />
<cfelseif arguments.animal.getFoodType is "veggie">
<cfset arguments.animal.eat("veggie pellets") />
</cfif>
</cfcase>
<cfcase value="dangerous">
<cfif arguments.animal.getFoodType() is "meat">
<cfset lockAnimal(arguments.animal) />
<cfset arguments.animal.eat("meat pellets") />
<cfset unlockAnimal(arguments.animal) />
<cfelseif arguments.animal.getFoodType() is "veggie">
<cfset lockAnimal(arguments.animal) />
<cfset arguments.animal.eat("veggie pellets") />
<cfset unlockAnimal(arguments.animal) />
</cfif>
</cfcase>
</cfswitch>
</cffunction>I would write the following block of code (from the whole thing) <cfcase value="dangerous">
<cfif arguments.animal.getFoodType() is "meat">
<cfset lockAnimal(arguments.animal) />
<cfset arguments.animal.eat("meat pellets") />
<cfset unlockAnimal(arguments.animal) />
<cfelseif arguments.animal.getFoodType() is "veggie">
<cfset lockAnimal(arguments.animal) />
<cfset arguments.animal.eat("veggie pellets") />
<cfset unlockAnimal(arguments.animal) />
</cfif>
</cfcase>
as
<cfcase
value="dangerous">
<cfset lockAnimal(arguments.animal) />
<cfif arguments.animal.getFoodType() is "meat">
<cfset arguments.animal.eat("meat pellets") />
<cfelseif arguments.animal.getFoodType() is "veggie">
<cfset arguments.animal.eat("veggie
pellets") />
</cfif>
<cfset unlockAnimal(arguments.animal) />
</cfcase>
How about you and why?
DL
|