- When is a java method name too long
- When is a Java method name too long?
- Max name length of variable or method in Java
- Does the method name length have any impact whatsoever on the performance?
- Is there anything inherently wrong with long variable/method names in Java? [duplicate]
- maximum length of method names
- Longest Class, Method and Attribute Names in Java
- Popular Articles
- Comments on «Longest Class, Method and Attribute Names in Java»
When is a java method name too long
Run those tests with long method names, then refactor them to short method names and rerun the tests. Also found similar question (though it didn’t appear in my initial search, or when I typed the question title which is weird): Maximum Method Name Length Solution 3: If you go over the size limit imposed by the VM for method names then you get a compiler error (at least with the version of javac I am using): Solution 1: Arguably it will take more space in memory and storage — so a jar file containing classes with enormous method names will be larger than one with short class names, for example.
When is a Java method name too long?
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:
- 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
- You can change within to in , getNumberOfEligibleItemsInTransaction
- You can change Transaction to Tx, which gets you to getNumberOfEligibleItemsInTx .
- Or if the method accepts a param of type Transaction you can drop the InTx altogether: getNumberOfEligibleItems
- 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
How do I write method names in Java?, While writing a method name we should follow the camel case i.e. first letter of the first word should be small and the first letters of the
Max name length of variable or method in Java
If I’m not mistaken, the limit is not in the language itself but in the classfile format, which limits names to 64k, so for all practical intents and purposes identifier length is not a problem. Specifically, this is the definition of a constant string in the pool, which seems to imply the maximal length is 16 bit:
Class names may be more of an issue for file systems, I agree, I’m not sure what’s currently supported.
JLS: An identifier is an unlimited-length sequence of Java letters and Java digits, the first of which must be a Java letter.
Also found similar question (though it didn’t appear in my initial search, or when I typed the question title which is weird): Maximum Method Name Length
If you go over the size limit imposed by the VM for method names then you get a compiler error (at least with the version of javac I am using):
Main.java:1: UTF8 representation for string «aaaaaaaaaaaaaaaaaaaa. » is too long for the constant pool
Inserting line break after method name in Java method declaration, There is no specified line indentation in Java. That means unline Python, in Java indentations are not syntactically counted.
Does the method name length have any impact whatsoever on the performance?
Arguably it will take more space in memory and storage — so a jar file containing classes with enormous method names will be larger than one with short class names, for example.
However, any difference in performance is incredibly unlikely to be noticeable. I think it’s almost certain that the projects where they were blaming long method names for poor performance were actually misdiagnosed. It’s not like it would be the first time that’s happened.
Of course, the best way to take the heat out of this situation is to provide evidence — if performance is important, you should have tests for performance. Run those tests with long method names, then refactor them to short method names and rerun the tests. I’d be incredibly surprised if there were a significant difference.
Method names are not just relevant with reflection but also during class loading and of course in both cases a long method names means that at some level there is more for the CPU to do. However, with method name length that are even remotely practical (i.e. not thousands of characters long), I am absolutely certain that it’s impossible for this to be significant compared to other things that have to be done during reflection or class loading.
But the client, and there were some people in the meeting arguing in this, was sure about this. They had some projects in that long method names were the cause of poor performance.
It sounds like a total guess being treated as fact. This is just a case of some people’s general nuttiness about performance. Even if they happen to be right , it’s a total guess.
Every program has room for performance improvement by changing certain things. Guessing does not inform you what those things are.
If two programs that do the same thing have different performance, it only means they’ve been optimized to different degrees. Your challenge is to explain this.
Java Methods, A method is a block of code which only runs when it is called. You can pass data, known as parameters, into a method. Methods are used to perform certain
Is there anything inherently wrong with long variable/method names in Java? [duplicate]
Nothing inherently wrong, it’s better to make it descriptive than cryptic. However, it’s often code-smell for a method that is trying to do too much or could be refactored
Better getAccountInformation(DateRange range)
I prefer to have long variable/method names that describe what’s going on. In your case, I think getPlayerWithMostGoals() is appropriate. It bothers me when I see a short variable name like «amt» and I have to transpose that in my head (into «amount»).
Something like getAmt() is looks like C++ code style. In java usually are used more descriptive names.
Your professor made a good understandable method. But it’s very popular word. It’s not a general case. Use your «longWordStyle» style it’s more java.
Use of underscore in variable and method names, Except for variables, all instance, class, and class constants are in mixed case with a lowercase first letter. Internal words start with
maximum length of method names
posted 18 years ago
I was wondering : is there a maximum in the length of the method names ? Or more precisely : is ther a maxiumum in the significant number of characters in a name ?
Like : getThis and getThat are both allowed, but because the JVM only looks at the first 5 characters, both names are equal.
author and iconoclast
posted 18 years ago
I think the only limitation is that the method name will be stored in the class file as a CONSTANT_Utf8_info structure, which uses an unsigned two-byte integer to represent the number of bytes in the String data. This limits the length of a method name to around 65536 characters. I’d be very impressed — or horrified — if anyone had hit this limitation in real life!
posted 18 years ago
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). Hope you can figure out why.
Mani
Quaerendo Invenietis
author and iconoclast
posted 18 years ago
I just said «around 2^16» because the limit may actually be less if any of the characters are outside of the 7-bit ASCII range, because then multiple bytes will be used per character.
posted 18 years ago
Mani
Quaerendo Invenietis
posted 18 years ago
just to be sure : although the length is almost unlimited, does the compiler/jvm use every character. It’s just that I know in C, a functionname can have almost any length, but only the first, say, 14 characters are actually used.
author and iconoclast
posted 18 years ago
That was a limitation of old-fashioned linkers, generally not a compiler or a language itself. It’s not really a problem anymore — C++’s long mangled names forced linker developers to accomodate arbitrarily long external symbols.
In any case, Java doesn’t use the platform linker to link its classes, so no such limitation applies. The answer is yes, every character counts.
author
posted 18 years ago
You probably missed that our naming policy requires everyone to use a full last name. Please adjust your display name accordingly. Thanks!
Regarding your question, I’m quite sure that Java uses the full name to differentiate between identifiers, no matter how long they are.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny — it is the light that guides your way. — Heraclitus
Longest Class, Method and Attribute Names in Java
Just got curious to see what is the longest class name in Java JDK bundle and extended that curiosity to method and attribute names as well.
Wrote a tiny Java program to load all classes, their methods and attributes from a Jar to print their name to a file. Then ran that on JDK 1.6 (rt.jar) and got the following results.
- For class name the award goes to (92 chars) InternalFrameInternalFrameTitlePaneInternalFrameTitlePane MaximizeButtonWindowNotFocusedState
- For method name the award goes to (66 chars) localizableRUNTIME_MODELER_PORTNAME_SERVICENAME _NAMESPACE_MISMATCH belonging to class ModelerMessages.
- For attribute name the award goes to (60 chars) ER_INVALID_NAMESPACE_URI_VALUE_FOR_RESULT_PREFIX _FOR_DEFAULT belonging to class XSLTErrorResources.
Hope you are following naming conventions strictly in your projects. I welcome you to run this below program on your project or your favorite packages like Spring / Hibernate etc and share your interesting findings in the comments.
This Java program will print all the class, method and attribute names from the Jar file, sorted in descending order of length.
package com.javapapers.java; import java.io.File; import java.io.PrintWriter; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.Enumeration; import java.util.List; import java.util.jar.JarEntry; import java.util.jar.JarFile; public class LongNames < public static void main(String[] args) throws Exception < String javaFile = "C:\\Program Files\\Java\\jdk1.6.0_24\\jre\\lib\\rt.jar"; ListclassList = new ArrayList(); List methodList = new ArrayList(); List attributeList = new ArrayList(); System.out.println("Processing. "); getList(new File(javaFile), classList, methodList, attributeList); sort(classList); print(classList, new PrintWriter("ClassList.txt")); System.out.println("Class list complete."); sort(methodList); print(methodList, new PrintWriter("MethodList.txt")); System.out.println("Method list complete."); sort(attributeList); print(attributeList, new PrintWriter("AttributeList.txt")); System.out.println("Attribute list complete."); > private static void getList(File file, List classList, List methodList, List attributeList) throws Exception < EnumerationjarEntries = new JarFile(file).entries(); while (jarEntries.hasMoreElements()) < JarEntry jarFile = jarEntries.nextElement(); String className = jarFile.getName(); if (className.endsWith(".class")) < className = className.replaceAll("\\.class", ""); className = className.replaceAll("/", "."); try < if (className != null && !className.isEmpty()) < Class clazz = Class.forName(className, false, ClassLoader.getSystemClassLoader()); // inner class hack if (clazz.getSimpleName().length() >0) < classList .add(new Content(clazz.getSimpleName(), "")); getMethodList(clazz, methodList); getAttributeList(clazz, attributeList); >> > catch (Exception e) < System.out.println(e); >> > > private static void getMethodList(Class clazz, List methodList) < Method[] methods = clazz.getMethods(); for (Method method : methods) < methodList .add(new Content(method.getName(), clazz.getSimpleName())); >> private static void getAttributeList(Class clazz, List attributeList) < Field[] fields = clazz.getFields(); for (Field field : fields) < attributeList.add(new Content(field.getName(), clazz .getSimpleName())); >> private static void sort(List nameList) < Collections.sort(nameList, new Comparator() < public int compare(Content c1, Content c2) < //descending return c2.content.length() - c1.content.length(); >>); > private static void print(List list, PrintWriter file) < for (Content str : list) < file.println(str.content.length() + "\t" + str.content + "\t" + str.className); >file.close(); > > class Content < public Content(String content, String className) < this.content = content; this.className = className; >String content; String className; >
Popular Articles
Comments on «Longest Class, Method and Attribute Names in Java»
According to the naming conventions, its good to follow this. But my doubt is why they have restricted to this specific length? Is there any reason behind this? Thanks,
Nagendra.
92 chars for class name,it is restricted,we can not go beyond to it.if this is restricted how did you calculate it?Is class name length dependent on JDK version?
One suggestion, your site template has very limited space for text/code. This makes difficult to scroll sidewise to understand code. I appreciate your posts and interest.
Yes Praveen, thats right. Presently, I am working menu redesign and I will fix this also along with that.
Hi Joe I tried to find in javadocs this class InternalFrameInternalFrameTitlePaneInternalFrameTitlePaneMaximizeButtonWindowNotFocusedState. I see none. Why? What is the use of this class? Regards
Jinu
It worked but got different results , I made some changes I used rt.jar which is in jre7 library as I don’t have java installed.
Thanks
Comments are closed for «Longest Class, Method and Attribute Names in Java».