Ask ‘What Is Its Purpose?’

 

Naming a class a ‘Helper’, as in CustomerHelper or BudgetHelper, isn’t very helpful. For both these classes and all such ‘Helpers’, it is not clear from the name what they do—what role they play in a program.

A class named ‘Helper’ indicates a lack of Cohesion. Why do we need PaymentGatewayHelper? What is it PaymentGateway alone cannot do that it requires a helper? Why not combine PaymentGateway and the helper into a module that can do what needs doing?!

Possibly the code in PaymentGatewayHelper is quite distinct from the behaviour in PaymentGateway. In that case, can we come up with a better name describing this distinction with clarity? Yes, of course, we can. It might not be easy, but it is possible. 

How do we know that we have a fitting name for a class? 

When we can ask ‘What Is Its Purpose?‘ or ‘What Does It Do?‘ and the answer—this singular, cohesive purpose—resonates with the class name.

PaymentGatewayHelper is not meeting the threshold. Asking ‘What is its purpose?’ does not clarify—it helps with payment gateway stuff?? 

How about RegisterNewCustomerWorkflow? That’s more like it—this class is concerned with coordinating the high-level workflow of registering a new customer. Nice. 

What about these?

  • SalesTaxSelector — Select a sales tax.
  • UserSource — A source of users
  • InvalidTransactionDate (in an Exceptions folder) — An exception for when the transaction date is not valid
  • AccountGroupTransactionList — A list of transactions for a group of accounts

They’re all well-named.

Few points:

  • A good name alone is not enough. The class must also do what it says. It must tell the truth.
  • I find it more useful when a name describes what it does, as in SalesTaxSelector, rather than the internal implementation, as in SalesTaxFactory—using a Factory pattern. This approach to naming our classes has the added advantage that we may change the underlying implementation, yet the name does not need to be modified.

When the purpose of a class isn’t reasonably clear from the name alone, it’s not a good name. If you find yourself in this situation, consider changing the class name.

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply