I have a question about cfargument
. If I am sending a parameter when calling a .cfc
, is there any benefit to defining it as argument first?
<cffunction name="someFunction" ....>
<cfargument name="myArg" required="no">
Or can I just use IsDefined() inside the function, without defining an argument?
<cffunction name="someFunction" ....>
...
<cfif isDefined("arguments.myArg")>
do something
</cfif>
I tried them and know that they both work. However, what is a difference between defining the argument and using isDefined()? Can that affect efficiency?
It's not about efficiency, it's about documentation. Here's an exceprt from my company's coding standards document on cfargument
.
When using the CFARGUMENT tag in a ColdFusion function, the following attributes are required:
ColdFusion does not require that you use cfargument
tags but they provide validation (type safety) and act as additional documentation, therefore always provide a cfargument
tag for each named argument your function expects.
Rules:
type
attribute in your cfargument tags. Try to avoid using type="any"
.required="true"
but do not specify a default
attribute value.required="false"
and specify a default
attribute value.default
, but instead use structKeyExists(arguments,"ARGNAME")
in the function body (remember that when you specify default
, you cannot tell the difference between the caller omitting that argument and the caller providing that same default value as an argument).foo
, reference arguments.foo
.)HTH