3

I have a ColdFusion parent component with a function that looks something like this:

public numeric function myFunction(var1, var2, var3, var4, var5){
    ... function code ...
} 

Then I have a child component that extends this component and has the following function:

public numeric function myFunction(var1, var2, var3){
    super.myFunction(var1, var2, var3, variables.var4, variables.var5);
} 

Where variables.var4 and variables.var5 are properties of the child.

The problem is that var1, var2, and var3 are optional. If any of them are not passed in, I get an error on the super.myFunction call:

Variable VAR2 is undefined

How can I invoke the parent method with whichever parameters were actually passed in + the 2 child properties? (Without spaghetti conditional coding)

froadie
  • 79,995
  • 75
  • 166
  • 235
  • Do you just need to scope the arguments in your function call? Like `super.myFunction(arguments.var1, arguments.var2, arguments.var3, variables.var4, variables.var5);` – Miguel-F Oct 20 '15 at 20:32
  • @Miguel-F - No, I actually did scope them in my actual method and lost that in simplifying it for the SO question. The problem is that if one of the arguments isn't actually passed in, then it's not defined and can't be passed on to the `super` function. And I can't just pass on theentire `arguments` struct because I need to also pass on 2 additional arguments from the properties. – froadie Oct 20 '15 at 20:35

2 Answers2

4
public numeric function myFunction(var1, var2, var3){
    return super.myFunction(var4=variables.var4, var5=variables.var5, 
                            argumentCollection=arguments);
} 
Henry
  • 32,689
  • 19
  • 120
  • 221
  • Thank you! That is just what I was looking for. I just couldn't figure out the syntax and if it was allowed to pass some arguments explicitly and some in a collection – froadie Oct 20 '15 at 21:07
  • I am surprised you are allowed to mix individual argument names and argumentCollection. Though neither is ideal, somehow that seems less clean than simply adding the values to the arguments structure. – Leigh Oct 20 '15 at 21:34
  • @Leigh so what is ideal? – froadie Oct 21 '15 at 09:03
  • @froadie - There is no ideal answer in this case, not unless the arguments can have default values (I assume not). Your first option seemed cleaner. Unfortunately, there is not a lot of options in this case, but passing individual arguments *and* argumentCollection seems more hackish than simply modifying the argument structure IMHO. – Leigh Oct 21 '15 at 13:21
  • @Leigh - tampering with the arguments structure seems messier to me. At least the mixed arguments, though strange that it's allowed, more clearly show what the intent is (i.e. I'd like to pass along any arguments passed in, plus these 2 arguments). But seems this is subjective :) – froadie Oct 21 '15 at 16:17
  • @froadie - Personally, the mix and match approach seems more ambiguous to me ;-) but .. that said, it is not entirely subjective. CF does not *completely* enforce function signatures, so the results of the above may differ depending which function is accessed and how. For example, if you access the parent externally, you may get five (5) arguments or seven (7), depending on how you pass the values. That only happens when mixing argumentCollection and individual arguments. – Leigh Oct 21 '15 at 20:36
  • @Leigh - re your comment "not unless the arguments can have default values (I assume not)" - they technically can have default values, but as those values are defaulted in the parent method, I thought it would be repetitive to default it in both places. Would that be better design? – froadie Oct 24 '15 at 19:57
1

The following code works:

public numeric function myFunction(var1, var2, var3){
    arguments.var4 = variables.var4;
    arguments.var5 = variables.var5;
    return super.myFunction(argumentCollection=arguments);
} 

However, this feels somewhat hacky. Is this an acceptable solution? Are there any better solutions?

froadie
  • 79,995
  • 75
  • 166
  • 235
  • it's missing a return statement – Henry Oct 26 '15 at 17:33
  • With that fixed, I think this is actually okay. Although this is not a one-liner, the intent is actually clearer. However, it is not usual to have 5 arguments and it would be quite hard to test such method. – Henry Oct 26 '15 at 18:23