定义两个具有相同参数类型的方法
今天我遇到了一个场景,我必须创建一个与现有方法共享相同名称、参数计数和参数类型
的方法,如下所示:
public static Department GetDepartment(string departmentName)
{
//LOGIC
}
public static Department GetDepartment(string employeeID)
{
//LOGIC
}
乍一看我只是说为什么不命名它用不同的名字来完成事情,但我做不到!我确实想保持我正在处理的代码的可读性,我希望它重载
到第一个代码,
所以我说为什么不添加一个假参数来从编译器的角度解决这个问题。
public static Department GetDepartment(string employeeID, object fakePassWtEver)
{
//LOGIC
}
此案例的最佳实践是什么?我看到了所有可以让我的代码运行的方法,但没有一个让我满意
Today I ran into a scenario where I have to create a method that share the same name, params count and params types
with existent one, Something like this:
public static Department GetDepartment(string departmentName)
{
//LOGIC
}
public static Department GetDepartment(string employeeID)
{
//LOGIC
}
at first glance I just said why not to name it with a different name and get things done, but I couldn't! I do want to maintain the readability of my code i'm working on, I want it to be overloaded
to the first one,
so I said why not to add a fake parameter just to workaround this issue from the compiler Point of view.
public static Department GetDepartment(string employeeID, object fakePassWtEver)
{
//LOGIC
}
What is the best practice for this case? I see all the ways can let my code run, but none of them satisfied me
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
保持可读性正是您应该重命名它的原因:
现在,无论您何时调用该方法,您所指的方法都绝对明显。如果您重载该方法,情况就完全不同了。
随着时间的推移,我变得越来越不愿意超载 - 有相当多的微妙问题 ,并且可读性经常下降。
Maintaining readability is precisely why you should rename it:
Now whenever you call the method, it's absolutely obvious which one you mean. That's very much not the case if you overload the method instead.
I've become increasingly reluctant to overload over time - there are quite a few subtle issues, and readability very often goes down.
您可以通过执行以下操作来更新方法签名,同时使代码更具可读性。
就我个人而言,我认为在代码中添加冗长的内容可以帮助后来的其他人理解发生了什么。它还有助于使您的方法更容易“阅读”。
You could update your method signatures and make your code more readable at the same time by doing something like the following.
Personally I feel that adding verbosity to code helps others that come later understand what's going on. It also helps make your methods "read" more easily.
定义2个方法:
public static Department GetDepartmentByDepartmentName(string DepartmentName)
public static Department GetDepartmentByEmployeeID(string employeeID)
Define 2 methods:
public static Department GetDepartmentByDepartmentName(string departmentName)
public static Department GetDepartmentByEmployeeID(string employeeID)
如果您可以通过检查参数以某种方式区分员工 ID 和部门名称,则另一种选择是委托给其他方法。
仅当您绝对无法添加其他方法来清楚起见时,您才应该这样做 - 其他答案 100% 正确。
Another option would be to delegate to other methods if you can somehow distinguish between an employee ID and a department name by examining the argument.
You should only do this if you absolutely cannot add another method for clarity - the other answers are 100% on point.
有点晚了,但有可能,我今天遇到了完全相同的情况(构造函数重载,因此无法更改名称)。这是我的做法,虽然是个小技巧,但它让我可以将所有相关的 LINQ 谓词放在同一个位置:
现在的技巧是使用参数名称来调用它们,如下所示:
A bit late but it is possible, I had the exact same scenario today (constructor overloading, thus can't change name). Here is how I did it, small hack but it lets me have all my LINQ predicates that are related in the same place:
Now the trick is to call them using parameter names like so: