Named vs Unnamed Arguments in CF functions
ColdFusion gives developers a great deal of flexibility when creating functions. Not all arguments have to be known at runtime by declaring them with a
So for function foo...
<cfargument name="bar" /.
...
</cffunction>
called by
Would yield an argument struct of: bar = 10, 2 = 20, 3 = 30, 4 = 50
If you call foo like the following:
you would get an argument struct of: bar = 10, var1 = 20, var2 = 30, bar2 = 40
At first I thought this was a great concept. Less typing, not constrained to a particular implementation, etc. Then I got to thinking. This could really turn into a slippery slope.
Functions that don't have a clear set of arguments can easily end up not having a clear definition of purpose. (Not to mention difficult to debug!) They could start to take on more and more behavior and then lose any resemblance of reusability.
Functions that perform one specific behavior will have a known set of inputs. Those input values may vary, but the inputs themselve are always known.
There are some instances where unnamed arguments does make things a lot easier. There's even an example of that within CFEasyMock itself, in the MockFactory.replay() method (and reset() and verify()). It is simply easier to pass in a series of components rather than make an array out of them first. (Which would be the most prudent way to do it.) However, there is still the chance that someone passes in the wrong object, but that wouldn't be caught until runtime anyway.
It's a double edged sword. There are definite benefits, but it should be used sparingly with good reason, as opposed to a general coding practice.

There are no comments for this entry.
[Add Comment]