开发者

method name for a long method

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);

alth开发者_如何学Pythonough 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.


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.


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

  1. verifyAddress
  2. storeAddress
  3. sendEmail


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.


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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜