• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Function value arguements vs struct arguement

Guest
Sep 12, 2013 Sep 12, 2013

Copy link to clipboard

Copied

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

Views

849

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Sep 12, 2013 Sep 12, 2013

Copy link to clipboard

Copied

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)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Sep 12, 2013 Sep 12, 2013

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 12, 2013 Sep 12, 2013

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Sep 12, 2013 Sep 12, 2013

Copy link to clipboard

Copied

LATEST

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation