Method Name for Long Method

Good style (Clean Code book) says that the name of the method should describe what this method does. For example, if I have a method that checks the address, stores it in the database, and sends an email if the name should be something like verifyAddressAndStoreToDatabaseAndSendEmail(address);

or

verifyAddress_StoreToDatabase_SendEmail(address);

although I can divide this functionality into 3 methods, I still need a method to call these 3 methods. Therefore, the name of a great method is inevitable.

Having AND named methods certainly describes what this method does, but IMO it is not very readable, since names can be very large. How would you decide?

EDIT: Perhaps I could use a free style to decompose the method name, for example:

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

but I need a way to ensure the order of the call. Perhaps using a state template, but this leads to code growth.

+4
source share
4 answers

As I approach this, I need to make 3 smaller methods, as you mentioned, and then in a higher method that calls 3 smaller ones, I will name it after the β€œwhy” I need to do these three things.

Try to determine why you need to complete these steps and use this as the basis of the method name.

+6
source

One method should not do 3 things. Thus, divide the work into 3 methods:

  • verifyAddress
  • storeAddress
  • Sendemail
+2
source

I follow my previous comment, but I have more than the one suitable for comment, so I respond.

The details of the method relate to documentation not by the name of the method (in my opinion). Think of it this way ... By putting SendEmail in the method name, you pass the implementation details of the method name. What if the decision is made along the way of sending notifications via SMS or Twitter or something else instead of email? Are you changing the method name and violating your API, or do you have a method name that misleads the consumers of the API? Something to consider.

If you insist on maintaining the functionality of a method in its name, I would ask you to find something more general. Maybe something similar to VerifySaveAndNotify(Address address) . So the method name tells you what it does, without specifying how it does it. A parameter of type Address allows you to know what is checked and stored. All of this works together to make your method name informative, flexible, and concise.

+2
source

EDIT: Perhaps I could use a free style to decompose the method name, for example:

verifyAddress (address) .storeToDatabase () SendEmail () ;.

but I need a way to ensure the order of the call. Perhaps using a state template, but this leads to code growth.

To ensure that teams are ordered in a free style, each result will be an object that provides only the functionality needed for the next step. For instance:

 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 convenient if you need to create a very granular design and you want to optimize your classes for reuse, but in most cases this can be an overuse design. Better, probably just like the others said, to choose a name that represents what the whole process should represent. You can simply call it "StoreAndEmail", making the assumption that verification is what you do regularly before transferring data to any destination. An alternative, if you do not mind the names being long, is simply to describe them fully and to acknowledge that a long name is required. In the end, it really costs you nothing, but it can certainly make your code more specific in purpose.

0
source

All Articles