Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

In many cases, I want to write methods that have the same functionality for different types of inputs. This is easily accomplished by method overloading if the parameter types are different.

But what's the best (most robust) way to go about resolving the case when the parameter types are the same (i.e. two different representations of the data with the same type)?

An example of this would be an integer matrix which can naturally be stored as an int[][]. But what if you want to write a method which accepts the transpose of the matrix as well? The transpose is also an int[][] but a clearly different representation altogether.

I can see a couple ways of doing this:

  • Giving the methods different names
  • Adding a flag to the method
  • Wrapping each representations in different classes

I think the third method is the most clear way of doing this. Unfortunately I'm working on some high performance libraries where that's not a feasible solution.

share|improve this question
    
Considering the beginning of your question, in your matrix example, the function accepting the matrix, and the function accepting the transpose have the same functionnality or not ? –  Matthieu Jan 14 '12 at 0:55
    
@Matthieu: Yes, the same functionality. –  tskuzzy Jan 14 '12 at 0:57
1  
Isn't this why we use different classes? Same method names, different implementation details? –  S.Lott Jan 14 '12 at 1:17
add comment

6 Answers

I vote for "Giving the methods different names" option especially if performance matters.

Don't use method1(int[][]) and method2(int[][]).
Use method(int[][]) and method_transposed(int[][]) for example.

Method name should always help the reader to understand what it does.

share|improve this answer
add comment

You need to be careful designing APIs that have methods overloaded on parameter type: the methods need to do exactly the same things, otherwise the users will be confused.

Two methods that take a matrix, one taking a "straight" one and the other taking a transposed one, are definitely not doing the same thing, so overloads on different types is an unlikely fit.

Choosing a "flag" vs. "different name" is easy: if your method looks like this

int[][] mul(int[][] a, int[][] b, bool flag) {
    if (flag) {
        // Do one thing
    } else {
        // Do another thing
    }
}

then you should use separate methods. If your method with a flag shares great deal of code, then you should use a method with a flag.

share|improve this answer
add comment

Use different method names if the parameter types are the same. Simple enough, no?

share|improve this answer
    
Doesn't that kind of break the naming scheme though? I'd have method1(int), method1(long), method1(float), method1ForSpecialFloat(float). That's rather inelegant especially for the enduser of a supposedly polished product. –  tskuzzy Jan 14 '12 at 1:00
1  
@tskuzzy: no, that is not inelegant, it's a good practice. method1_transposed will do. It's the most natural thing. –  Falcon Jan 14 '12 at 10:50
add comment

I don't know what language you are in, but possibly a structure or tuple would group together the int[][] plus a flag into one "thingy" with acceptable performance, and it results in a cleaner interface. If not, I think the most logically similar thing to a class is breaking out what would otherwise be the class type into another parameter (maybe an enumeration or something rather than a flag to keep your options more open). And you would be consciously choosing speed over a cleaner interface. You might check the speed to make sure it matters, if the matrices are 1,000 by 1,000 it might not matter. Though I imagine this has already occurred to you.

share|improve this answer
add comment

Don't use the same data type for different representations. If you have a matrix and a "transposed-representation-of-a-matrix" version of your method, it might be a good idea to create two data types for these representations. This way, the compiler will also yell at you if you accidentally use the wrong one somewhere.

share|improve this answer
add comment

This is the perfect use-case for interfaces; Use a single base class to encapsulate the business logic, but implement different interfaces in subclasses that take varying parameters. This perfectly illustrates why you should code to an interface, rather than an implementation. So yes, you would use different classes, but you would do

public class MatrixOne implements ThisMatrix {
  //code
}

public class MatrixTwo implements TransposeMatrix {
  //code
}

Obviously, this is a simplification. There are other options, but if you can pass a message indicating which version of the matrix is effective, you can tell the receiver how to process it, as well.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.