I try to find out not according to semantics but weight. I mean, should I take care when set a long name to a variable?
closed as primarily opinion-based by user22815, Bart van Ingen Schenau, David Hammen, Thomas Junk, Nicol Bolas Aug 6 '16 at 20:14
Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.
There's no set length which can be said to be the ideal length for a variable name (in Java or in any other language, pretty much). There are, however guidelines you can use when deciding:
- The most important thing about the variable name is that it clearly shows the purpose of the variable, and that it is clearly readable, as mentioned by @RobertHarvey. Remember that you're not writing read-only code, and that some poor sod is going to have to go through that code in a few months' time in order to do some maintenance. If you're unlucky, the poor sod in question might even be you :P
- It's mostly included in point 1, but avoid ambiguous names, or names that are too similar to other variable/class/function names already in use.
- Follow language conventions. In Java, that generally means naming variables in camelCase. In some cases (not sure about Java) that might mean that naming a loop index i is perfectly acceptable, for example, despite the fact that it's not all that informative a name.
- All else being the same, prefer shorter names, but only as long as it doesn't impact readability. If you find yourself trying to contort variable names in order to shave a character or two off, please stop and give a bit of thought to said poor sod who's gonna have to read the code in a few months' time.