nikolis
Do you have project level patterns with standardized solutions for common problems?
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.
Most Liked
cmo
I would use with or || to avoid calling each getter. You might only need to call the first one.
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.
garrison
Both the Elixir and Erlang syntax references call them variables, but I see what you’re getting at. What I meant was that in another language, say JS, this is confusing:
let x = 0;
if (x =1) { x = 2; }
What is x here? What if we defined x inside the condition? And so on.
But this can’t happen in Elixir because the assignment in the if condition is properly scoped and those scopes are very consistent because the language is designed well.
But certainly I can see a point in Elixir where this could start to get ugly, like this:
cond do
({:ok, {x, _y, _z}} = do_thing()) && x -> x
end
Definitely don’t do that lol
Popular in Discussions
Other popular 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
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance










