3 ms·
Agree with many things, big disagree with not using 'else' with 'with' and actually for the same reason you stated not to use it. I use 'with' when I have a pip
by Grimm1 5y ago
Agree with many things, big disagree with not using 'else' with 'with' and actually for the same reason you stated not to use it. I use 'with' when I have a pipeline, but the error conditions in the pipeline matter. In fact the example you gave in the article is exactly one of the cases where I think 'with' and 'else' are useful because you can clearly show the dependency of each action in the pipeline and clear failure cases to address them.
I think there is a limit to how many things you should handle in a with statement, but the same goes for regular pipes as well.
- keathley 5y agoMy general feeling is that if the errors matter, `with` probably isn't the right construct. In those situations I prefer to use case. That's been helpful in my experience.
- devoutsalsa 5y agoIf the errors don't matter & there's no real chance of returning an error, why use &with/1 at all? &with/1 has the quirky behavior in that the return value of anything that doesn't pattern match get returned. So if &call_service/1 or &parse_response/1 doesn't return an an ok tuple, you're increasing your debugging surface by having to track down where the unexpected result came from. Good luck tracking down that rogue nil! Instead of this... with {:ok, response} <- call_service(data), {:ok, decoded} <- parse_response(response) do decoded end Maybe just do this... {:ok, response} = call_service(data) {:ok, decoded} = parse_response(response) decoded Or even this... data |> call_service!() # don't bother returning ok tuples |> parse_response!() # just use bang methods!