nikolis

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

  1. The form params (for example to get the values of the users latest changes)
    but if this is nil
  2. 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
  3. 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

cmo

I would use with or || to avoid calling each getter. You might only need to call the first one.

sodapopcan

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

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

Where Next?

Popular in Discussions Top

ricklove
I was just introduced to Elixir and Phoenix. I was told about the 2 million websocket test that was done 2 years ago. From my research, t...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54120 245
New
rower687
Hi all, I’ve been reading a lot about the “let it crash” term and how supervising processes and the whole messaging passing make an elixi...
New
rms.mrcs
A couple of days ago I was discussing with a friend about different approaches to write microservices. He said that if he was going to w...
New
wmnnd
The Go vs Elixir thread got me thinking: Would it be too hard to implement a simple mechanism for creating Go-style static app binaries f...
New
Jayshua
I recently came across the javascript library htmx. It reminded me a lot of liveview so I thought the community here might be interested....
New
Rustixir
Hi everyone, im working on find best language/framework/system for high concurrency, high performance and stable performance after wor...
New
cblavier
Hey there, It’s been more than a year since we started using LiveView as our main UI library and building a whole library of UI componen...
New
100phlecs
Are there any downsides, like perf issues, to putting all functional components in CoreComponents as long as you prefix it with the conte...
New
matthias_toepp
I’d love to hear what people think about Wisp, the new Gleam web framework started by Gleam’s primary creator Louis Pilfold. Gleam, alon...
New

Other popular topics Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement