2

Is there a rule of thumb when you are coding in Java that easily you know which one to use (Size or Length)?

One can memorize which data structure has what method, but is there an easier way of doing it?

For example if you define Array of int but you need to use length for ArrayList.

(My rule of thumb is to use length for Arrays and Size for data structures like collection and arrayList)

sheidaei
  • 9,842
  • 20
  • 63
  • 86
  • 2
    An IDE like IntelliJ will help you sort that out. If not, use javadocs. Don't memorize such trivia. – duffymo Feb 11 '13 at 18:22
  • The rule of thumb is enough. Just look up the API if necessary. – nhahtdh Feb 11 '13 at 18:22
  • My rule of thumb is just to use the same one for everything. Actually length is more difficult to apply to collections which are not intuitively contiguous like a `TreeSet` or an `HashSet` but there was really no need to have two different names according to the kind of collection. – Jack Feb 11 '13 at 18:22
  • @Jack, how's that working out for you? :P – Colleen Feb 11 '13 at 18:23
  • 1
    Aren't `String`s and arrays about the only ones that are `length` (at least in the standard API)? – Bernhard Barker Feb 11 '13 at 18:23
  • I understand that I can use an IDE to help me with that, but I wanted to see what is the rationale behind it, to name one Size and one Length, if one understand that rational, can remember that easier. – sheidaei Feb 11 '13 at 18:23
  • @Colleen: I mean when I have to provide my own `size()` or `length()` method. When using the JDK I have to stick to their choices. – Jack Feb 11 '13 at 18:24
  • 1
    This will help : http://stackoverflow.com/questions/2203425/is-there-a-technical-difference-between-the-terms-length-and-size-in-progra – Arpit Feb 11 '13 at 18:24
  • @Dukeling that's about it? (I don't know anything else using length.) – sheidaei Feb 11 '13 at 18:25
  • @sheidaei I'm not 100% sure because *I don't try to memorise them* (and I don't use all the classes in the standard API), but I think so. – Bernhard Barker Feb 11 '13 at 18:28
  • @Dukeling I don't want to memorize them either per say, main goal is to understand the logic behind it. Then when you know the logic, you always know which one is which. – sheidaei Feb 13 '13 at 16:15

3 Answers3

5

The rule of thumb is to use the field or method provided to you by the API. That's about it.

If you have an IDE like Eclipse, you can type the name of your list, hit the . key, and wait for a list of public fields or accessors to appear so that you can scroll through and browse for the right one. This is easier than memorization (though the memorization would likely serve you better in the long run).

  • primitive arrays use length
  • Strings (being CharSequences) use length()
  • and almost everything else uses size()

(Small addendum: the size() function of Collections gives you only the number of populated fields, not the actual number of fields allocated to the Collection, which doubles each time its current allotment is exceeded.)

asteri
  • 11,402
  • 13
  • 60
  • 84
  • I understand that I can use an API to help me with that, but I wanted to see what is the rationale behind it, to name one Size and one Length, if one understand that rational, can remember that easier. – sheidaei Feb 11 '13 at 18:24
  • "almost everything else" uses size() => is there any documentation that can refer to it and make this everything else rather than almost? I think everything else use Size(), but I am not sure. – sheidaei Feb 11 '13 at 18:27
  • There is no rationale, it just happened. Yes, it's bad design. – starblue Feb 11 '13 at 18:29
  • @sheidaei I would like to say "everything else uses size", but I attempt to refrain from making universal statements. Because inevitably one counterexample will come out of the woodwork and make my statement false, even if that counterexample is from the dark, unused, and unseen recesses of programming. – asteri Feb 11 '13 at 18:30
  • @sheidaei read that ques: http://stackoverflow.com/questions/2203425/is-there-a-technical-difference-between-the-terms-length-and-size-in-progra It will clear most of your doubts. – Arpit Feb 11 '13 at 18:30
  • @Jeff that's what I am trying to find out. Thanks for your contribution though. – sheidaei Feb 13 '13 at 16:03
0

I would refer you to this related S.O. post:

count vs length vs size in a collection

It suggests that the convention is based on idioms and nuances of language and what is intuitive.

Community
  • 1
  • 1
gcek2
  • 106
  • 3
  • Is this about Java or is it talking in general? collection is using Size as well not count in Java. Am I missing something? – sheidaei Feb 11 '13 at 18:30
0

Length is useful for continuas things. like list and arrays.

Size : it give the size which may or may not be equal to length.

More here :Is there a technical difference between the terms "length" and "size" (in programming, of course)?

Community
  • 1
  • 1
Arpit
  • 12,767
  • 3
  • 27
  • 40