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

后端 未结 6 1832
粉色の甜心
粉色の甜心 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 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{
        TOutput Extract(TInput input);
    }
    
    public class CustomerExtractor : IExtractor
    {
        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
        }
    

提交回复
热议问题