Pattern Matching Functions to solve "Complex Branching" #27
dotdotdotpaul
started this conversation in
Ideas
Replies: 1 comment 2 replies
-
Thanks for raising this discussion @dotdotdotpaul ! This modification was motivated by a good discussion I had with @josevalim on this issue(#4). I think it's worth taking a look at it. Anyway, we can keep this discussion open to thinking about it together. |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Ref. https://github.com/lucasvegi/Elixir-Code-Smells#complex-branching
I'm not a fan of having a gazillion private functions named with overly "descriptive" names, like is described under "Complex Branching" (see above).
I absolutely agree you don't want large
case
orcond
(orif/else
for that matter) blocks where you have to trace indents across screens of code. But I wonder if the better refactoring pattern isn't moving those blocks into individual functions, but rather, using pattern matching functions to replace the conditional altogether.Instead of the suggested
Would this be better handled by piping to a handler function that pattern matches on the result to handle "branching"?
This has the benefit of keeping each branch in separate functions, without cluttering function namespace. Because the function names are the same, linters will flag if the handler functions get "separated" by new code (I would argue that it is important to keep these handlers together, regardless of which pattern is used -- but if the functions are named differently, there's nothing forcing that proximity).
It's probably hard to make a generalization about which is better (with the small one-liner function bodies from the original example, it's hard to say that any refactoring is worthwhile at all).
Just thought I'd start the conversation. 😄
Beta Was this translation helpful? Give feedback.
All reactions