23

In C# we have to name the parameters of a method of an interface.

I understand that even if we didn't have to, doing so would help a reader understand the meaning, however in some cases it's not really needed:

interface IRenderable
{
    void Render(GameTime);
}

I would say the above is as readable and meaningful as the below:

interface IRenderable
{
    void Render(GameTime gameTime);
}

Is there some technical reason why names for parameters of methods on an interface are required?


It's worth noting that the implementation of the interface method can use different names to those in the interface's method.

George Duckett
  • 31,770
  • 9
  • 95
  • 162

5 Answers5

21

One possible reason could be the use of optional parameters.

If we were using an interface, it would be impossible to specify named parameter values. An example:

interface ITest
{
    void Output(string message, int times = 1, int lineBreaks = 1);
}

class Test : ITest
{

    public void Output(string message, int numTimes, int numLineBreaks)
    {
        for (int i = 0; i < numTimes; ++i)
        {
            Console.Write(message);
            for (int lb = 0; lb < numLineBreaks; ++lb )
                Console.WriteLine();
        }

    }
}

class Program
{
    static void Main(string[] args)
    {
        ITest testInterface = new Test();
        testInterface.Output("ABC", lineBreaks : 3);
    }
}

In this implementation, when using the interface, there are default parameters on times and lineBreaks, so if accessing through the interface, it is possible to use defaults, without the named parameters, we would be unable to skip the times parameter and specify just the lineBreaks parameter.

Just an FYI, depending upon whether you are accessing the Output method through the interface or through the class determines whether default parameters are available, and what their value is.

Lukazoid
  • 19,016
  • 3
  • 62
  • 85
  • Interfaces were there before optional and named parameters. :) – CodeCaster Nov 18 '15 at 21:18
  • @CodeCaster Good shout, most likely not the original reason however it is definitely a reason now :) I guess the possibility of future features such as this were the reason they named them. – Lukazoid Nov 18 '15 at 21:32
  • @Lukazoid, well done. That's the most logical answer whichever way you look at it. – Jay Shanker Jul 22 '17 at 14:48
13

I don't see any reason that would make this a technical requirement. But I can think of one particularly good reason:

As you mention, the parameter names are not needed when implementing the interface, and can be easily overridden.
However, when using the interface, imagine the difficulty if no parameters had meaningful names! No intellisense, no hints, nothing but a type? Yuck.
This has got to be the biggest reason that a name is always required.

Scott Rippey
  • 15,614
  • 5
  • 70
  • 85
  • plus it makes it consistent with method declarations in classes. – Thilo Dec 23 '11 at 09:32
  • 2
    "imagine the difficulty if no parameters had meaningful names!" - just write some Java in Eclipse, most parameter names are shown as "arg0" etc... – Adam Dec 23 '11 at 09:35
  • 1
    @codesparkle That sounds as much fun as reading through a minified JavaScript file! – Scott Rippey Dec 23 '11 at 09:49
  • 1
    @codesparkle: That is a limitation in the Java bytecode format (parameter names are not part of the class file). Is not really a problem in Eclipse, though, because it can get them from Javadoc or source jars if present, and then they don't just show up as `arg0`. – Thilo Dec 23 '11 at 11:41
  • @thilo yeah, that's the same way Javascript documentation works in visual studio. – Scott Rippey Dec 23 '11 at 19:39
  • The person who wrote the interface knows what it does, and other people are unlikely to see the source. Describe the methods function in documentation. – Aaron J Lang Jan 03 '12 at 02:20
  • @ScottRippey exactly "yuck", that's the difference between Java and C# readability...and for the number of coder base to rise or fall... – bonCodigo May 25 '14 at 14:36
4

Naming interface method parametres helps with self documentation:

For example ...

interface IRenderable
{
    void Render(TimeSpan gameTime);
}

... says more than:

interface IRenderable
{
    void Render(TimeSpan);
}
Michał Kuliński
  • 1,928
  • 3
  • 27
  • 51
  • +1 for self-documentation, but also (if you're so inclined) it also helps with the `///` documentation if you use this for intellisense. – Ben Jul 16 '18 at 08:58
  • One should be carefull about `///` documentation, because it can turn wrong: https://stackoverflow.com/questions/13191542/how-to-remove-all-c-sharp-methods-properties-fields-summary-comments-starting – Michał Kuliński Jul 16 '18 at 11:10
  • 1
    I agree that the `///` documentation is only as good as the person that writes it and is not always necessary. However, `///` documentation can sometimes be very useful if you are creating interfaces as it clarifies the interface contract made between users and implementers of the concrete classes. – Ben Jul 16 '18 at 15:15
1

I can't think of any valid technical reason that interfaces have to have names defined.

I can easily see a situation where the names are auto-implemented like the backing members for auto-implemented properties are today.

However, I think that there are probably 3 main reasons why they have been needed:

1) It was probably substantially easier to implement interface validation in the compiler using the same rules as actual methods. Since it was only relatively recently that auto-implemented properties were introduced, I suspect this is a non-trivial compiler change.

2) For those languages that support auto-creation of the interface members in the implementing class (i.e. VB), it is probably much easier to create the interface implementation using pre-defined names than trying to create names on the fly.

3) Since an interface can be exposed outside of the defining application, names remove the ambiguity associated with an ill-defined interface.

For example, attempting to implement an interface method of:

void Foo(string, string, int)

Would most likely lead to substantially more confusion than your self-documenting example. However, this is really more of an interface usability issue than a technical one, although one could argue that if the interface is unusable, there is an underlying technical issue.

competent_tech
  • 44,465
  • 11
  • 90
  • 113
0

Ok, this possibility almost seems too frivolous, but -- maybe so when you let Visual Studio implement the interface and stub in properties and methods, it knows what to name the parameters?

On the other hand, VS has no problem generically naming controls...

Patches
  • 1,115
  • 1
  • 10
  • 13