AstonJ
Your Elixir Tips Thread
This blog post hit my timeline earlier, and I’ve also been learning about some fantastic Elixir related tips via @pragdave’s new online course, his book, and @sasajuric’s EIA - so thought it would be cool to start this thread to see what tips you might have yourself ![]()
Most Liked
AstonJ
One of my favourites so far is making functions do only one thing (or one transformation) - so if possible, avoid if/elses/conditionals and go for multiple def clauses instead. This makes your code more readable and maintainable/refactorable and just overall less brittle. This is definitely one of the answers to my “surely there has to be a better way of doing things” when developing using a different programming language ![]()
kelvinst
Oh, one more thing! Apply the “let it fail” philosophy wherever you can!
For example, instead of:
case foo(bar) do
{:ok, bar} -> {:ok, bar.foo}
resp -> resp
end
Do no match all clause, and let it fail for unknown scenarios:
case foo(bar) do
{:ok, bar} -> {:ok, bar.foo}
{:error, resp} -> {:error, resp}
end
Again, silly example, but what I mean is: exceptions like CaseClauseError are better errors to get than unexpected values escaping out of your own code, believe me.
mrkjlchvz
Probably one of the best tip I’ve heard was always use pipes wherever possible. Do not overdo it but make sure to use it in situations that will make your code easier to understand.
EDIT: Also, it is beneficial to organize your files by responsibilities. For instance, I will create separate modules for the main implementation and GenServer capabilities.
Popular in Guides/Tuts
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance










