添加到容器(如果尚未存在)的方法的好名字

发布于 2024-07-30 07:21:08 字数 211 浏览 4 评论 0原文

对于向容器中添加某些内容(如果容器尚不存在)的方法来说,什么是一个好名称,即,

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

じее 2024-08-06 07:21:08

我会选择像 AddIfMissing 这样的东西,尽管我喜欢重命名为 Set 的想法,因为这就是它的本质。

public static class ListExtensions
{
      public static void AddIfMissing<T>( this List<T> list, T item )
      {
            if (!list.Contains(item))
            {
                list.Add( item );
            }
      }
}

I would go with something like AddIfMissing, though I like the idea of renaming to Set since that's really what it is.

public static class ListExtensions
{
      public static void AddIfMissing<T>( this List<T> list, T item )
      {
            if (!list.Contains(item))
            {
                list.Add( item );
            }
      }
}
递刀给你 2024-08-06 07:21:08

通常会使用“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).

酒解孤独 2024-08-06 07:21:08

您可以像 通用列表 那样创建一个 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.

无尽的现实 2024-08-06 07:21:08

使其有两种方法。 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.

余生共白头 2024-08-06 07:21:08

在我看来,你问这个问题是因为你设计的 OO 不是最优的:

void AddCustomerToList(CustomerList list, Customer customer)

确保客户是否必须在场的责任必须分配给
客户列表

在这种情况下,您将方法命名为:

  • Add(如果您在文档中指定仅在以下情况下才会添加客户)
    否则不存在
  • AddIfNotPresentPutIfNotPresent

我更喜欢后者,因为它更自动记录

in my opinion you are asking this question since you design OO is sub-optimal:

void AddCustomerToList(CustomerList list, Customer customer)

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 if
    not present
  • AddIfNotPresent or PutIfNotPresent otherwise.

I prefer the latter since it is more autodocumented.

蝶舞 2024-08-06 07:21:08

也许只是将其命名为 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(..).

不羁少年 2024-08-06 07:21:08

你没有说如果它不存在你会做什么。 假设答案是“没什么”,我会同意。

bool InsertIfNew (CustomerList list, Customer customer)

如果该方法已“插入”,则返回 true;如果已存在,则返回 false。 如果条目已经存在,调用者可以执行替代逻辑。 您可能不想这样做,但有人可能并且您已经掌握了例程中的知识。

You didn't say what you do if it isn't there. Assuming the answer is "nothing", I'd go with.

bool InsertIfNew (CustomerList list, Customer customer)

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.

驱逐舰岛风号 2024-08-06 07:21:08

将CustomerList改为CustomerSet,那就很明显了。

add(CustomerSet set, Customer customer);

Change CustomerList to CustomerSet, then it's obvious.

add(CustomerSet set, Customer customer);
ゃ人海孤独症 2024-08-06 07:21:08

如果不存在则添加

AddIfNotPresent

太阳公公是暖光 2024-08-06 07:21:08
bool TryAdd(CustomerList list, Customer customer)
bool TryAdd(CustomerList list, Customer customer)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文