slouchpie

slouchpie

Is it "better" to crash when missing preloads or to always ensure preloads assocs?

Greetings comrades

We are having a friendly discussion at work about whether it is better to always preload or to let it crash.

For illumination, consider these 2 variations of function which lives in some Chats module:

Option 1

def is_safe?(chat) do
  Enum.all?(chat.messages, &safe_message?/1)
end

Option 2

def is_safe?(chat) do
  chat = Repo.preload(chat, :messages)
  Enum.all?(chat.messages, &safe_message?/1)
end

Some people prefer option 1 because it avoids a situation where we might be calling Repo.preload without realizing it. They believe the function should simply crash if preloads are missing, putting the onus on the developer to ensure all necessary preloads are present. The stipulation here is that the developer must write tests to ensure actual user flows that call this function are preloading before calling it.

Other people prefer option 2 because it avoids crashing. Crashing LiveViews can result in a crash loop which hammers the database.

Personally I prefer option 1 but I think the choice is quite subjective.

I am simply asking for opinions. I do not expect a “one true way” answer or overall consensus. Scribble your baseless reckonings with complete abandon.

Most Liked

al2o3cr

al2o3cr

One hazard with option 2 is that if somebody decides to call is_safe? for each chat in a list, you’re right back in N+1 query trouble.

A solution I’ve used in production was to have a helper called ensure_preload that behaves differently in production vs test:

  • in test, it crashes with a detailed message about what’s missing if the preloaded data isn’t there
  • in production, it does the requested preload and logs a message (and/or sends a notification to your exception tracker)

That way production is never broken (just slow) but code that’s covered by tests is verified to work efficiently.

19
Post #7
sodapopcan

sodapopcan

Not a direct answer but to me there is nothing subjective about having side effects in a predicate function. If I’m just asking a question I don’t expect anything to happen, ever.

katafrakt

katafrakt

I think this is a great question and we should have more discussion of this kind in Elixir community.

In my opinion the question could be rephrased as: are you ok with this function having a dependency on the Repo, that is a low-level persistence concern? Does it fit this place in you application?

In general, I think Ecto.Repo tends to leak all over the application, because we are passing around Ecto schemas, on which it’s possible to call functions like Repo.preload. I sometimes wish it was not called Ecto.Repo and would make way to have proper repositories in the app that return structs that are completely disconnected from the persistence concerns. But this is just a digression.

I like the heuristic that predicate function should not generally have side effects. Because a database call definitely is a side effects (in terms of functional programming, pure functions etc.), even if not a very “dangerous” one. That being said, there might be case in which having side effects for such functions is ok. I’m just not sure how this is in your case.

Where Next?

Popular in Discussions Top

andre1sk
A big advantage to Elixir is all the distributed goodness but for many applications running on multiple nodes having integrated Etcd, Zoo...
New
blackode
Elixir Upgrading is so Simple in Ubuntu and It worked for me Ubuntu 16.04 git clone https://github.com/elixir-lang/elixir.git cd elixir...
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31265 143
New
AstonJ
Are there any Elixir or Erlang libraries that help with this? I’ve been thinking how streaming services like twitch have exploded recentl...
New
Crowdhailer
I’ve been hearing much about the new formatter and it’s something I have been keen to try. I find examples buy far the most illuminating...
248 19327 150
New
mbenatti
Following https://github.com/tbrand/which_is_the_fastest |> https://raw.githubusercontent.com/tbrand/which_is_the_fastest/master/imgs...
New
RudManusachi
What configs will make sense to put to runtime.exs? – A bit of how I configure apps: I have generic configs in config/config.exs, dev...
New
ben-pr-p
In general I’ve been sticking to this community style guide GitHub - christopheradams/elixir_style_guide: A community driven style guide ...
New
Qqwy
Looking at the stacks that existing large companies have used, WhatsApp internally uses Mnesia to store the messages, while Discord uses ...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

Other popular topics Top

danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29603 241
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement