When is a Java method name too long? [closed]

0 votes
asked Feb 9, 2010 by mexicanhacker

In the last weeks I've seen some guys using really long names for a Method or Class (50 characters), this is usually under the premise that it improves readability, my opinion is that a long name like this is an indicator that we are trying to do a lot or too much in a method class if we need such a long name, however I wanted to know what do you guys think about it.

An Example is:


20 Answers

0 votes
answered Jan 9, 2010 by s-lott

Context "...WithinTransaction" should be obvious. That's what object-orientation is all about.

The method is part of a class. If the class doesn't mean "Transaction" -- and if it doesn't save you from having to say "WithinTransaction" all the time, then you've got problems.

0 votes
answered Jan 9, 2010 by rex-kerr

My rule is as follows: if a name is so long that it has to appear on a line of its own, then it is too long. (In practice, this means I'm rarely above 20 characters.)

This is based upon research showing that the number of visible vertical lines of code positively correlates with coding speed/effectiveness. If class/method names start significantly hurting that, they're too long.

Add a comment where the method/class is declared and let the IDE take you there if you want a long description of what it's for.

0 votes
answered Jan 9, 2010 by bill-the-lizard

The length of the method itself is probably a better indicator of whether it's doing too much, and even that only gives you a rough idea. You should strive for conciseness, but descriptiveness is more important. If you can't convey the same meaning in a shorter name, then the name itself is probably okay.

0 votes
answered Jan 9, 2010 by kaz-dragon

As with any other language: when it no longer describes the single action the function performs.

0 votes
answered Jan 9, 2010 by uckelman

An identifier name is too long when it exceeds the length your Java compiler can handle.

0 votes
answered Jan 9, 2010 by flybywire

Java has a culture of encouraging long names, perhaps because the IDEs come with good autocompletion.

This site says that the longest class name in the JRE is InternalFrameInternalFrameTitlePaneInternalFrameTitlePaneMaximizeButtonWindowNotFocusedState which is 92 chars long.

As for longest method name I have found this one supportsDataDefinitionAndDataManipulationTransactions, which is 52 characters.

0 votes
answered Jan 9, 2010 by flybywire

Some techniques for reducing the length of method names:

  1. If your whole program, or class, or module is about 'skin care items' you can drop skin care. For example, if your class is called SkinCareUtils, that brings you to getNumberOfEligibleItemsWithinTransaction

  2. You can change within to in, getNumberOfEligibleItemsInTransaction

  3. You can change Transaction to Tx, which gets you to getNumberOfEligibleItemsInTx.

  4. Or if the method accepts a param of type Transaction you can drop the InTx altogether: getNumberOfEligibleItems

  5. You change numberOf by count: getEligibleItemsCount

Now that is very reasonable. And it is 60% shorter.

0 votes
answered Jan 9, 2010 by mark-byers

Just for a change, a non-subjective answer: 65536 characters.

A.java:1: UTF8 representation for string "xxxxxxxxxxxxxxxxxxxx..." is too long for the constant pool


0 votes
answered Jan 9, 2010 by hackergil

There are two ways or points of view here: One is that it really doesn't matter how long the method name is, as long as it's as descriptive as possible to describe what the method is doing (Java best practices basic rule). On the other hand, I agree with the flybywire post. We should use our intelligence to try to reduce as much as possible the method name, but without reducing it's descriptiveness. Descriptiveness is more important :)

0 votes
answered Jan 9, 2010 by sal

I tend use the haiku rule for names:

 Seven syllable class names 
 five for variables
 seven for method and other names

These are rules of thumb for max names. I violate this only when it improves readability. Something like recalculateMortgageInterest(currentRate, quoteSet...) is better than recalculateMortgageInterestRate or recalculateMortgageInterestRateFromSet since the fact that it involves rates and a set of quotes should be pretty clear from the embedded docs like javadoc or the .NET equivalent.

NOTE: Not a real haiku, as it is 7-5-7 rather than 5-7-5. But I still prefer calling it haiku.

Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter