Why are parameter names necessary in an interface definition? I am allowed to choose new parameter names during implementation

后端 未结 6 1822
粉色の甜心
粉色の甜心 2020-12-11 00:16

Not sure if this is a silly question, but I just noticed this:

public interface IActivityDao : IDao
{
    IList GetAllSinceSe         


        
相关标签:
6条回答
  • 2020-12-11 00:56

    Many languages like C# and VB support named and option arguments to methods. Without the argument names in the interface, using named and optional arguments would not be possible. Naming the arguments also helps the reader understand the interface's intention and function.

    0 讨论(0)
  • 2020-12-11 00:57

    History. This goes back to the very early days of .NET, back when COM ruled the world. Being able to interop with COM was very important back then, nobody ever throws away everything to adopt an entirely new style of programming.

    Which made COM interop strongly supported in .NET in general. As well as the need to have named arguments for interface methods, type libraries require them.

    The interesting corner case is forever the C++/CLI language. It adopted many C++ syntax rules, including the ability to omit parameter names in declarations. In other words, this is legal:

        public interface class IFoo
        {
            void bar(int, long, double);
        };
    

    The type library exporter generates this declaration:

        HRESULT bar(
                        [in] long p1, 
                        [in] long p2, 
                        [in] double p3);
    

    Very similar outcome if you implement the interface in a C# class, as autogenerated by IntelliSense:

    class FooImpl : cpptemp36.IFoo {
        public void foo(int __p1, int __p2, double __p3) {
            throw new NotImplementedException();
        }
    }
    

    This makes nobody happy.

    0 讨论(0)
  • 2020-12-11 00:57

    Let me ask you this, is there anywhere else in the .net framework that allows you to define a method signature without parameter names?

    At the end of the day all things are possible but most things are there for a reason, in this case I would imagine that this is a limit of the framework and compiler design, and does it really matter?

    You are after all defining a contract for use, one would expect them to be there really.

    0 讨论(0)
  • 2020-12-11 01:04

    Parameter names are required in an interface declaration for clarity of implementation and for reference. If someone were using your interface, the names of the method parameters are meant to be self documenting so the consumer of the interface understands what to pass to the method (eg when viewing the method description via IntelliSense)

    And yes, when you implement the interface you can name the parameters whatever you want.

    0 讨论(0)
  • 2020-12-11 01:13

    In the new world of code quality, the parameters name at the implementation level must be the same as interface level.

    In sonarqube has a rule like 'parameter names should match base declaration and other partial definitions' and calls this 'Noncompliant Code':

    interface IFoo
    {
      void Bar(int i);
    }
    
    class Foo : IFoo
    {
      void Bar(int z) // Noncompliant, parameter name should be i
      {
      }
    }
    

    The funny thing is it refers to this document which does not covering interface: https://wiki.sei.cmu.edu/confluence/display/c/DCL40-C.+Do+not+create+incompatible+declarations+of+the+same+function+or+object

    I personally like to have code like this:

    public interface IExtractor<TInput, TOutput>{
        TOutput Extract(TInput input);
    }
    
    public class CustomerExtractor : IExtractor<Order, Customer>
    {
        public Customer Extract(Order order)
        {
            // ...
        }
    }
    

    But the tool forces me to use the following declaring it as a critical issue:

        public Customer Extract(Order input)
        {
            // ...
        }
    

    input doesn't have the same meaning as order in this case.

    To get rid of the noise,

    • If no DI involved, Use static utility classes for this sort of classes (Map, Extract, Combine, ...)
    • If DI involved, trick the tool by adding a helper variable
        public Customer Extract(Order input)
        {
           var order = input;
    
           // use order from here on
        }
    
    0 讨论(0)
  • 2020-12-11 01:20

    I would imagine this is due to the named parameters feature in C#. Ie, you need to be able to specify parameters by name, not just in the default order:

    IActivityDao dao;
    dao.GetAllSinceSequence(count: 1, sequence: 2);
    

    Of course, the parameter names would be different if the object is cast as your instance.

    var concreteDao = (ActivityDao) dao;
    concreteDao.GetAllSinceSequence(maxRecords: 1, sequence: 2);
    
    0 讨论(0)
提交回复
热议问题