travisf

travisf

Piping module attributes

I picked this pattern up from another dev and have made fairly extensive use of it with Multis:

   @multi Multi.new()
   ...
   @multi
   |> Multi.put(:params, params)
   |> Multi.run(:somefunc, &some_func/2

This ensures that pipechains start with a raw value and, I think, it looks a little cleaner. Does anybdy else do this? Are there any articles on this pattern? It’s fairly simple but there is some nuance due to the constraints of module attributes (I’ve only done it with Ecto.Multi.new/2 and raw values), I wonder if you would run into problems during compile time if you tried it with modules in your project. Of course you couldn’t do it with a function in the same module.

Most Liked

andyleclair

andyleclair

How does it ensure the pipe chain starts with a raw value?

IMO I would not do this. We have some legacy tests that use module attrs and they are a bit of a PITA to deal with and refactor.

If you want your pipe chains to start with a raw value, maybe use credo?

andyleclair

andyleclair

I have seen this attitude before in people who are just learning elixir, fussing over micro things that map to things they know from other languages.

My advice is: don’t worry about it! Write the most straightforward, obvious code you can, and go back to make it faster later if you need to. Always heed the words of Saint Joe:

“Make it work, then make it beautiful, then if you really, really have to, make it fast. 90 percent of the time, if you make it beautiful, it will already be fast. So really, just make it beautiful!”

sodapopcan

sodapopcan

Not to pile on but I’m really not a fan of things like this. This is really no different than a single-line private function that I have to jump to to be sure of what it is. It’s also creating a compile-time dependency on Ecto.Multi which isn’t a huge deal in this case but not ideal. I agree with @andyleclair that credo is the best tool for the particular outcome you’re looking for.

acangiano

acangiano

I tend to use module attributes as storage for compile-time constants, which is particularly handy for “caching” expensive operations with no runtime dependencies. I don’t think that what you’re doing is particularly bad but it feels unnecessary.

sodapopcan

sodapopcan

I like the suggestions for when I accidentally do stuff like:

|> Enum.flatten()
|> Enum.map(& &1.foo)

And credo is like, “You dummy, just use flat_map!” It’s especially nice when you upgrade Elixir and it shows you all the places you can improve with stuff in the new version.

…though I suppose this “artificial intelligence” everyone keeps talking about can also do that.

I do like the linting a lot, I just disable a bunch of checks.

Where Next?

Popular in Discussions Top

chuck
Let me start by stating an assumption: Phoenix is a great approach to building REST APIs. There are many reasons for this, but I will ass...
New
pillaiindu
I want to convert a Phoenix LiveView CRUD website to a CRUD mobile app. What do you think is the easiest way to do so?
New
ricklove
I was just introduced to Elixir and Phoenix. I was told about the 2 million websocket test that was done 2 years ago. From my research, t...
New
AlexMcConnell
The reason that Rails is as popular as it is is because it’s very easy for relatively inexperienced developers to get a lot of work done....
588 19568 166
New
sergio
There’s a new TIOBE index report that came out that shows Elixir is still not in the top 50 used languages. It also goes on to call Elix...
New
restack_oslo
Hello, Please pardon me for any faux paux. I am 46 and this is my first time on a forum of any kind. I wanted to to get answers from tho...
New
jsonify
So, is Heroku the only free option for hosting Phoenix/Elixir at this point? I’m not ready to commit to paying monthly and was wondering ...
New
tomekowal
Hey guys! I want to create a toy project that shows a chart of temperature over time and updates every 5 seconds. I feel LiveView is per...
New
joeerl
I’m playing with Elixir - It’s fun. I think @rvirding does give Elixir courses these days. Re: files and database - when I given Erlang ...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

Other popular topics Top

albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New

We're in Beta

About us Mission Statement