| |
Last
Updated:
|
Coldfusion: Structure As An Argument
By Raymond Camden
Expert Author Article Date: 2007-08-13 Joel asks: This is just a quick "best practices" question about CFC's, specifically regarding arguments.So just in case folks don't quite get what Joel is saying - he is talking about two options to pass data to a CFC. You can either use a set of arguments or have one argument that is a struct. Consider the following two examples: <cffunction name="test" access="public" returntype="string" output="false">
This is how most people set up their methods and here is the alternate way (struct as arguments) that Joel mentioned: <cffunction name="test2" access="public" returntype="string" output="false">
So as you can see - Joel is right. In the second example, you no longer have any real clues as to what the UDF is using, outside of one structure. You also lose the validation that helpfully double checked name and age. So in my simple example, I definitely think Joel is right - using "real" arguments does make sense. But as he points out - what about a method that takes many more attributes? It would be simpler, for example, to be able to pass the FORM scope as a structure and not use a lot of... form.name,form.age,form.foo,form.goo,form.rustillreading Luckily there are a few things we can to make this easier. First off - don't forget that you can use CFINVOKE and CFINVOKEARGUMENT syntax. While this results in more typing (potentially), it does make things readable. And you can use these tags without typing until the cows come home. Consider this example: <cfinvoke component="#application.foo#" method="doIt" returnVariable="result">
All I've done here is treat the form scope as a structure where each name reflects an argument in my CFC method. You can, if you want, add conditional logic to hide certain form fields, like submit button names, but if your CFC isn't using them, they will simply be ignored. And don't forget the use of attributeCollection: <cinvoke component="#application.foo#" method="doIt" returnVariable="result" attributeCollection="#form#"> This example is equivalent to the previous form - just more inline and less typing, but again if you did want to skip certain form fields you would not be able to. So all in all - I agree with Joel here and hopefully the examples above will show ways to mitigate cases where you have to deal with a large number of attributes. Comments About the Author: Raymond Camden, ray@camdenfamily.com http://ray.camdenfamily.com Raymond Camden is Vice President of Technology for roundpeg, Inc. A long time ColdFusion user, Raymond has worked on numerous ColdFusion books and is the creator of many of the most popular ColdFusion community web sites. He is an Adobe Community Expert, user group manager, and the proud father of three little bundles of joy. |
| DevWebProIN is an iEntry, Inc.® publication - 1998-2008 All Rights Reserved Privacy Policy and Legal |