Iris Classon
Iris Classon - In Love with Code

Stupid Question 94: How long should a method name be at most, and are acronyms ok?

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.

Comments

Leave a comment below, or by email.
Jimmy Bosse
11/30/2012 8:31:24 AM
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. 
Roe
11/30/2012 1:04:43 PM
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. 
Mike Reynolds
11/30/2012 8:41:45 PM
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 
DANIEL
12/1/2012 2:35:44 PM
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. 
Fredrik Björeman
12/3/2012 2:06:37 AM
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 :-) … 


Last modified on 2012-11-29

comments powered by Disqus