0

What is the logic behind this that String has .length but Collections has .size.

Saikrishna Rajaraman
  • 3,205
  • 2
  • 16
  • 29

2 Answers2

8

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
  • 92,896
  • 20
  • 260
  • 197
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

gi097
  • 7,313
  • 3
  • 27
  • 49