Nov 292012
 
 November 29, 2012  Posted by at 8:00 am Not So Stupid Questions  Add comments

I use the five-words max rule, and generally avoid acronyms for short names (methods and variables), but use the more common ones in longer names. Sometimes I find that very long names indicate that the method does to much, or its just a bad name. I write down suggestions on paper and compare them, the one most people would understand gets picked.

  5 Responses to “Stupid Question 94: How long should a method name be at most, and are acronyms ok?”

  1. My previous employer had a great post about thinking about variable names like you would name a child: http://blog.logicboost.com/2009/04/13/name-a-variable-like-you-name-your-first-born/

    I think the same applies to methods.

  2. You want your functions to be self documenting in their purpose, however you also don’t want them to be too long. I don’t have a rule that I follow, however I would agree with the quote above that the 5 word limit makes sense. If you need more than 5 words to describe the purpose of a function then you might ask yourself if this function should be broken down into more granular functions to better follow SOLID Principals.

  3. I think the number five from whatever source you’ve read is related to the magic number in psychology (see http://en.wikipedia.org/wiki/The_Magical_Number_Seven,_Plus_or_Minus_Two) — basically the magic number in psychology is a number in the range of 5 to 9 highlighting the number of “chunks” of information we humans can keep in mind at any give time due to limitations of working memory.

    However, I believe method names should contain less than five words — all depending on word length for each word. It’s difficult to read a method name that is way too long, and I have seen methods composed of three words that are way too long to be considered reasonable or comprehensible.

    Mike

  4. I would say that scope is important here aswell, if you have a long scope or your methods are exposed as an external an API or a library it’s important that the method names are simple and yet descriptive. If you have a short scope, a private method the name can be more wordy. Yet, a method should do one thing and one thing only, otherwise it breaks the single responsibility principle.

  5. One of my favourite long method names, discovered deep inside Apple’s private networking methods:

    NetConnection::closeStreamsIfPossibleOrSignalThatThatNeedsToBeDonePrettyPlease()

    In Apple-land, the rule of thumb is pretty much to use names which read well, are explicit and don’t worry about length so much. Named parameters really help making this useful too, once you get used to it you start to want similar naming in other languages 🙂 …

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)

What is 13 + 4 ?
Please leave these two fields as-is:
IMPORTANT! To be able to proceed, you need to solve the following simple math (so we know that you are a human) :-)