And I cringed. Not because I knew it was wrong (it wasn't). But because using ".first" feels like a code smell. (I should note that this isn't specific to the particular syntax. Seeing product.price would be equally cringe-inducing.)
So is the use of .first (or equivalent) a code smell?
- "first" is arbitrary and not even necessarily in a consistent order. That means it's not deterministic, which is frequently a problem. If you really want random, then wouldn't it be better to use a randomizer explicitly?
- first means you're working with a collection, and it's inefficient to get the whole collection if you only want one.
- if you have a collection in which every element is equivalent in some way, then getting information off the first one (or any random one) is just fine. You may later need it as a collection for something else.
- some languages have quirks where if you want one then you get a collection of one element.
I'm still not completely sure, but my current thinking is that .first is pretty much always a bad idea except in cases where the language makes you. What do ya'll think?