方法重载。你能过度使用它? [英] Method Overloading. Can you overuse it?

查看:94
本文介绍了方法重载。你能过度使用它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

定义几个方法,返回的数据与不同的过滤器相同的形状时,什么是更好的做法?明确的方法名称或重载方法?

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 example. If I have some Products and I'm pulling from a database

明确的方式:

public List<Product> GetProduct(int productId) {    // return a List    }
public List<Product> GetProductByCategory(Category category) {    // return a List    }
public List<Product> GetProductByName(string Name ) {    // return a List    }

重载方式:

public List<Product> GetProducts() {    // return a List of all products    }
public List<Product> GetProducts(Category category) { // return a List by Category }
public List<Product> GetProducts(string searchString ) { // return a List by search string }

我意识到你可能会进入一个问题的类似的签名,但如果你传递对象,而不是基本类型(字符串,整数,字符,日期时间等),这将是更小的问题。所以...这是一个好主意,为重载方法,以减少你有方法和清晰的数字,每种方法该过滤的数据以不同的方式有不同的方法名称

I realize you may get into a problem with similar signatures, but if you're passing objects instead of base types (string, int, char, DateTime, etc) this will be less of an issue. So... is it a good idea to overload a method to reduce the number of methods you have and for clarity, or should each method that filters the data a different way have a different method name?

推荐答案

是的,超载可以很容易被滥用。

Yes, overloading can easily be overused.

我发现的关键,制定过载是否正当与否是考虑观众 - 不是编译器,但维护程序员谁将会沿着周/月/年现身,并有了解什么在code要达到的目的。

I've found that the key to working out whether an overload is warranted or not is to consider the audience - not the compiler, but the maintenance programmer who will be coming along in weeks/months/years and has to understand what the code is trying to achieve.

样的GetProducts一个简单的方法名()是明确和可以理解的,但它留下了很多没说。

A simple method name like GetProducts() is clear and understandable, but it does leave a lot unsaid.

在很多情况下,如果()传递的GetProducts参数都很好命名,维护家伙就能摸出超负荷做什么 - 但是这依赖于良好的命名纪律在使用的时候,你可以' Ť执行。你可以强制执行是他们正在调用该方法的名称。

In many cases, if the parameter passed to GetProducts() are well named, the maintenance guy will be able to work out what the overload does - but that's relying on good naming discipline at the point of use, which you can't enforce. What you can enforce is the name of the method they're calling.

这是我遵循的原则是只有超负荷的方法,如果他们是可以互换 - 如果他们做同样的事情。这样的话,我不介意哪个版本我班的消费者调用,因为他们是等价的。

The guideline that I follow is to only overload methods if they are interchangable - if they do the same thing. That way, I don't mind which version the consumer of my class invokes, as they're equivalent.

要说明这一点,我会愉快地使用重载的的DeleteFile()方法:

To illustrate, I'd happily use overloads for a DeleteFile() method:

void DeleteFile(string filePath);
void DeleteFile(FileInfo file);
void DeleteFile(DirectoryInfo directory, string fileName);

不过,对于你的例子,我会使用不同的名称:

However, for your examples, I'd use separate names:

public IList<Product> GetProductById(int productId) {...}
public IList<Product> GetProductByCategory(Category category) {...}
public IList<Product> GetProductByName(string Name ) {...}

拥有的全名,使code更明确的维修家伙(谁很可能是我)。它避免了与有签名的碰撞问题:

Having the full names makes the code more explicit for the maintenance guy (who might well be me). It avoids issues with having signature collisions:

// No collisions, even though both methods take int parameters
public IList<Employee> GetEmployeesBySupervisor(int supervisorId);
public IList<Employee> GetEmployeesByDepartment(int departmentId);

也有介绍过载每个目的的机会:

There is also the opportunity to introduce overloading for each purpose:

// Examples for GetEmployees

public IList<Employee> GetEmployeesBySupervisor(int supervisorId);
public IList<Employee> GetEmployeesBySupervisor(Supervisor supervisor);
public IList<Employee> GetEmployeesBySupervisor(Person supervisor);

public IList<Employee> GetEmployeesByDepartment(int departmentId);
public IList<Employee> GetEmployeesByDepartment(Department department);

// Examples for GetProduct

public IList<Product> GetProductById(int productId) {...}
public IList<Product> GetProductById(params int[] productId) {...}

public IList<Product> GetProductByCategory(Category category) {...}
public IList<Product> GetProductByCategory(IEnumerable<Category> category) {...}
public IList<Product> GetProductByCategory(params Category[] category) {...}

code读多了很多比它是写 - 即使你从来没有回来了code初步检查后,到源代码控制,你仍然会被读取行的codeA夫妇十几次,你写的code后面。

Code is read a lot more than it is written - even if you never come back to the code after the initial check in to source control, you're still going to be reading that line of code a couple of dozen times while you write the code that follows.

最后,除非你正在编写一次性code,你需要让其他人从其他语言调用你code。看来,大多数业务系统最终会停留在生产早已过日期的使用。这可能是code,消耗类在2016年最终被写在VB.NET,C#6.0,F#或完全新的东西,这不是发明出来。这可能是因为语言不支持重载。

Lastly, unless you're writing throwaway code, you need to allow for other people calling your code from other languages. It seems that most business systems end up staying in production well past their use by date. It may be that the code that consumes your class in 2016 ends up being written in VB.NET, C# 6.0, F# or something completely new that's not been invented yet. It may be that the language doesn't support overloads.

这篇关于方法重载。你能过度使用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆