By Peter Bell

The Benefits of Named Arguments When Calling CFC Methods

There are three main ways to pass arguments into a method: unnamed arguments (where the arguments are matched up based on their position), argumentsCollection (where you pass a struct into the method containing your arguments in the form method(argumentsCollection=MyStruct)), and named arguments where you explicitly name all of the arguments you are passing in and can therefore enter them in any order (e.g. method(Arg1=Value1, Arg2=Value2)).

On the whole I'm a pretty big fan of minimizing the typing within an application, but I'm starting to move quite a few of my method calls to using named arguments because of the benefits they can provide over unnamed (positional) arguments . . .

One of the first benefits of named arguments is that it makes your code more readable. For methods with lots of arguments, sometimes it is nice to see the name of all of the arguments right there in the method call rather than having to jump back and forth between the caller and the called classes to see the arguments being passed.

Secondly, named arguments (or using an argumentsCollection) is the only way to have multiple optional arguments and to have some method calls including one optional argument and other method calls including another.

Finally, if you're using delegates, named arguments has the minor benefits that you don't *have* to put any of the cfargument statements in the delegate methods as I found out earlier today - keeping your code just a little DRYer (but not for free - see cautions in the posting).

Of course, you get all of these benefits if you use an argumentCollection, and for simple methods where there are a small number of arguments most of which are required, this isn't a big deal - I can't imagine using named arguments for getters and setters, for instance. But for larger, more complex methods, I'm finding the move to named methods to be quite useful.

What is your take? How do you decide between named arguments, an argumentCollection and simple unnamed/positional arguments for a given method?

Comments
Hmmmm, I never thought about it much, but after reading your post I'd say I'll probably use named arguments more often, if only for readability, because it's so true that for somebody who's not as familiar with the code it's much easier to have the argument names in the call. Unnamed arguments would be OK if you have only 1-2 arguments and they are sort of obvious. argumentCollection would probably be the one I'd use least, but I get the feeling that when you use it it's because it's really the only way to go.

Man, it took me a while to read through all of today's posts! Sorry if I don't reply on everything, there's a bunch of stuff I need to digest to fully comprehend. This blog is quickly turning into my favorite, I feel like I learn tons just by reading it. Thanks a lot of all the posts.
# Posted By Thomas Messier | 5/13/07 9:59 PM
tend to use named arguments, for readability, and also because you never know what order your function is expecting its arguments in. especially true if you're not the developer that created/maintains that function.
# Posted By duncan | 5/14/07 4:22 AM
I do basically the same as you, but when I can think of a good name that says its arguments in the order it expects them, I'll use that instead (this would generally be 2-argument methods). For single argument methods, I generally don't use it, but for 3+ I almost always do.

I can see that even though I got in at 5:30, it was useless, since you seem to have posted enough to keep me busy until noon =)... good to see you posting quality material at that furious pace again. Bad for my work efficiency.
# Posted By Sam | 5/14/07 7:03 AM
Same as Sam I use named arguments whenever they couldn't be obviously figured out. With most simple one or sometimes two argument functions seem obvious enough, and they aren't going to change then no good reason for it.

On a somewhat similar note, I do like the idea of using coldspring with named constructor injection and having it just set everything passed in though by looping through the arguments. Basically all my init methods just super() up to the base one which sets whatever arguments are passed in. Saves some time assuming you don't have circular dependencies.
# Posted By Adam Fortuna | 5/14/07 9:22 AM
@Everyone, Sounds like a consensus is emerging :->

@Adam, Of course, if you ONLY have super.init() in your inits, you can just remove the init() methods and the parent one will be called automatically . . .
# Posted By Peter Bell | 5/14/07 10:08 AM
Good article. If you're interested, I posted another case in which using named arguments is helpful.

http://www.cfinternals.org/blog/2007/05/named_argu...
# Posted By Michael Long | 5/14/07 10:17 AM
@Michael, Nice - thanks!
# Posted By Peter Bell | 5/14/07 10:25 AM
BlogCFC was created by Raymond Camden. This blog is running version 5.005.