-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New Check: enforce pattern matching by structs on function parameters #103
Comments
👋 @rrrene I'm implementing these checks, but I would like your opinion. Do you think it makes sense? The second check can be useful for the upcoming Elixir 1.18 to take advantage of the type inference. |
Hi, what would be the benefit of using def get_role(user = %User{}) do
user.role
end over def get_role(%User{role: role}) do
role
end Wouldn't this be equally telling to the compiler? |
That's not what I said. The benefit is I want to enforce that if you are pattern-matching by a map with atoms, it probably should be a struct. |
Okay, I can see how this might be a rule that you could align on in a specific situation. But why should this be a standard Credo check? People use maps with atom keys all the time or am I misunderstanding something about the proposal? |
Why would you use a map with atoms instead of a Struct? The only case I can think about is a configuration object/map, but configurations don't have to be passed as parameters to functions because you can access them using On the other hand, this rule could help take advantage of the new type inference coming in Elixir 1.18 |
With the upcoming Elixir releases introducing type inference, a rule to enforce pattern matching by struct will be beneficial.
This is the behavior I would expect from this rule:
🛑 Accessing to variable atom property:
🛑 Pattern-matching by an atom key of a map:
✅ Pattern-matching by Struct helps the developer know the type and shape of the parameters, it also helps the code editor with autocomplete.
Maybe the rule can be split in two, one enforcing pattern-matching by the property you are using in the function body to avoid access to unexistent fields, and another enforcing using structs when you are pattern-matching by maps with atom fields.
First Check
Prefer pattern-match by map property the body of a function access to a map field:
🛑 Accessing a map field in the body:
✅ Pattern-matching by an atom key of a map:
🛑 Accessing a map field in the body:
✅ Pattern-matching by an atom key of a map:
Maybe it can be called
Credo.Check.Warning.SafeParamAccess
? 🤔Second Check
Prefer pattern-match by struct when pattern-matching by map atom:
🛑 Pattern-matching by an atom key of a map:
✅ Pattern-matching by struct
Maybe it can be called
Credo.Check.Refactor.FunctionMapParams
? 🤔The text was updated successfully, but these errors were encountered: