So You Want Cleaner Code? Nesting Conditionals and Booleans
This post continues the infrequent series on Clean Code examples. If you are enjoying this series, come to CFUnited 2010 and CF.Objective() 2010 to catch my presentation, Making Bad Code Good- Part 2, a live version of this series.
The main idea here is to provide practical examples of working code that can be written in a cleaner fashion. You may agree or disagree with what I've written, and I want to hear from you either way in the comments.
If you have a code sample you'd like to see refactored, send it to me through email (if you have my email already) or through the Contact Me form on this blog.
Code Sample
2 <cfargument name="attributes" required="true"/>
3 <cfset var local = structNew() />
4 <cfset local.customerGateway = createObject("component","model.customerGateway").init() />
5 <cfif structKeyExists( client, "customerID" ) AND val( client.customerID ) GT 0>
6 <cfif val( arguments.customerGW.getByID( val(client.customerID ) ).getChallengeQuestionID() ) GT 0>
7 <cfset local.result = "true" />
8 </cfif>
9 <cfelseif structKeyExists( arguments.attributes, "ChallengeAnswer") AND
10 structKeyExists( arguments.attributes, "ChallengeQuestionID") >
11 <cfif structKeyExists( arguments.attributes, "ChallengeQuestionID") AND val( attributes.ChallengeQuestionID ) GT 0>
12 <cfif structKeyExists( arguments.attributes, "ChallengeAnswer") AND len( trim( attributes.ChallengeAnswer ) ) GT 0>
13 <cfset local.cust = arguments.customerGW.getById( client.customerID ) /> />
14 <cfset local.cust.setChallengeQuestionID( attributes.ChallengeQuestionID ) />
15 <cfset local.cust.setChallengeAnswer( attributes.ChallengeAnswer ) />
16 <cfset arguments.customerGW.save( cust ) />
17 <cfset local.result = "true" />
18 <cfelse>
19 <cfset local.result = "false" />
20 <cfset attributes.userMessage= "Please select a security question and provide an answer." />
21 </cfif>
22 </cfif>
23 <cfelse>
24 <cfset local.result = "false" />
25 </cfif>
26 <cfreturn local.result />
27</cffunction>
Read through the entire example and ask yourself the following questions:
- What is the purpose of this code block? What are you using for your inferences?
- Is it clear what this code is doing? Why or why not?
- Can you spot the subtle bug in the code?
So You Want Cleaner Code? Return Statement Execution
I'm going to start an infrequent series on Clean Code examples. If you are enjoying this series, come to CFUnited 2010 and CF.Objective() 2010 to catch my presentation, Making Bad Code Good- Part 2, a live version of this series.
The main idea here is to provide practical examples of working code that can be written in a cleaner fashion. You may agree or disagree with what I've written, and I want to hear from you either way in the comments.
If you have a code sample you'd like to see refactored, send it to me through email (if you have my email already) or through the Contact Me form on this blog.
Code Sample
2 <cfreturn true />
3<cfelse>
4 <cfreturn false />
5</cfif>