In Kent Beck's Implementation Patterns, one can read
"A common use of constants is to communicate variations of a message in an interface. For example, to center text you could invoke
setJustification(Justification.CENTERED)
. One advantage of this style of API is that you can add new variants of existing methods by adding new constants without breaking implementors. However, these messages don't communicate as well as having a separate method for each variation. In this style, the message above would bejustifyCentered()
. An interface where all invocations of a method have literal constants as arguments can be improved by giving it separate methods for each constant value."
Why is this? Generally when I'm coding and I notice that I have a couple of similar parameterless methods that could be reduced to just one, with an argument, like in the following example,
void justifyRight()
void justifyLeft()
void justifyCentered()
I'd generally do just the opposite of what Kent advices, which would be to group it into
setJustification(Justification justification)
How do you usually handle this situation? Is this totally subjective or there is really a very strong reason that I can't see in favour of Kent's view of this matter?
Thanks