Method Overloading. Can you overuse it?

前端 未结 16 1422
有刺的猬
有刺的猬 2020-12-02 13:38

What\'s better practice when defining several methods that return the same shape of data with different filters? Explicit method names or overloaded methods?

For exa

16条回答
  •  猫巷女王i
    2020-12-02 13:52

    I have seen overloading overused when you have only subtle differences in the arguments to the method. For example:

    public List GetProduct(int productId) { // return a List  }
    public List GetProduct(int productId, int ownerId ) { // return a List  }
    public List GetProduct(int productId, int vendorId, boolean printInvoice) { // return a List  }
    

    In my small example, it quickly becomes unclear if the second int argument should be the owner or customer id.

提交回复
热议问题