Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

methods

People also ask

How long is too long for a method name?

It is 65535 characters to be exact. The same rule applies to the length of the class level variables as well (but not to the local variables - the one which is defined inside a method).

How long should method names be in Java?

Use short enough and long enough variable names in each scope of code. Generally length may be 1 char for loop counters, 1 word for condition/loop variables, 1-2 words for methods, 2-3 words for classes, 3-4 words for globals.

Why are Java method names so long?

The classes and variables have long names because the things they represent have long names. Understanding them doesn't make them shorter. But they aren't Conventions, they are InternationalizationConventions.

Are long variable names bad?

As per standards, longer descriptive names are advised to make it more readable and maintainable on longer term. If you use very short naming e.g. a variable as a , you will forget yourself, what that variable is meant for after sometime. This becomes more problematic in bigger programs.


A name in Java, or any other language, is too long when a shorter name exists that equally conveys the behavior of the method.


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.


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

;-)


I agree with everyone: method names should not be too long. I do want to add one exception though:

The names of JUnit test methods, however, can be long and should resemble sentences.

Why?

  • Because they are not called in other code.
  • Because they are used as test names.
  • Because they then can be written as sentences describing requirements. (For example, using AgileDox)

Example:

    @Test
    public void testDialogClosesDownWhenTheRedButtonIsPressedTwice() {
        ...
    }

See "Behavior Driven Design" for more info on this idea.


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.


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.


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.