Muhammad
How can I refactor this elixir code
I would like to refactor the following code.
The first thing that comes to my mind is to extract the case block into a new method,
but maybe there is a better solution?
def start_conversation(chat_id, user_id, participant) do
{first_user_id, second_user_id} =
case participant.gender do
"female" -> {participant.user_id, user_id}
"male" -> {user_id, participant.user_id}
end
params = %{
chat_id: chat_id,
start_time: DateTime.utc_now,
first_user_id: first_user_id,
second_user_id: second_user_id
}
Db.Conversation.create(params)
end
Marked As Solved
yurko
I’d split it into few more specific functions so that each of them does less and has less variables, something like this:
def start_conversation(chat_id, user_id, %{gender: "female", user_id: participant_user_id}) do
create_conversation(chat_id, participant_user_id, user_id)
end
def start_conversation(chat_id, user_id, %{gender: "male", user_id: participant_user_id}) do
create_conversation(chat_id, user_id, participant_user_id)
end
defp create_conversation(chat_id, first_user_id, second_user_id) do
Db.Conversation.create(%{
chat_id: chat_id,
start_time: DateTime.utc_now,
first_user_id: first_user_id,
second_user_id: second_user_id
})
end
4
Also Liked
dimitarvp
Extracting the case statement to a function would also mandate that function have 2 parameters: participant and user_id. And since your case statement is only 5 lines long I don’t think the extraction would make the code better in any way.
I think the code is fine. Do always run mix format though, I’d write DateTime.utc_now() and not DateTime.utc_now – always put parentheses even when calling functions without an argument.
2
Popular in Questions
Good day to you all.
I have been struggling to get a query involving like and ilike to work.
Can anyone assist me on this, please?
pro...
New
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set?
Thanks.
New
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: )
Hello all, this is ...
New
In AR this is so simple
@articles = current_user.articles
How to do in Ecto?
def index(conn, _params) do
current_user = conn.assig...
New
I have a super simple question about elixir - how would I take a file like this
foo bar baz
and output a new file that enumerates th...
New
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
Other popular topics
Hello, I get Persian date from my client and convert it to normal calendar like this:
def jalali_string_to_miladi_english_number(persi...
New
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1]
15:22:35.803 [error] gen_event {lager_file_backend...
New
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
What is the idiomatic way of matching for not nil in Elixir?
E.g.,
First way:
defp halt_if_not_signed_in(conn, signed_in_account) when...
New
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
Hi all,
Trying to get some more clarity over utc_datetime and naive_datetime for Ecto:
https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
I have an umbrella app.
Some of the apps inside depend on other apps in the umbrella, unsurprisingly.
I'm writing a test for one of t...
New
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
Hi everyone,
One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New







