AstonJ

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 :003:

Most Liked

AstonJ

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 :lol:

15
Post #2
kelvinst

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.

10
Post #5
mrkjlchvz

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.

kelvinst

kelvinst

Well, I’ve some:

  1. Understanding some key concepts of Elixir, which @josevalim nailed explaining them on his keynote for last ElixirConf EU. For those who like to go straight to the point, is the “What is Elixir? (ElixirConf version)” part I’m talking about. What I mean is that, once you understand the concepts he talks about, you will understand how Elixir is made to work, and consequently do better code for Elixir.

  2. Use railway oriented development, and Kernel.SpecialForms.with/1 is a great tool for it. Pipe operators are great when you have to make a simple job in many steps, but when you have to handle errors on any of these steps, with fits better.

  3. Another silly thing I do is to avoid var attributions on my functions, at all. I know, maybe a lot silly, but conceptually it’s very tight with what @AstonJ said. No attributions help me to think when I can extract another function to do something that I’m saving in a variable to use after. Of course, that’s not something to do in every single case, sometimes it’s a good idea to save calculated values which will be used a lot later. But most times, you could do this:

    def foo(map) do
      bar = Map.fetch(map, :bar)
      "ID: #{bar.id}, NAME: #{bar.name}"
    end
    

    Without attributions:

    def foo(map) do
      map
      |> Map.fetch(:bar)
      |> do_foo()
    end
    
    def do_foo(bar) do
      "ID: #{bar.id}, NAME: #{bar.name}"
    end
    

    I know, silly example, but on complex cases it can help you a lot. Hope you got what I mean.

josevalim

josevalim

Creator of Elixir

To clarify, both alias and import are fast to compile and both do not affect the runtime performance. The issue is that import implies a compile time dependency, aliases do not. When you do import Foo, Foo needs to be available at moment and the compiler will block until such happens.

Where Next?

Popular in Guides/Tuts Top

Morzaram
Hey guys I’ve made a guide on how to connect Quill to Phoenix. For the sake of formatting it might be easier to view it on my Notion Doc...
New
sergio
https://sergiotapia.me/generate-images-with-name-initials-using-elixir-and-imagemagick-374eca4d14ff Hope this saves you guys some time!...
New
9mm
So I’m really loving elixir. BY FAR the most excruciating piece of learning a functional language for me is having to “transform” all my ...
New
zenw0lf
Hello all! For those wanting to try your hands at Elixir / Phoenix, I wrote a comprehensive tutorial on doing a simple JSON API with sai...
New
benwilson512
Correct if me I’m wrong, as best I can tell there aren’t any reasons to use mix run --no-halt in production vs releases. The marginal val...
New
dogweather
I just finished a long process of configuring and debugging a Docker Compose-based dev environment. Several late-night hours! I think I’v...
New
Eiji
Hey, today I give amnesia library a try and found a few problems. I would like describe how to setup it properly and solve problems which...
New
zookzook
If you want to use the MongoDB in your next Killer-App-Project, but you did not dare ask because otherwise many would advise you to use P...
New
nietaki
Just a quick heads up: There seems to be a bug in Erlang/OTP 21.3, which can cause some errors when making http requests. If you’re using...
New
mudasobwa
The post covering how to generate nifty types to use in @spec in compile time with macros. https://rocket-science.ru/hacking/2020/07/15/...
New

Other popular topics Top

yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
josevalim
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

We're in Beta

About us Mission Statement