Success = 1 or error = 0

TechBoyJK

Lifer
Oct 17, 2002
16,699
60
91
Hey Guys,

I'm building some custom functions using cfml on OpenBD, but I think this question will fall under general programming logic.

These functions will either be successful, or throw an error.

The way I'm writing them now, there is an explicit error value, as well as an explicit success value.

Each function starts with both being equal to 0. If during the function's flow something breaks, the error value will be set to a string describing the error. Also, if during the flow, the function is deemed successful (error still eq 0 and the work is done with good result) the 'success' value will be set to 1.

I'm wondering though, if the 'success' value is really necessary as through every step of the function I check for errors or unsuccesful queries/functions and will flag the error variable if anything is caught.

If the function works, I can safely say that the error variable will remain set to '0', so why not just look for 'error eq 0' to determine if the function was successful?

Here's an example of a function I've written.

Code:
 	<cffunction name="checkExistsAPIKey" returntype="struct" output="no" access="remote" returnformat="json">
 
 		<cfargument name="userID" type="string" required="true" default="" hint="user email" />
		
		<cfset checkExistsAPIKeyResults=StructNew()>
		<cfset checkExistsAPIKeyResults.error = 0>
		<cfset checkExistsAPIKeyResults.success = 0>
		
		<cfif not isNumeric(arguments.userID)>
			<cfset checkExistsAPIKEYResults.error = 1>
		</cfif>
		
		<cfif checkExistsAPIKEYResults.error eq 0>
		
			<cfset var local = StructNew()>
			<cfset local.userID = "#arguments.userID#">
	
			 <cfquery name="checkExistsAPIKey" datasource="dbAPI" maxRows="1" >
	
				SELECT keyID
				FROM tbl_apiKey
				WHERE userID = <cfqueryparam value="#local.userID#">
	
			</cfquery>
	
			<cfif checkExistsAPIKey.recordcount eq 0>
					<cfset checkExistsAPIKEYResults.error = 1>
			<cfelse>
				<cfset checkExistsAPIKEYResults.success = 1>
			</cfif>
			
		</cfif>
	
		<cfreturn checkExistsAPIKEYResults> 
	
	 </cffunction>

In this function, I think error=0 would return the same result as success=1, as for the function to be successful, it must not catch any errors. I have at the end of each function a check to make sure the function is getting what it's supposed to, and if not, it's flagged as error. So if error=0, I know it was successful.

Or... should I use 'success' and if there is an error, set 'success eq 0'

Thoughts?
 
Last edited:

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
I would worry only about one variable.
Error == 0 - good run
Error \= 0 problem - check error string.

Keeping multiple flags around is looking for trouble
 

Dratickon

Junior Member
May 13, 2012
21
0
0
Yeah, I don't see much point in having both variables as one will always be the opposite of the other. Having the single variable would also help clean up the function a bit as you could simply return 0/1 or false/true and not have to worry about picking out what you want from the returned struct.

As in:
Code:
<cfset userID = 123>
<cfif checkExistsAPIKey(userID)>
    success
<cfelse>
    failure
</cfif>
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
Agree with the above, and would add: only return an error code when the possibility of failure is expected within the normal operating flow of the system, otherwise if the situation causing the error should never occur throw an exception.

Examples would be: The GetCustomer function finds no customer for a given name, return an error code. GetCustomer fails to locate an external file that should always be there, throw an exception.