Rukenshia
I am used to early returns using the “return” keyword many languages provide.
As I am currently trying to learn elixir, I found it really hard to get a proper structure in my code and avoid if…else blocks which I have to nest very deep.
As an example, I wrote a small bot for Discord (don’t do it by the way, the discord_ex library doesn’t seem to work properly) and have to check several things when I receive a message.
As an example, here is one handler.
defp handle_command({ "msg", "remove-trigger", cmd }, { payload, state }) do
if payload.data["author"]["id"] == 1234 do
shortname = String.split(cmd, " ") |> List.first
if !Messages.has shortname do
send_message "unknown Message", { payload, state }
else
info = Messages.get(shortname)
trigger = String.replace_leading(cmd, "#{shortname} ", "")
if Enum.member?(info[:trigger], trigger) do
Messages.remove_trigger shortname, trigger
save_messages
end
send_message "trigger #{trigger} removed from #{shortname}", {payload, state}
end
end
end
I am pretty sure that this can be way improved - what’s the proper design pattern for that in Elixir, how do I escape the if-block hell?
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 5- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
dardub
I’m new to elixir as well and still figuring this out. But one thing you can do is break this up into more functions and take advantage of pattern matching.
For instance you can get rid of the first if statement by pattern matching the handle_command function:
defp handle_command({ "msg", "remove-trigger", cmd }, { %{data: %{"author" => %{"id" => 1234}}}, state }) doibgib
In addition to the pattern matching that @dardub mentioned (
), I might change the structure of the
Messagesmodule’s functions to a tagged format so you aren’t using theif..thenfor control flow.So your
Messages.get/1would return either{:ok, info}or{:error, :not_found}(or more generally{:error, reason}(EDIT: And you wouldn’t even use theif has/1in your example). To me, this would make it more of an expression approach using acaseorwithstatement with a more understandable “error” workflow.atimberlake
You can pattern match the handle_command function to drop the first if
For the second
ifI would drop the call toMessage.has/1(assumingMessage.get/1returnsnilfor no message)Then have a function deal with the presence or lack of message
Keep going by creating functions that handle each situation and try to come up with names that reveal the intent of each step (
)
do_send_messageis a terrible function name but because naming is hard, you get to solve thatjosevalim
To add to this, at some point the community, even Elixir codebase, settled on
do_send_messageas a convention but today we see it as a bad practice. We are trying our best to not usedo_prefixes in Elixir and use proper function names for them, even if it ends up being the original name with extra information, for example,check_shortname_and_send_message.Rukenshia
Thanks a lot guys, this really helped me. It’s really easy for me to forget how powerful the pattern matching just is. I know Rust’s pattern matching which is pretty cool already, but this is way more powerful.