Its very easy to refactor nested code like this in elixir. if/else can be easily pattern matched
defp check_operation(operation) do
if operation.done do
if operation.error do
{:error, "Something went wrong in GCP"}
else
{:ok, "Project creation successful"}
end
else
{:error, :retry}
end
end
Each and every if else can be transformed into a separate function call. So, above code can be written as:
defp check_operation(%{done: true} = operation) do
if operation.error do
{:error, "Something went wrong in GCP"}
else
{:ok, "Project creation successful"}
end
end
defp check_operation(operation) do
{:error, :retry}
end
This can be further broken down into this:
defp check_operation(%{done: true, error: true}) do
{:error, "Something went wrong in GCP"}
end
defp check_operation(%{done: true} do
{:ok, "Project creation successful"}
end
defp check_operation(_operation) do
{:error, :retry}
end
another code golf 😄