添加到容器(如果尚未存在)的方法的好名字
对于向容器中添加某些内容(如果容器尚不存在)的方法来说,什么是一个好名称,即,
void AddCustomerToList(CustomerList list, Customer customer)
但该名称不能正确传达如果容器尚不存在则不会添加该内容。 什么是更好的名字? 如果不存在则将客户添加到列表? 确保客户在列表中?
What's a good name for a method that adds something to a container if it's not already there, i.e.
void AddCustomerToList(CustomerList list, Customer customer)
but that name does not properly convey that it won't be added if it's not there already. What is a better name? AddCustomerToListIfNotThereAlready? EnsureCustomerInList?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
我会选择像
AddIfMissing
这样的东西,尽管我喜欢重命名为 Set 的想法,因为这就是它的本质。I would go with something like
AddIfMissing
, though I like the idea of renaming to Set since that's really what it is.通常会使用“put”而不是“add”来表达这一点,但是我同意chase,您应该将其称为“add”并使用“set”而不是“list”。 当然,除非容器支持这两种操作(这很奇怪)。
Usually "put" would be used instead of "add" to convey this, but I agree with chase that you should just call this "add" and use "set" instead of "list". Unless of course the container supports both operations (which would be odd).
您可以像 通用列表 那样创建一个 ContinsCustomer 函数来首先检查它是否存在,如果返回 false,则使用 AddCustomerToList。
You could do like the generic list does and create a ContinsCustomer function to check if it exists first, then use AddCustomerToList if it returns false.
使其有两种方法。 IsCustomerPresent() AddCustomer()。 然后,如果您愿意,您可以创建一个仅调用松散耦合逻辑的 AddCustomerIfNotAlreadyPresent() 方法。
Make it two methods. IsCustomerPresent() AddCustomer(). Then if you want you could make a AddCustomerIfNotAlreadyPresent() method that just calls your loosely coupled logic.
在我看来,你问这个问题是因为你设计的 OO 不是最优的:
确保客户是否必须在场的责任必须分配给
客户列表
。在这种情况下,您将方法命名为:
Add
(如果您在文档中指定仅在以下情况下才会添加客户)否则不存在
AddIfNotPresent
或PutIfNotPresent
。我更喜欢后者,因为它更自动记录。
in my opinion you are asking this question since you design OO is sub-optimal:
the responsability to ensure if a customer must be present must be assigned to
CustomerList
.In that case you name your method:
Add
in the case you specify in the documentation that the customer will be added only ifnot present
AddIfNotPresent
orPutIfNotPresent
otherwise.I prefer the latter since it is more autodocumented.
也许只是将其命名为 AddCustomerToList,并且不要让它检查它是否已经存在,而是在最后调用另一个方法,RemoveMultipleOccurences(..)。
Maybe just name it AddCustomerToList, and don't make it check to see if its already there, instead at the end, call another method, RemoveMultipleOccurences(..).
你没有说如果它不存在你会做什么。 假设答案是“没什么”,我会同意。
如果该方法已“插入”,则返回 true;如果已存在,则返回 false。 如果条目已经存在,调用者可以执行替代逻辑。 您可能不想这样做,但有人可能并且您已经掌握了例程中的知识。
You didn't say what you do if it isn't there. Assuming the answer is "nothing", I'd go with.
The method returns true if it was "inserted", and false if it was already there. That the caller can perform alternate logic if the entry was already there. You might not want to do that, but someone might and you already have the knowledge inside the routine.
将CustomerList改为CustomerSet,那就很明显了。
Change CustomerList to CustomerSet, then it's obvious.
如果不存在则添加
AddIfNotPresent