mvysny / vok-orm

Mapping rows from a SQL database to POJOs in its simplest form
MIT License
21 stars 4 forks source link

Notation is Confusing #1

Closed NiranjanShah closed 6 years ago

NiranjanShah commented 6 years ago

In order to get a table row based on the ID normally the convention is to pass as a parameter and the square brackets are used with arrayList wherein you need to get the value at that index.

i.e Category[25] is confusing as it seems that it will give the 25 the index from the ArrayList, where as actually is the WHERE clause of sql (WHERE id = 25).

mvysny commented 6 years ago

Thanks! The original intent was as follows: since the category database table is a collection of rows (every row representing a category), the Category class should follow this principle and appear as a collection of Category instances as well. Calling Category[25] would thus fetch the category with ID 25. In this regard, it would be identical to calling Category.getById(25) which would just run select * from category where id=:id limit 1.

However, there are the following disadvantages:

  1. It may be perceived that when calling Category[25], we want the 25th Category from a list of categories, and not the Category with ID of 25.
  2. It may be perceived that at first the full list of Categories is built (therefore loading the full content of the select * from category into the memory), and then the 25th item would be returned.
  3. It is expected that the [] operator runs in O(1) and returns the result very quickly. That's definitely not the case when running a select
  4. Navigating to the implementation of the [] operator is quite hard since it's hard to ctrl+click in Intellij IDEA on a single character.
  5. The [] operator is not automatically imported in Intellij. Even though it's a bug in Kotlin plugin, it still hinders the usability of vok-orm

I will therefore deprecate both the [] operator and the get method, and I'll introduce getById() function which clearly states its intent.

mvysny commented 6 years ago

Fixed in vok-orm 0.2