What is the logic behind this that String
has .length
but Collections
has .size
.
Asked
Active
Viewed 2,161 times
0
Saikrishna Rajaraman
- 2,621
- 2
- 11
- 26
2 Answers
7
The main reason is to minimize confusion when migrating from Java, which has the same method names. In pre-1.0 versions of Kotlin, we tried to make the method names the same, bu this tripped people up constantly (because both of those methods are used extremely often).
yole
- 80,603
- 15
- 224
- 177
1
Well, those are just naming conventions, which are basically just a taste of the creator of the programming language.
A String
tends to refer to contiguous elements, so we can say that a string has a length
. Naming it count
will be weird right?
When we have a collection, we usually iterate it or want to know the amount of items inside it. So, I want to know the size of that collection.
You can read a lot more here about naming conventions: count vs length vs size in a collection
Giovanni
- 6,055
- 2
- 23
- 44