Hacker News new | ask | show | jobs
by zapnuk 2072 days ago
Edit: My bad didn't recognize the parent comment was about Ruby, and that the code isn't elixir by a long way.

Correct me if I'm wrong but I think there are 1 (and a half) other ways you could write it - that don't result in branching

1:

  def something(args) do
    with {:ok, obj1} <- depencency1.call(args),
         {:ok, obj2} <- dependency2.call(obj1),
         {:ok, obj3} <- dependency2.call(obj2),
         {:ok, obj4} <- dependency2.call(obj3),
         {:ok, obj5} <- dependency2.call(obj4)
    do
      {:ok, obj5}
    else
      {:error, msg} -> {:error, msg}
    end
  end

1.5: Use a try/rescue block where you match {:ok, obj} and then catch Match errors.
3 comments

I think your parent was writing Ruby, not Elixir. (Unless this is Ruby's new pattern-matching syntax that I'm not familiar with.)
Indeed if you look at my ruby code, it tries to be a with statement ;)
The parent is referring to rubocop, so I think they're talking about Ruby.

The Elixir formatter sounds much less strict, and I have very little problem with it.