Named parameters enable you to associate argument with the parameter’s name rather than with the parameter’s position in the parameter list. They can be used in functions, constructors.
In earlier versions of ColdFusion, named parameters were allowed in user-defined functions and in component constructors. We are introducing named parameters for built-in functions with ColdFusion 2018.
Before ColdFusion (2018 release), the order of parameters in a BIFs was necessarily fixed, since it was the only way that CF could identify which value is intended to be used for which purpose. But now with named parameters, position or order of parameters does not matter, the arguments are evaluated in the order in which they appear in the list.
Optional parameters enable you to omit arguments for some parameters. Named parameters in CF are used in conjunction with optional parameters. We take care of providing default values to these optional parameters when they are omitted from the parameter list.
<cfscript> mystruct={"key2":"value2","key1":"value1","key4":"value4"} StructSort(sortType="text", struct=mystruct) </cfscript>
The above example uses both named parameters and optional parameters. StructSort accepts 4 parameters struct, sortType, sortOrder and localeSensitive. While specifying the parameter list in the above example we have omitted the two optional parameters: sortOrder & localeSensitive. Both these parameters are initialized with default values which is “asc” & “false”.
I am sure you might be wondering why should I use named parameters. Well, named parameters definitely free you from remembering the order of function arguments. In CF, Most of the BIFs function have more than 4 to 5 parameters. In such functions, it’s always tedious to remember the order of function arguments.
For example:
<cfscript> longString = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890" encryptedString = encrypt(longString, AES_KEY, AES, BASE64) decryptedString = decrypt(encryptedString, AES_KEY, AES, BASE64)> </cfscript>
When you look at the above code, it’s always difficult to remember what comes first, whether it’s the encryption/decryption algorithm or the key or the binary encoding. In such cases, I would use Named Parameters.
<cfscript> longString = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890" encryptedString = encrypt(string=longString, algorithm=AES, key= AES_KEY, encoding=BASE64) decryptedString = decrypt(string=encryptedString, algorithm=AES, key= AES_KEY, encoding=BASE64)> </cfscript>
Named parameters also improve code readability. For example, Find() searches for a substring in a string.
find(str1,str2,1)
Looking at the above code, its impossible to guess which one is the string and which is substring.
find(substring="str1",string="str2",start=1)
This definitely reduces confusion for anyone reading the code.
Below are few more examples of named parameters:
Example 1:
<cfscript> myarray= [4,2,1,3] writeoutput(ArrayFind(array=myarray, value="4")) writeoutput(Arrayfind(callback=function(index,item){ if(index == item) return true else return false }, array=myarray)) </cfscript>
ArrayFind can either accept a value to find in an array or a callback that has the searching logic. You can use both value or callback with named parameters as in the above example.
Example 2:
<cfscript> finalVar = evaluate(expression = ["setvariable( 'first',1 )", "setvariable( 'second', 2 )", "setvariable( 'third', 3 )", "setvariable( 'fourth', 4 )" ]); finalVarOut = evaluate(expression ="setvariable( 'first',1 )"); writeOutput(finalVarOut); writeOutput(finalvar); writeOutput(first); writeOutput(second); writeOutput(third); writeOutput(fourth); </cfscript>
Evaluate function when used with named parameters, can accept a single expression and an array of expressions. This can be seen in the above example. precisionEvaluate() has been enhanced in a similar fashion..
In this release, we support named parameters for the headless versions of Built-In-Functions. We intend to support named parameters in member functions. Also, argument collection for BIFs would also be supported in near future.
The document that has parameter names for all the Built-In-Functions is uploaded at, http://cfdownload.adobe.com/pub/adobe/coldfusion/2018/publicBeta/NamedParametersColdFusion2018.pdf.
Have a look at the parameter names, we have tried to keep all the argument names to be intuitive and easy to remember.
You must be logged in to post a comment.