Nando,

You've convinced me.
I've now rewritten it as:

<cffunction name="DateOutput" returntype="string" hint="I return the date
formatted for output" output="false">
<cfargument name="myDateTime" type="any" required="yes">
<cfset var result = "">
<cfif isDate(arguments.myDateTime)>
   <cfset result = DateFormat(myDateTime,"yyyymmdd")>
</cfif>
<cfreturn result>
</cffunction>

________________________________________
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf
Of Nando
Sent: Tuesday, May 23, 2006 8:24 PM
To: [email protected]
Subject: Re: [CFCDev] Easy question

... easy answer: use type="any"

Why? because CF is a dynamically typed language, and it seems from your
description that you've got an argument where dynamic typing comes into
play. So using "any" would be correct.

If you believed that there was only the possibility that a date could be
passed into this function during normal application flow and typed the
argument type="date", CF would throw a runtime error if somehow the thing
being passed into the function wasn't a date. 

On the other hand, if you typed the argument "string" or "any", but still
expected it to be a date that was passed in, CF would again throw a runtime
error if somehow the thing being passed into the function wasn't a date, but
this time it would be on your DateFormat() function. 

In a general sense, either ColdFusion throws a runtime error or it throws a
runtime error. So in what way does strong typing the argument help you in
this particular situation? It doesn't.

In the end, strong typing your arguments only seems to be useful in CF if
you want to force CF to throw an error before the function is run in an
exceptional situation. Maybe you wouldn't want a function to complete only
half it's job - it would leave your app in an unstable state. So better to
throw the error beforehand than in the middle somewhere.

Otherwise, in situations where you've got dynamic typing in play, I would
use type="any" and deal with normal application flow within the function. I
know we use variable names like "myDate", but we need to remember that in
many cases, that variable is dynamically typed, so it's actually
representing something like "myDateOrStringOrEmptyString"

Strongly typed arguments aren't as useful or helpful as they might seem in a
dynamically typed language that is compiled at runtime and doesn't support
null.


Phillip Senn wrote: 
<cfcomponent displayname="DateTime" output="false">
<cffunction name="DateOutput" returntype="string" hint="I return the date
formatted for output" output="false">
<cfargument name="myDateTime" type="date" required="yes">
<cfset var result = "">
<cfif DateDiff('d','1/1/1900',myDateTime) GT 0>
        <cfset result = DateFormat(myDateTime,"yyyymmdd")>
</cfif>
<cfreturn result>
</cffunction>



This works fine, except it doesn't accept an empty myDateTime.
So I suppose I need to change type="date" to type="string".

But then I'll need to verify that myDateTime is either "" or a valid date.




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

An archive of the CFCDev list is available at
www.mail-archive.com/[email protected]



  

-- 

Aria Media Sagl
CP 234
6934 Bioggio
Switzerland
www.aria-media.com




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

An archive of the CFCDev list is available at 
www.mail-archive.com/[email protected]


Reply via email to