Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you do optional parameters in a function thru cfscript?

I am finally getting around to writing stuff in cfscript, and so I am starting with writing some needed formatting functions. Here is an example:

    Function FormatBoolean(MyBool, Format) { 

    Switch(Format){
        Case "YES/NO":{
            If (MyBool eq 1)
                Return "YES";
            Else
                Return "NO";
            Break;
        }

        Default:{
            If (MyBool eq 1)
                Return "Yes";
            Else
                Return "";
            Break;
        }
    }
}

What I would like to do is make Format an optional argument. If you don't include the argument, the function will currently still run, but it won't find format, and it seems that cfparam did not get translated to cfscript.

Will I just have to check if Format is defined and give it a value? Or is there a nicer way of doing this?

Thanks

like image 427
Limey Avatar asked Oct 17 '11 19:10

Limey


People also ask

How do you add an optional parameter to a function?

To declare optional function parameters in JavaScript, there are two approaches: Using the Logical OR operator ('||'): In this approach, the optional parameter is Logically ORed with the default value within the body of the function. Note: The optional parameters should always come at the end on the parameter list.

Does Go support optional parameters in functions?

Go does not have optional parameters nor does it support method overloading: Method dispatch is simplified if it doesn't need to do type matching as well.

Can parameters be optional?

The definition of a method, constructor, indexer, or delegate can specify its parameters are required or optional. Any call must provide arguments for all required parameters, but can omit arguments for optional parameters. Each optional parameter has a default value as part of its definition.


2 Answers

Personally I prefer to set defaults to this kind of arguments. Also I've refactored function a bit... But not tested :)

function FormatBoolean(required any MyBool, string Format = "") { 

    switch(arguments.Format) {
        case "YES/NO":
            return YesNoFormat(arguments.MyBool EQ 1);
        default:
            return (arguments.MyBool eq 1) ? "Yes" : "";
    }

}

Please note that (arguments.MyBool EQ 1) may be replaced with (arguments.MyBool), so it covers all boolean values. You may be interested to make it more reliable, something like this (isValid("boolean", arguments.MyBool) AND arguments.MyBool) -- this should allow to check any value at all.

like image 158
Sergey Galashyn Avatar answered Sep 19 '22 06:09

Sergey Galashyn


All variables passed into a function are available to access programmatically via the ARGUMENTS scope. You can refer to it as if it were an array (because it is), as well as standard struct key access (which I've done for you below for the MyBool parameter):

<cfscript>
    Function FormatBoolean(MyBool) { 

    var theFormat = '';

    if (ArrayLen(ARGUMENTS) GT 1)
        theFormat = ARGUMENTS[2];

    Switch(theFormat){
        Case "YES/NO":{
            If (ARGUMENTS.MyBool eq 1)
                Return "YES";
            Else
                Return "NO";
            Break;
        }

        Default:{
            If (ARGUMENTS.MyBool eq 1)
                Return "Yes";
            Else
                Return "";
            Break;
        }
    }
}
</cfscript>

Add your preferred additional levels of data validation as necessary.

like image 26
Shawn Holmes Avatar answered Sep 18 '22 06:09

Shawn Holmes