Here are two versions of code I wrote, they both work.
I’d like insight from you guys as to : what is best style between the two, and maybe what would be an even better style ?
I find the version with “with” a bit contrived with all those temprary tuples and atoms I had to create.
First version
world =
case Msim.Simulator.get_world_db(world_id, true) do
{:ok, res} -> res
{:error, _error} -> nil
end
legit =
case world do
nil -> false
_ -> check_path_to_module(__MODULE__, world.type.front_module)
end
{:ok,
socket
|> assign(
msim_auth: msim_auth,
request_path: request_path,
# specific data after
world_id: world_id,
world: world,
legit_path: legit,
banks: nil,
current_user: Msim.Account.fetch_user_from_db(user_id)
)}
Second version
{world, legit} =
with {:get, {:ok, world}} <- {:get, Msim.Simulator.get_world_db(world_id, true)},
{:legit, world, true} <-
{:legit, world, check_path_to_module(__MODULE__, world.type.front_module)} do
{world, true}
else
{:get, _} -> {nil, false}
{:legit, world, _} -> {world, false}
end
{:ok,
socket
|> assign(
msim_auth: msim_auth,
request_path: request_path,
# specific data after
world_id: world_id,
world: world,
legit_path: legit,
banks: nil,
current_user: Msim.Account.fetch_user_from_db(user_id)
)}