长方法的方法名称

发布于 2024-12-05 00:13:04 字数 514 浏览 0 评论 0原文

良好的风格(《清洁代码》书)说,方法的名称应该描述该方法的用途。例如,如果我有一个验证地址、将其存储在数据库中并发送电子邮件的方法,那么名称应该是 verifyAddressAndStoreToDatabaseAndSendEmail(address);

verifyAddress_StoreToDatabase_SendEmail(address) );

虽然我可以将该功能分为 3 个方法,但我仍然需要一个方法来调用这 3 个方法。所以大的方法名是不可避免的。

拥有并命名的方法当然描述了该方法的作用,但在我看来,它的可读性不是很好,因为名称可能非常非常大。你会如何解决它?

编辑:也许我可以使用流畅的风格来分解方法名称,例如:

verifyAddress(address).storeToDatabase().sendEmail();

但我需要一种方法来确保调用顺序。也许通过使用状态模式,但这会导致代码增长。

The good style (Clean Code book) says that a method's name should describe what the method does. So for example if I have a method that verifies an address, stores it in a database, and sends an email, should the name be something such as verifyAddressAndStoreToDatabaseAndSendEmail(address);

or

verifyAddress_StoreToDatabase_SendEmail(address);

although I can divide that functionality in 3 methods, I'll still need a method to call these 3 methods. So a large method name is inevitable.

Having And named methods certainly describes what the method does, but IMO it's not very readable as names can be very very large. How would you solve it?

EDIT: Maybe I could use fluent style to decompose the method name such as:

verifyAddress(address).storeToDatabase().sendEmail();

but I need a way to ensure the order of invocation. Maybe by using the state pattern, but this causes the code to grow.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

笑叹一世浮沉 2024-12-12 00:13:05

我的方法是创建您提到的 3 个较小的方法,然后在调用 3 个较小的方法的更高方法中,我以“为什么”我需要做这三件事来命名它。

尝试定义为什么需要执行这些步骤并将其用作方法名称的基础。

How I approach this is to make the 3 smaller methods as you mentioned and then in the higher method that calls the 3 smaller ones, I name it after the "why" I need to do those three things.

Try to define why you need to do those steps and use that as the basis of the method name.

断肠人 2024-12-12 00:13:05

一个方法不应该做三件事。因此将工作分为3个方法:

  1. verifyAddress
  2. storeAddress
  3. sendEmail

A single method should not do 3 things. Thus divide the work into 3 methods:

  1. verifyAddress
  2. storeAddress
  3. sendEmail
北方的巷 2024-12-12 00:13:05

我正在跟进我之前的评论,但我在这里得到的内容超出了评论的合理范围,所以我正在回答。

该方法的详细信息属于文档而不是方法名称(在我看来)。可以这样想……通过将 SendEmail 放在方法名称中,您就可以将实现细节提交给方法名称。如果以后决定通过短信、推特或其他方式而不是电子邮件发送通知怎么办?您是否更改了方法名称并破坏了您的 API,或者您的方法名称是否误导了 API 的使用者?需要考虑的事情。

如果您坚持在其名称中保留该方法的功能,我建议您找到更通用的名称。也许类似于VerifySaveAndNotify(Address address)。这样,方法名称就可以告诉您它正在做什么,而无需指定它如何执行该操作。 Address 类型的参数让您知道正在验证和保存的内容。所有这些共同作用使您的方法名称信息丰富、灵活且简洁。

I'm following up on my previous comment, but I've got more here than would fit reasonably in a comment so I'm answering.

The details of the method belong in the documentation not in the name of the method (in my opinion). Think of it this way... By putting SendEmail in the name of the method, you're committing implementation details to the method name. What if a decision is made down the road to send notification via SMS or twitter or something else instead of email? Do you change the name of the method and break your API, or do you have a method name that misleads the consumers of the API? Something to consider.

If you insist on keeping the functionality of the method in its name, I'd urge you to find something more generic. Perhaps something along the lines of VerifySaveAndNotify(Address address). That way, the method name tells you what it's doing without specifying how it does it. The parameter of type Address let's you know what is being verified and saved. All of that works together to make your method name informative, flexible, and terse.

失与倦" 2024-12-12 00:13:05

编辑:也许我可以使用流畅的风格来分解方法名称,例如:

verifyAddress(地址).storeToDatabase().sendEmail();

但我需要一种方法来确保调用的顺序。也许可以使用状态模式,但这会导致代码增长。

为了确保以流畅的方式对命令进行排序,每个结果都将是一个仅公开下一步所需功能的对象。例如:

public class Verifier
{
    public DataStorer VerifyAddress(string address)
    { 
        ...
        return new DataStorer(address);
    }
}

public class DataStorer
{
    public Emailer StoreToDataBase()
    {
        ...
        return new Emailer(...);
    }
}

public class Emailer
{
    public void SendEmail()
    {
        ...
    }
}

如果您需要创建非常精细的设计并希望优化类以实现可重用性,那么这很方便,但在大多数情况下可能是设计过度的。正如其他人所说,最好选择一个代表整个过程应该代表的名称。您可以简单地将其称为“StoreAndEmail”,假设验证是您在将数据提交到任何目的地之前例行执行的操作。如果您不介意名称太长,另一种选择是简单地完整描述它并接受长名称是必要的。最后,它确实不会花费您任何费用,但肯定可以使您的代码的意图更加具体。

EDIT: Maybe I could use fluent style to decompose the method name such as:

verifyAddress(address).storeToDatabase().sendEmail();

but I need a way to ensure the order of invocation. Maybe by using the state pattern, but this causes the code to grow.

To ensure ordering of commands in a fluent style, each result would be an object that exposes only the functionality required by the next step. For example:

public class Verifier
{
    public DataStorer VerifyAddress(string address)
    { 
        ...
        return new DataStorer(address);
    }
}

public class DataStorer
{
    public Emailer StoreToDataBase()
    {
        ...
        return new Emailer(...);
    }
}

public class Emailer
{
    public void SendEmail()
    {
        ...
    }
}

This is handy if you need to create a very granular design and want to optimise your classes for reuseability, but is likely to be design overkill under most circumstances. Better probably as others have said to choose a name that represents what the whole process is supposed to represent. You could simply call it "StoreAndEmail", making an assumption that verification is something you do routinely before committing data to any destination. The alternative if you don't mind names being long is to simply describe it in full and accept that a long name is necessary. In the end, it really doesn't cost you anything, but can certainly make you code more specific in its intent.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文