开发者

Singular method name for single object argument, plural for a list?

开发者 https://www.devze.com 2022-12-27 21:27 出处:网络
I\'m having a problem with naming a method for a database application. In my Database instance, I have a method that can remove an Agreement object from the database. However, I want to be able to re

I'm having a problem with naming a method for a database application.

In my Database instance, I have a method that can remove an Agreement object from the database. However, I want to be able to remove multiple Agreements at once, to be able to use transactions. The problem is that I also have an overload to remove a single Agreement object.

Essentially, my structure is like this:

public class Database
{
    // ...

    public void RemoveAgreement(Agreement a)
    {
        // ...
    }

    public void RemoveAgreement(IEnumerable<Agreement> agreements)
    {
        // ...
    }
}

But this can be confusing, as the overload with the list of Agreements has a singular name, despite being inherently plural.

My question is, how should I structure this? Should I have two overloads with the name RemoveAgreement(), or RemoveAgreements()? Or shoul开发者_如何学编程d I use two separate methods, instead of overloads?

Thanks.


I'd say that for the method that receives the list as parameter the name RemoveAgreement is not correct for the reason you describe.

I would call it RemoveAgreements

For the name of your class (Database) I would say that you're using it as DAO for ALL your entities.

If you were using this class ONLY for Agreement entities I would have this 2 methods

public void Remove(Agreement agreement)

and

public void Remove(IEnumerable<Agreement> agreements)


Here's what I would do in that situation:

public void RemoveAgreement (Agreement agreement)
{
    // Do Stuff
}

public void RemoveAgreements (IEnumerable<Agreement> agreements)
{
    foreach (Agreement a in agreements)
    {
        RemoveAgreement(a);
    }
}


I recommend using separate names, and making an overload of the plural version that takes a params Agreement[].

0

精彩评论

暂无评论...
验证码 换一张
取 消