-
Notifications
You must be signed in to change notification settings - Fork 48
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
Suggestion of smell: mixing extraction and conditionals in pattern matching #9
Comments
I am missing "non-exhaustive pattern" checking in language like Haskell. We're very easily create a partial function when conditions more complicated or more and more clauses. or How to "make illegal states unrepresentable" in Elixir? |
It think that’s a separate discussion as it is more about language features and this one would require a type system and even then exhaustiveness checks may not be that straightforward. In the lack of static types, you make illegal states unrepresentable by not representing them. :) |
Great suggestion @josevalim! It is really related to "Complex multi-clause function", but with implications of its own. I'll add it to the catalog. I thought of using a shorter and more objective name, like "Complex extraction in function signature". What do you think about him? |
Maybe "Complex extraction in signature" to get even smaller... :) |
Either that or "Complex extraction in clauses", since it may apply to case, receive, etc. :) |
Great! "Complex extraction in clauses" sounds good! |
I just added this smell to the catalog. What do you think? https://github.com/lucasvegi/Elixir-Code-Smells#complex-extraction-in-clauses |
What do you think of leaving the def drive(%User{age: age} = user) when age >= 18 do
- %User{name: name} = user
+ %{name: name} = user
"#{name} can drive"
end |
Looks great, both ways are fine by me! :) |
Hi Tiago, Thanks for your comment. Your way is valid too. But I think the ideal would be to standardize the extraction forms (in the function signature and internally). I particularly think that the extraction using the struct is more in line with the smell description, so I prefer to leave it as is. :) Do you think we can close this issue (@josevalim and @tiagoefmoraes)? |
Hi there, I know this is closed but just had a question about why a user name is being extracted when we can avoid most of the typing by calling dot Before
After:
By removing the extraction in the method body we make the code "easier" (maybe?) to read. That also means the complexity of the function is where most of the code is, in this example it's in the function definition which means users will focus more attention on the function definition when skimming. Lastly, we know that a user has been pattern matched in, so it is safe to know we can call Maybe this is another code smell discussion 😅 "Don't use extraction when a dot will suffice" 😄 (Thanks for all the work btw!!!) |
Good point! The dot and pattern matching are semantically equivalent in this case. You could use the dot, but if you prefer to use dot, then it is less likely you will run into this issue anyway. :D so I would personally keep it as is! |
Excellent observation @AdamT! I will even think about the suggestion of "Don't use extraction when a dot will suffice". Anyway, about the current smell (Complex extraction in clauses), I think @josevalim comment represents what I think too:
The idea behind this smell is not necessarily to say that pattern matching/extraction is a bad practice and should not be used. I think it's more along the lines of: "If you're going to use this feature, it's better to use it this way..." 😃 |
Take the following code:
In the code above, the pattern matching of the user is extracting fields for further usage (
name
) and for pattern/guard checking (age
). While the example above is fine, once you have too many clauses or too many arguments, it becomes hard to know which parts are used for pattern/guards and what is used only inside the body. A suggestion is to extract only pattern/guard related variables in the signature once you have many arguments or multiple clauses:This is also related to "Complex multi-clause function".
The text was updated successfully, but these errors were encountered: