0

I've been told a cffunction should have no more than 3 arguments - Is there a better way to write this function? Every argument is a possible filter on the previous window and is used to filter the results down in the where clause.

<cffunction name="Example" access="remote" returntype="query">
    <cfargument name="keyword" type="string">
    <cfargument name="office" type="numeric">
    <cfargument name="builder" type="numeric">
    <cfargument name="sup" type="numeric">
    <cfargument name="mgr" type="numeric">
  • 4
    1.) A function can have as many arguments as it needs 2.) You can have default values if that is useful 3.) With structs and arrays you can effectively pass as many arguments as you need 4.) If you find yourself passing the same arguments over and over again, it might be that an object is more appropriate 5.) Do you have a citation for this 3 argument rule? – James A Mohler Apr 26 '17 at 04:19
  • Thanks James, I heard it at last year's cfsummit. I think it was in a session on clean code.. wasn't sure if that was the only reason or not. since all the arguments are url variables, I think i can pass a URL struct to the function. gonna try it. – kevin freese Apr 26 '17 at 04:36
  • 1
    @kevinfreese I've heard the same thing regarding clean code. But as I understand it, it has more to do with 1) a function taking that many arguments may be doing too much. Can it be refactored into multiple, smaller functions? and 2) It's easier to follow the flow of a function that has fewer arguments. – Shawn Apr 26 '17 at 21:12
  • @Shawn, Awesome, thanks for the reply! – kevin freese May 08 '17 at 18:28

1 Answers1

2

The code you have in your question is better than this suggestion:

<cfargument type="struct">

The reason is that a struct can contain anything and is not restricted to what the function really needs. In the case of the function in the question, it really needs one string and four numbers.

That being said, you can still pass a struct, such as a url to your function. It's a simple matter of this:

myQuery = Example(argumentCollection = url);
Dan Bracuk
  • 20,699
  • 4
  • 26
  • 43