23

When we create interface methods, can't we do something like in java:

void interface_method(Integer,String, /* other parameter */);

Instead I noticed that we also need to give the parameter names lile:

void interface_method(Integer i, String s);

Also the interface implementor don't need to have the same parameter name as in interface method.

I found a similar question about c# over here . They mention one scenario of named parameter but I don't find any other relevent reason specifically in java.

Community
  • 1
  • 1
Priyank Doshi
  • 12,895
  • 18
  • 59
  • 82
  • 1
    +1: Really intersting question. C and C++ doesn't have this requirements..really no idea why Java does. – Heisenbug Jun 12 '12 at 13:57
  • You are going to get a lot of suggestions as to possible reasons here, but it's all guesswork. You're asking in the wrong place. You would need to ask Jim Gosling. – user207421 Jun 12 '12 at 22:37

3 Answers3

28

From a technical standpoint it wouldn't be necessary.

I've always taken it as a syntax normalization, and a documentation aid.

This way there's:

  1. No reason to differentiate between class and interface method syntax, and
  2. Default Javadoc documentation can be generated using the (descriptive!) parameter name.
Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • 1
    +1 Your point about Javadoc can be made even stronger: if parameter names were optional, then all the `@param` tags would need to be in the right order, and missing parameters would cause a lot more confusion. – Sergey Kalinichenko Dec 31 '12 at 13:36
15

Without parameter names it becomes very hard to distinguish between parameters of the same type.

double divide(double, double);

Convention says that the second parameter would be the divisor. But with named parameters, it is explicit and much clearer. In turn, the documentation can clearly use the names instead having to continually say "first parameter" or "second parameter" when trying to explain how the method should be use and what each parameter is meant to represent.

unholysampler
  • 17,141
  • 7
  • 47
  • 64
  • if its matter of properly distinguishing between parameters,isn't it better via javadoc rather than parameter name? – Priyank Doshi Jun 12 '12 at 14:17
  • 5
    @PriyankDoshi IMO not really; the *first* source of documentation should always be code, because code is the ultimate artifact. Javadocs are for supplying *additional* information not available in the signature. – Dave Newton Jun 12 '12 at 14:26
  • @PriyankDoshi: There is also no requirement that the javadoc lists the parameters in the same order as the method signature. A case could be made for listing the most important parameter first. Also, if you add a parameter to a method and place it at the beginning, all old documentation is invalidated because "first parameter" no longer refers to the same thing. With names, the intent of `thing1` stays the same even if it becomes the second parameter. – unholysampler Jun 12 '12 at 14:32
  • Right. I would say this sounds like the real answer. – Arturas M Dec 06 '16 at 18:43
5

My take on this:

  1. This is in language specification for parameters to have names;

  2. Interface being interface, names help to communicate the meaning;

  3. Interface is not really a forward declaration of implementation that will follow like in C/C++, so comparison is not entirely correct;

maksimov
  • 5,792
  • 1
  • 30
  • 38
  • 1.If its language specification, they can always make an exception for interface method..! 2. Won't javadoc be better option? – Priyank Doshi Jun 12 '12 at 14:18
  • 1
    @PriyankDoshi maybe they will, but I doubt. With parameter names you're forced to communicate, with javadoc it's optional. – maksimov Jun 12 '12 at 14:32
  • yeah it may be the case. Programmers always hate documentation.. :P parameter name may serve the purpose. – Priyank Doshi Jun 12 '12 at 14:41
  • 1
    Mind, hardcore "hackers" will not be deterred by this, they are still allowed to call their parameters p1, p2, p3 etc. – maksimov Jun 12 '12 at 14:45