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.

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?

3 Likes

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.

1 Like

This is a complete no-go, module attributes basically get replaced with their value at compile time which also means that you might get 2 different Multi.new values (though I have not tested this so it’s likely not true).

But as the others said there’s zero value in this. Multi.new is not an expensive operation either so your reason to try to do this is quite puzzling. What’s your rationale? “Looks little cleaner” is, I am sure we all agree, a very subjective statement, and even if we agree that it’s kinda sorta objective there are still degrees; you are gaining next to nothing by doing this replacement with a module attribute.

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!”

2 Likes

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.

1 Like

From the responses; I will stop doing it. The one thing I’m a little confused about is when you say “If you want your pipe chains to start with a raw value, maybe use credo?” I’m not sure exactly what you mean?

I actually got into this habit because of credo, although I can see that it’s now off by default but I think at one time the codebase I was throwing PiepChainStart warnings.

https://hexdocs.pm/credo/Credo.Check.Refactor.PipeChainStart.html

I’d be surprised if starting a chain with Multi.new() triggers that Credo check - there is specific code to allow that shape when there are no args:

As a guy working with Elixir for 8 years now, I am confused as well. :003:

I was confused by “make sure it starts with a raw value” and how this pattern would solve it. I probably should have just asked but instead I ass-u-me’d you meant to ensure you use Ecto.Multi.new() over %Ecto.Multi{}. Credo in this case could just make sure that the latter isn’t used anywhere other than on the left hand side of a pattern match (or guard, I suppose).

Sorry, what I meant was “use a credo rule to enforce this”

It has been a while since I religiously used Credo but I have to say that if I understand that rule correctly then I disagree with it. If for no other reason than this thread exactly: it’s completely valid to start a pipe with a value that’s generated by a function i.e. Ecto.Multi.new().

We don’t use that rule at work (we cp from project to project maintain a custom set of credo rules), it’s one of the most annoying rules. For my personal projects, I don’t use Credo at all!

Yeah same, I use Credo as an advisor a few times and then just drop it. It’s been helpful but not by a lot.

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.

1 Like

I should have probably said this because this is what I ended up doing in my last 3 projects.

I believe there is an option to allow arity-0 functions with this rule!