views:

72

answers:

5

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

  1. I am using Java
  2. 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.

+1  A: 

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.

Philipp
A: 

[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.

Nivas
A: 

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).

Matthieu
A: 

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.

Landei
sorry, i am not able to link your answer with my question
articlestack
This is another way to organize "functions" with default arguments, but probably not very performant. The advantage is that you can provide the "arguments" in any order you want. Of course the syntax isn't as nice as for a real function.
Landei
+1  A: 
awe