4 Replies Latest reply on Sep 12, 2013 11:05 AM by wispreaver

    Function value arguements vs struct arguement

    wispreaver

      In a function is it better to define 4 arguements of type string, or one single arguement of type struct that contains these values?

        • 1. Re: Function value arguements vs struct arguement
          duncancumming Level 3

          That depends really on your requirements.  There's no de facto standard in ColdFusion AFAIK.  Does the data make more sense as a single structure, or are they 4 independent strings that should be on their own?  e.g. if it was something like a customer record, it might make sense as a structure:

          doStuff(

            customer = {

              firstname: "Joe",

              lastname: "Bloggs",

              title: "Sir",

              greeting: "Hi "

            }

          );

           

          Whereas you might have 4 values that aren't really all about one thing, e.g.

          doStuff(

            pageTitle= "Hello World",

            cssFile= "/path/to/file.css",

            customerEmail= "info@example.com",

            today= dateFormat(now())

          );

           

          Which would also be fine (apart from the wisdom of having a function requiring such disparate arguments)

          • 2. Re: Function value arguements vs struct arguement
            wispreaver Level 1

            I am talking from a speed and performance. Because I am going to be doing this a lot of times for a lot of users.

            • 3. Re: Function value arguements vs struct arguement
              Adam Cameron. Level 5

              It will not make a performance difference. Aim for what is semantically the best code.

               

              As Duncan said... are the strings in any way related? SHould they be perhaps properties of an object, not individual values, or even a struct.  EG: if two of them are firstName and lastName, they perhaps would be better as a single object name.

               

              If you really do need to pass four separate arguments to a function... there's a a chance your function is doing too much. What's an example of the function being called? (ie: show us the code).

               

              --

              Adam

              • 4. Re: Function value arguements vs struct arguement
                wispreaver Level 1

                THis example shows isNumeric function. This is needed to have a user maintained system of validation, so a bunch of these things exist and are set up the same way.

                 

                 

                --- BEGIN CODE ---

                 

                <cffunction name="isNumeric" output ="false" access="public" returntype="struct">

                    <cfargument name="valuesStructure" required="yes">

                        <cfset var result = {}>

                       

                        <cfif valuesStructure.objectValue EQ "">

                            <cfset result.passOrFail = "pass" />

                            <cfset result.errorMessage = "" />

                        <cfelse>   

                       

                              <cfif  REFind("^[0-9]+",valuesStructure.objectvalue) GT 0>

                                   <cfset result.passOrFail = "fail" />

                                <cfset result.errorMessage = valueStructure.objectName & " can contain only numbers."  />           

                            <cfelse>

                                  <cfset result.passOrFail = "pass" />

                                <cfset result.errorMessage = "" />

                            </cfif>

                           

                            <cfreturn result>

                    </cfif>   

                </cffunction>

                 

                 

                 

                 

                ---- END CODE --

                 

                 

                This is using a struct, so some fucntions use more struct values while others may not. So, to use the values I would have to m ake the one argument into many. Plus what calls these uses cfinvokeargument, so, if 4 arguments, then it does the invoke 4 times for each argument.