nikolis
Fair warning I come from an OOP backround so I might be missing something very obvious here.
Hierarchical Selection of value
Lately I have been trying to go over my elixir project and try to identify potential patterns in the code. One such pattern that I “identified” from my own code when I was working with developing custom components like for example a LiveSearchSelect component etc is the need to get a value through a hierarchical structure like for example get the field value from
- The form params (for example to get the values of the users latest changes)
but if this is nil - The changeset(For example if the form param are empty due to an update triggered through the changes happened from a javascript trigger interceptor which also triggers re render of the custom component)
but if this is also nil - Maybe some case that exist to save user progress
but if this is also nil
“empty string”
So I found this as a let’s say re occurring problem and I came up with a name in order to standardize a solution on this kind of problem for the context of my project at least.
The solution I found to be the most elegant and easy to maintain is the following
{first-degree, second-degee, thrid-degree} = {get_val, .., ..}
case tuple do
{nil, nil, nil } ->
whatever
{nil, nil, third-degree } ->
third-degree
{nil, second-degree, _ } ->
second-degree
{first-degree, _, _} ->
third-degree
end
Do you relate/have any to share?
So I wanted to share this mainly to ask whether other developers can relate ?? and if you have also “project level” patterns with standardized solutions on common problems of either your domain or in general when working with Elixir PhoenixLiveView and relevant technologies.
Trending in Discussions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #ai
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
cmo
I would use
withor||to avoid calling each getter. You might only need to call the first one.nikolis
The reason for the case do structure was that no matter the amount of layers there is not going to be any nesting, which would lead to code that is easy to test and maintain in general. Although I hear the argument about not needing to get all the values, in most cases this is very cheap operation. So in order for the with to make sense, it would need to not make any sacrifices in the “complexity/nesting” side of things, so how would you approach the same problem if you used
withinstead ?sodapopcan
or just:
EDIT: I missed the
whateverso if that is important, your case would usewithlike so:nikolis
About the first part I understand that you still need to define other outcomes for example what happens if you end up getting a result from the “get_second_val” then you still need to match that in the else statement making the hierarchy of the code less apparent (the way I see it) and when it comes to the second approach it’s for sure the most elegant but for me it did not work on the debug phase.
But in general the point of the thread was not to argue about the particular peace of code but rather open a discussion of less common design patterns that might be somewhat domain specific on the context of Phoenix LiveView does this make sense ?
garrison
This is pretty much the canonical
withuse-case as mentioned above, but for times when you already have all of the values acondcan also be used for nil-handling.Which is equivalent to this (but easier to read if there are many cases):
cevado
i’d use a cond in this context, something like:
if you see
not is_nil(value)getting repeated a lot, i’d define a guard in this module:sodapopcan
Certainly wasn’t trying to argue.
Fully agreed on
cond. It seems many people forget about it and I actually know people who actively avoid it (which I don’t fully understand). It’s probably the least used construct but really useful when it isOtherwise, for specific LiveView stuff, I would need to see more code as there may be cleaner ways of structuring it. For example (and this keeps coming up lately), I very very rarely look at params—I would pass them right to the changeset then check if
changesis empty. Otherwise, if a whole key is missing, I would maybe handle these in differenthandle_eventmatches. But again, very hard to say without seeing more explicit code.PS lol at your new avatar, @cevado
EDIT: It may have been a glitch but your avatar has changed again.
D4no0
I personally like cond too instead of the generic nested
caseorif/else, but I am not a big fan of assignments in eithercond/if, it’s a little bit too much magic, it’s completely confusing for folk that didn’t write elixir and even for newcomers.garrison
The cond will naturally guard against
nil(it is falsey). And I don’t think you can use guards in a cond anyway, right?But I agree with the spirit of your post, and actually I think this is probably better than
with:But this is all only for nil-handling. If we were dealing with
:ok/:errortuples it would be a different story.sodapopcan
I do this constantly. I feel understanding that everything is an expression is required knowledge if you’re going to be writing Elixir. A bit off topic, though.