I was trying to make an API. I just wanna hide all details from end programmer. I also want to provide them number of options to call a function.For example
I have 4 functions with same name but different signature (overloaded functions)
function1()
function1(arg1)
function1(arg1,arg2)
function1(arg1,arg2,arg3)
In above sequence 4th function ie function1(arg1,arg2,arg3) is having actual logic. rest functions are calling next function with some default values.
Now ,If a user calls 1st function in above sequence ie function1() then it is calling 2nd function ie function1(arg1) with some default values. and so on.
My question is, this sort of chaining is saving LOC(Line of Code) and increasing understanding. But whether it is good as per performance view?
Conditions with me
- I am using Java
- I am using JDK1.4. So variable number of arguments are supported.
Although you can suggest me performance in other languages as well, provided that you are not suggesting “variable number of arguments” feature.
Generally, calling a function will cause a memory jump, which regarding to performance is more costly than just running in sequence. When calling this just a couple of times, this is not a big issue, even with many levels of chaining. But if it is called within large loops, it could be noticeable performance issues.
Note:
To reduce the chaining to a minimum, you can call directly to the most detailed function (with all the parameters declared) from all the simplified functions.
This way you only have one extra step.
Note:
The compiler might give you some advantages by optimizing the calls for you. It might even be that the compiler will replace the initial call to
function1()with a call tofunction1(null,null,null)because this is allfunction1()does.