gperales

gperales

Credo check for encouraging pattern matching by struct in the parameters

Hi folks!

With the new release of Elixir 1.18, I would like to enforce pattern-matching by struct in our projects to take advantage of the type inference. To do so, I’m working on a Credo check and I would like feedback.

The rule will do the following:

:stop_sign: Pattern-matching by an atom key of a map:

# not preferred
def get_role(%{role: role}) do # if the map keys are atoms it should be a Struct.
  role
end

:white_check_mark: Pattern-matching by struct

# preferred
def get_role(%User{role: role}) do
  role
end
# preferred
def get_role(user = %User{}) do
  user.role
end

I’ve found that there are cases where there are maps with atom keys and they don’t have to be structs, like in the props of the LiveView Components, or the Phoenix Controllers, but I think it is worth having this check and disable it for those cases, or cheat the rule moving the pattern matching inside the body of the function like for this case:

def update(%{param_1: param_1, param_2: param_2}, socket) do
  ...
end

doing:

def update(params, socket) do
  %{param_1: param_1, param_2: param_2} = params
  ...
end

Do you think it would make sense? Would you find it useful enough to add it to Credo?

Most Liked

sodapopcan

sodapopcan

Hey, welcome to the forum!

It can be contentious but I often destructure params inside the function body, preferring to only match what’s necessary in the function head (within reason). It’s even mention in the anti-pattern guide. That is just to say it’s hard to handle that case as it’s often legit. In any event, I think there needs to be some level of trust there!

Otherwise, it may be overkill but if there is a bare map you could have the rule require adding a guard with is_non_struct_map/1.

Where Next?

Popular in Questions 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
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
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
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
gonzofish
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
hpopp
To simplify some tasks at work, I wrote and published this package yesterday. It’s a simple macro that enables Access behaviour on struct...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New

Other popular topics Top

lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
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
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New

We're in Beta

About us Mission Statement