Skip to main content
Participant
July 17, 2014
Question

Disable cffunction accepting undeclared arguments?

  • July 17, 2014
  • 1 reply
  • 619 views

I have a lot of function calls with long lists of arguments and sometimes they can get mismatched (invoke with invokeargument status, function argument status_code). Coldfusion allows this to happen and makes the undeclared argument available in the arguments scope. Is there any way to disable this behavior so it only allows declared arguments and throws and error when it gets passed an argument that it doesn't expect?

This topic has been closed for replies.

1 reply

BKBK
Community Expert
Community Expert
July 19, 2014

First of all, if a function has a long list of arguments, say, more than 5 or 6 arguments, then it is complex. It is likely to be doing too many things. As a rule, a function should have just one responsibility. You should split such complex functions up into 2 or more separate functions.

To avoid ambiguity, use the name of the arguments when you call the function. For example, suppose that myFunc has arguments arg1, arg2,..., arg6, all of which are optional. Then you could call it as follows: myFunc(arg3="Journal",arg5="2014").

Participant
July 21, 2014

The nature of this project requires the long lists of arguments, and I do use names with optional arguments. The issue I'm having is when I invoke a function expecting "status_code" as an argument with an argument of "status" instead. The function executes with status_code at its default value and status just sitting in the arguments scope unused. This is never intentional, but happens on occasion. I would like coldfusion to throw an error any time there is something in the arguments scope that is not an expected argument.

BKBK
Community Expert
Community Expert
July 21, 2014

Please bear with me: I still don't get it. What do you mean by, "when I invoke a function expecting "status_code" as an argument with an argument of "status" instead. The function executes with status_code at its default value and status just sitting in the arguments scope unused" ?

Could you just paste a little code here to illustrate? Thanks.