6

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.

Jason Lewis
2,11313 silver badges18 bronze badges
asked Jan 14, 2012 at 0:47
3
  • 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 ? Commented Jan 14, 2012 at 0:55
  • @Matthieu: Yes, the same functionality. Commented Jan 14, 2012 at 0:57
  • 1
    Isn't this why we use different classes? Same method names, different implementation details? Commented Jan 14, 2012 at 1:17

6 Answers 6

10

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.

answered Jan 14, 2012 at 1:15
4

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.

answered Jan 14, 2012 at 1:14
1

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

answered Jan 14, 2012 at 0:55
2
  • 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. Commented Jan 14, 2012 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. Commented Jan 14, 2012 at 10:50
0

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.

answered Jan 14, 2012 at 1:15
0

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.

answered Jan 14, 2012 at 9:27
0

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.

answered Jan 14, 2012 at 10:05

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.