1

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?

Martin Schröder
  • 354
  • 1
  • 4
  • 19
B. Alpízar
  • 27
  • 1
  • 3
  • 5
    The length that provides the most readability and clarity of purpose. – Robert Harvey Aug 06 '16 at 05:36
  • 12
    Exactly 27 characters. –  Aug 06 '16 at 05:41
  • @Snowman do you have a padding strategy? or do you always come up with exact 27 character meaningful names :) – Chip Aug 06 '16 at 06:11
  • 1
    @Chip That's why programming takes so long. It is pretty difficult to find a meaningful name for any variable when the requirement is the length must be exactly 27 characters. But oh boy is it worth it. – Andy Aug 06 '16 at 06:58
  • 5
    As short as possible. As long as necessary. – CodesInChaos Aug 06 '16 at 09:08
  • Never mind you lose all the benefits of having the variable name being exactly 27 characters if the length is not exactly 27!! – Zavior Aug 06 '16 at 13:38
  • 1
    Possible duplicate of [Is there an excuse for short variable names?](http://programmers.stackexchange.com/questions/176582/is-there-an-excuse-for-short-variable-names) – Martin Schröder Aug 06 '16 at 13:51
  • If you can't come up with a good name, inline the variable. This only works if the variable is used in one place of course – michaelsnowden Aug 06 '16 at 16:38

2 Answers2

5

The comments to the original question have the best answers:

  • The length that provides the most readability and clarity of purpose. – Robert Harvey
  • As short as possible. As long as necessary. – CodesInChaos
Bryan Oakley
  • 25,192
  • 5
  • 64
  • 89
0

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:

  1. 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
  2. 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.
  3. 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.
  4. 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.
Iker
  • 865
  • 1
  • 6
  • 11
  • It is ok for coding conventions, what I'd like to know is what happens in the JVM. I mean, is there a high level cost for declaring long variables names? – B. Alpízar Aug 08 '16 at 01:06