Should a method name describe what it does or what it intends?

Bob Martin raises a good example in InformIT: Robert C. Martin’s Clean Code Tip of the Week #1: An Accidental Doppelgänger in Ruby > Duplication of two functions that do the same thing but mean different things by it.

I recently stumbled into a slightly different take on the question of should a function say what it does or what it intends?

When a  function implements business process Alpha that today consists of steps A and B (but tomorrow  may change) should you call the function DoBusinessProcessAlpha or call it DoStepsAandB?

One answer would be, if the function is in a public package which exposes business functionality then the name should probably show that it does BusinessProcessAlpha. But if it is a private, not exposed, function then the reader is probably looking for the detail, that it does StepsAandB.

The question is more awkward if Steps A and B are themselves business process functions. That is, if you asked your customer, they would understand what steps A and B mean.

I suppose in that case you could always call it DoAlphaAsandB().

3 thoughts on “Should a method name describe what it does or what it intends?”

  1. It’s an interesting take, but if Business Process Alpha changes to also include step C, or to remove Step B, not only do you have to modify the contents of the StepsAandB method, but you also have to rename it and update all the calling sites to use the new name.
    The fact that process Alpha consists of specific named steps is an implementation detail of the DoProcessAlpha method and not the business of any callers of that method. A caller which needs Steps A and B, so it calls DoProcessAlpha as a convenience to get both at once, is violating encapsulation. DoProcessAlpha is a method reserved for callers which need to execute process Alpha, whatever steps it may contain. Anybody who needs to interact with the individual steps outside of an Alpha context need to make a new method to call those steps directly.

Leave a Reply

Your e-mail address will not be published. Required fields are marked *