Reputation: 7413
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
Although you can suggest me performance in other languages as well, provided that you are not suggesting "variable number of arguments" feature.
Upvotes: 0
Views: 126
Reputation: 22442
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.
function1(){
function1(null,null,null);
}
function1(arg1){
function1(arg1,null,null);
}
function1(arg1,arg2){
function1(arg1,arg2,null);
}
function1(arg1,arg2,arg3){
// Actual logic here...
}
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 to function1(null,null,null)
because this is all function1()
does.
Upvotes: 1
Reputation: 54574
Probably this has worse performance, but adds more flexibility, and avoids that users mix up arguments. You can have something similar to default arguments like in C++ if you do something like:
public class function1 {
private String arg1 = "ding";
private String arg2 = "dong";
private String arg3 = "dang";
public function1 arg1(String arg1) {
this.arg1 = arg1;
return this;
}
public function1 arg2(String arg2) {
this.arg2 = arg2;
return this;
}
public function1 arg3(String arg3) {
this.arg3 = arg3;
return this;
}
public String eval() {
return arg1 + arg2 + arg3;
}
}
System.out.println(new function1().arg2("deng").eval());
Probably this is overkill here, but as more arguments you have, as nicer this works out. Note that if some of the arguments are mandatory, you can put them in the constructor.
Upvotes: 0
Reputation: 16397
Used to have something like that in C where the extra arguments were always default values when they are not provided.
To do that, I used #define... maybe not very pretty, but it works and will not have any performance impact in the final binary (no additional stack space, or time jumping around the program counter).
Upvotes: 0
Reputation: 18344
[Note: I assume you are talking about a mordern programming language when you ask your question (since it is not explicitly tagged with a language). If this is not true, please discard this answer.]
First, the purpose of such chaining is not, and should not be reduction LOC. Lines of code are the least important thing in terms of performance, and to some extent even for readability, especially in the days of mordern IDEs. (For the record: "to some extent". I dont say a 2500 line method is good)
The purpose is to avoid duplication of logic, and convinience to a programmer, which otherwise would be a maintainence nightmare. (If you are going to have the logic in all of the methods) and some difficulty for the end programmer (if you are going to have only one implementation)
In mordern programming languages, the overhead caused by a extra two method class is nigligible. Such optimizations (reducing number of method calls) mostly lead to nowhere.
The performance is dependent on what the method is doing and one should concentrate on that if there are any performance issues.
I don't see any problems, performance included, and actually see benefits in the sort of method chaining you had mentioned in your question.
Upvotes: 0
Reputation: 11814
In languages like C/C++ the compiler can handle such things in a way that there might be no performance penalty at all. In languages like Matlab, there is a notable time for each function call.
If such a time matters at all is strongly dependent on how often your methods are called. If they do something that requires lots of calculation or if they are used for initialization, they are probably called not too often. In these cases I woudn't worry about this. If they are called often, I would advise to measure before making decisions.
Upvotes: 1