Method Names

Quick, what does this method do:

isSupressMultiLanguage()

If you guessed that it determines if multi-language is turned on or off you’d be correct.

Quick, what does this method do:

isMultiLanguageSupressed()

If you guessed that it is does the same thing, you’d be correct.

The point of this is that each is readable. One may flow off the tongue slightly easier than the other, however both are perfectly legitimate verbiages for the underlying flag. Both point directly to the intent of the method and give a clear picture of what its purpose is.

So what is the opportunity cost? Zero. From a development standpoint it is pretty clear that these mean the same thing and a developer can get a feel for what each method does at a glance. As long as a developer can tell what a method does from just the name, the actual naming of the method is just a matter of personal preference.


About the author

Jason McDonald

View all posts

5 Comments

  • If the variable itself is used more than the method the first is better. That way you could say supressMultiLanguage inline and have it read like plain english. If you were calling the method more then the second one would be better because it reads more like plain english.

    Its all about where you call things from the most.

  • Last comment then I’m going to let the common consensus take over.

    “2. True doesn’t mean true. A “true” value means “off” or false. Better to make true mean true.”

    This rings true (no pun intended) for both representations…

    isSupressMultiLanguage() – true means its disabled, which is the primary state we care about.

    isMultiLanguageSupressed() – true also means it is disabled.

    Your argument for the mental bit switching is a valid one, just not in this particular case, primarily due to the need surrounding the variable/method. It should be noted that the best name for a method isn’t a cut or dry thing, hence my entire argument in the original post, and that it is extremely context sensitive. In this above case the “mental bit switching” is negated by the fact that we simply want to know if the multi-language feature is off. If we were wanting to know if it was on, this method name would be counter-intuitive. And we would likely want something that wouldn’t require the “mental bit switching”.

  • Fowler has a Refactoring Pattern called “Rename Method” to make code easier to read. He thinks it’s pretty important. I agree with him. I’m sure you and most others do, too.

    So what’s the problem with “isSuppressMultiLingual”? It’s not the end of the world, but it’s not as simple as it could be.

    1. isSuppressMultiLingual isn’t English. It takes an extra cycle or two to parse.

    2. True doesn’t mean true. A “true” value means “off” or false. Better to make true mean true.

    Taken together, you have to first parse broken English and then flip the bit in your head to understand if it’s on or off.

    Compare that with:

    isMultiLanguageEnabled( )

    Active voice, plain English, and true means true.

    If this were the only instance of code like this in a codebase, no big deal. Life goes on. The code still works. But when little things like this are repeated hundreds and thousands of times in a large project written by a lot of junior people … well, you’ve seen the result of that.

    To prevent a big nasty mess, everyone on a team has to be pay attention to little details like this.

  • I don’t really see how one could interpret the first method any way other than that it represents a boolean value (in java at least, which is the language in question for this post).

    If we were checking to see if the Suppress object was multi-language it would be:

    suppress.isMultiLanguage()

    not

    [some object].isSupressMultiLanguage()

    I’m not arguing for one being more virtuous than the other – clearly the second way is closer to English and thus easier to read (for native English speakers) – I am simply making the argument that both are perfectly understandable and thus legitimate options.

  • I greatly prefer the second, actually. Mostly because It is grammatically correct in English. For the first example, in normal prose we would either have to switch to active voice (as oppose to passive voice) making it “areWeSuppressingMultiLanguage”, or even worse, add a sort of caveat, as in “is it suppressed? (multi-language).”

    To understand the first example, native English speakers have to perform mental transformation on the words. They may get it right the first time, or they may interpret it incorrectly a couple times before they get it right (decreasing readability), or, worst of all, they may decide on a the meaning incorrectly. For instance, in this case, one might decide that there was an object “suppress”, and we are checking to see whether or not it is multi-language.

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.