3 ms·
> Complex else clauses in with This has proven controversial, but sometimes the Else behavior is very sensitive to which of the With expressions failed (especi
by Terr_ 2y ago
> Complex else clauses in with
This has proven controversial, but sometimes the Else behavior is very sensitive to which of the With expressions failed (especially for better logging) and I maintain that the least terrible approach involves augmenting the With clauses using atoms to identify each expression when it fails, ex:
For example:
with {_, {:ok, key} } <- {:phase_key, get_key() },
{_, {:ok, door}} <- {:phase_unlock, unlock(door) },
{_, %Thingy{} } <- {:phase_thingy, fetch_thingy(door)},
{_, %Widget{} } <- {:phase_widget, fetch_widget(door)} do
else
{:phase_key, {:error, _msg} = err} -> # I can tell the key was missing, rather than the lock broke
{:phase_unlock, {:error, _msg} = err} -> # I can tell the lock broke, rather than missing a key
{:phase_thingy, nil } -> # I can tell this nil value is a missing thingy, not a widget
{:phase_widget, nil } -> # I can tell this nil value is a missing widget, not a thingy
end
As a big bonus, when something crazy-unexpected is emitted to cause a WithClauseError--like "hello world"--you have a much better chance of quickly diagnosing what happened from log messages, instead of inspecting a dozen different logical branching or trying to blindly repro it.
Pre-buttals:
"Just rewrite those 4 functions to return better somehow" -> No, that's not always practical or even possible. In the above example, the signatures of some of these functions are already perfectly fine on their own. The ambiguity comes when they get used near others in different ways, and trying to assign globally unique features to match on is a bad idea.
"Create 4 private functions to wrap things" -> Ugh, now the person reading the code has an even harder time, since they need to refer to functions elsewhere in the file, any of which might be hiding unexpected bonus behavior. Those 4 private functions might not even be useful in other with-statements in the same module, causing you to make even more private functions... and all of it still falls apart the moment your Else logging/cleanup needs to record or revert something from a prior (successful) step.
- conradfr 2y agoI think with is ok and actually kind of elegant if you don't care too much about the actual error. To be honest I would prefer a return keyword and using early returns as in practice I'm not a fan of "the last statement is the return".
- sph 2y agoWhen using `with` and the `{:ok, term} | :error` patterns in Elixir, I just wish we had first class monads. There are some third-party libraries to make working with these a little easier, but it never sat right with me. Probably being so close to the BEAM means we cannot stray very far from what Erlang is doing, but errors would be much nicer to handle with monads. That said, pattern matching alleviates a lot of pain in many cases. If you have a pipeline of fallible steps, you can do: def step1({:ok, data}), do: ... def step1(:error), do: :error def step2({:ok, data), do: ... def step2(:error), do: :error etc. I think the `with/else` anti-pattern is that your example just feels clunky because of this lack of monads. It is an anti-pattern not because it's a bad idea, but because it is simply unergonomic and just looks bad. In general, whenever I feel the need to differentiate errors to handle them separately like your example, with a little refactor I can usually simplify the `with` expression and avoid having to do all that ceremony.
- Fire-Dragon-DoL 2y agoGleam has an interesting approach with the USE statement. It looks better than with, but I haven't tried to write code with it yet
- Yoric 2y agoI find your snippet surprisingly readable. Thanks for the suggestion!
- Fire-Dragon-DoL 2y agoOh yeah, the with statement was the ugliest part of elixir and also required unfortunately