Assuming that having a non-existent user is a normal part of your program
flow and not an error condition, then no, this is definitely not the way
you'd do it in Java. Using exceptions for normal (expected) program flow is
considered bad practice and is also a performance killer (throwing
exceptions in Java is very expensive). I'd recommend that you not to it in
CFML either. Since CFML doesn't support the concept of null, a way around
this without using exceptions is to define a "doesUserExist" method that
you'd invoke first before calling "getUser". So a modified version of
Barney's example would look like this:
<cffunction name=doesUserExist" returntype="boolean">
<cfreturn doesUserExistCondition />
</cffunction>
<cffunction name="getUser" returntype="userCFC">
...
<cfif doesUserExist()>
<cfreturn theUser />
<cfelse>
<cfthrow type="UserNotFoundException" />
</cfif>
</cffunction>
Then you'd call it like this:
<cftry>
<cfif doesUserExist()>
<cfset user = getUser() />
.. do stuff ..
<cfelse>
.. deal with the invalid user ..
</cfif>
<cfcatch type="UserNotFoundException">
.. deal with the invalid user ..
</cfcatch>
</cftry>
You'll probably still want to keep your exception handling in place in case
someone forgets to call "doesUserExist" before "getUser"--but that's now an
error condition instead of normal program flow. The ultimate answer, of
course, is that CFML should support the concept if nulls. In an earlier
thread on this list I proposed the introduction of an "isNull" method, so
that you could do this (again, a modified version of Barney's original
example):
<cfset user = getUser() />
<cfif isNull( user )>
.. deal with invalid user ..
<cfelse>
.. do stuff ..
</cfif>
(Yes, I expect we'll introduce the concept of null objects and the "isNull"
function in a future version of BD).
Vince
> -----Original Message-----
> From: [EMAIL PROTECTED]
> [mailto:[EMAIL PROTECTED] On Behalf Of Andrew Scott
> Sent: Sunday, October 23, 2005 1:36 AM
> To: [email protected]
> Subject: RE: [CFCDev] CFC return types
>
> Is that the same way one should do it in Java as well?
>
> I sort of look at some code and see null or object being
> returned, and guess have that stuck in my head.
>
> Thanks Barney / Sean.
>
>
> Regards,
> Andrew Scott
>
> Quote of the Day:
> Felix's Wife: Dr. Connors. I want to thank you for fixing
> Felix's back.
> He can even help around the house again. Phil Connors: I'm
> sorry to hear that, Felix -----Original Message-----
> From: [EMAIL PROTECTED]
> [mailto:[EMAIL PROTECTED] On Behalf Of Barney Boisvert
> Sent: Sunday, 23 October 2005 3:24 PM
> To: [email protected]
> Subject: Re: [CFCDev] CFC return types
>
> Here's the skeleton of the function:
>
> <cffunction name="getUser" returntype="userCFC">
> ...
> <cfif doesUserExist>
> <cfreturn theUser />
> <cfelse>
> <cfthrow type="UserNotFoundException" />
> </cfif>
> </cffunction>
>
> And then calling it:
>
> <cftry>
> <cfset user = getUser() />
> .. do stuff ..
> <cfcatch type="UserNotFoundException">
> .. deal with the invalid user ..
> </cfcatch>
> </cftry>
>
> Just for references, with a "null" return value the function
> would return some kind of null instead of throwing the
> exception, and the calling code would look like this:
>
> <cfset user = getUser() />
> <cfif user is null>
> .. deal with invalid user ..
> <cfelse>
> .. do stuff ..
> </cfif>
>
> cheers,
> barneyb
>
> On 10/22/05, Andrew Scott <[EMAIL PROTECTED]> wrote:
> > Barney,
> >
> > If you don't mind I would like an example to clarify, in
> this example
> > I am checking to see if a user exists, it returns the user
> object if it does.
> So
> > when it returns back to the caller how do I handle it, if
> you can give
> > me
> an
> > example to help clarify what your saying that would be much
> appreciated.
> >
> >
> > Regards,
> > Andrew Scott
>
>
> --
> Barney Boisvert
> [EMAIL PROTECTED]
> 360.319.6145
> http://www.barneyb.com/
>
> Got Gmail? I have 100 invites.
>
----------------------------------------------------------
You are subscribed to cfcdev. To unsubscribe, send an email to
[email protected] with the words 'unsubscribe cfcdev' as the subject of the
email.
CFCDev is run by CFCZone (www.cfczone.org) and supported by CFXHosting
(www.cfxhosting.com).
CFCDev is supported by New Atlanta, makers of BlueDragon
http://www.newatlanta.com/products/bluedragon/index.cfm
An archive of the CFCDev list is available at
www.mail-archive.com/[email protected]