Should I use `Code.ensure_loaded?/1`?

I saw a commit in Ecto: https://github.com/elixir-ecto/ecto/commit/46674c3d31f55480e8cedba1e1d2e651cf2ae941

Which seems to avoid using Code.ensure_loaded?/1, I had used this function to check the module availability. So should I use try instead?

1 Like

I think that’s just because the function in question is potentially called often, and the check happens at runtime. ensure_loaded requires a synchronous call to the code server so it’s rather slow. I would still prefer it in code that’s not too performance-critical.

1 Like

Ensure_loaded is great if you can get away with doing it only once, like at code-gen time. ^.^

2 Likes