FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Suggestion of smell: complex else clauses in with · Issue #7 · lucasvegi/Elixir-Code-Smells · GitHub

Suggestion of smell: complex else clauses in with #7

Description

It is a small to match on different else results in with:

with {:ok, encoded} <- File.read(...),
     {:ok, value} <- Base.decode64(encoded) do
  value
else
  {:error, _} -> :badfile
  :error -> :badencoding
end

That's because it is impossible to know from which clause the error value came from. Instead, you should normalize the return types in the clauses:

with {:ok, encoded} <- file_read(...),
     {:ok, value} <- base_decode64(encoded) do
  value
end

def file_read(file) do
  case File.read(file) do
    {:ok, contents} ->{:ok, contents}
    {:error, _} -> :badfile
  end
end

def base_decode64(contents) do
  case Base.decode64(contents) do
    {:ok, contents} ->{:ok, contents}
    :error -> :badencoding
  end
end

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    suggestion of smellElixir-specific smells suggested by the community

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions


      Back | FazBrowse Home | New Git URL