There isn’t a ton of difference when you’re just using
from, although if you’re using
|> where a lot the alias can feel verbose.
To your more general question though: Imports make sense when you want a DSL, and are particularly safe when that DSL is unlikely to be unambiguous. Aliases make sense for most everything else. To pick an example from my code base:
|> where(location: ^location)
|> join(:inner, [si], s in assoc(si, :sensor), as: :sensor)
|> where([sensor: s], s.service_token_id == ^token.id)
|> preload([sensor: s], sensor: s)
If you only
aliased query you’d have:
|> Query.where(location: ^location)
|> Query.join(:inner, [si], s in assoc(si, :sensor), as: :sensor)
|> Query.where([sensor: s], s.service_token_id == ^token.id)
|> Query.preload([sensor: s], sensor: s)
To me, this impairs readability a fair bit because to see what’s actually going on you need to skip over the
Query part of each line, cause it isn’t really telling you anything useful.
I do agree that
import should be used sparingly. I did a quick grep through my code base and 99% of all imports are for either
Ecto.Changeset. I think these end up being the most common ones to
import because, in the context in which they are imported, ambiguity is non existent and there are frequently a TON of calls to functions in those modules all right next to each other, and without the import it would obscure what’s actually going on.