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
cfargumenttags but they provide validation (type safety) and act as additional documentation, therefore always provide acfargumenttag for each named argument your function expects.Rules:
typeattribute in your cfargument tags. Try to avoid usingtype="any".required="true"but do not specify adefaultattribute value.required="false"and specify adefaultattribute value.default, but instead usestructKeyExists(arguments,"ARGNAME")in the function body (remember that when you specifydefault, you cannot tell the difference between the caller omitting that argument and the caller providing that same default value as an argument).foo, referencearguments.foo.)HTH