aj-foster

aj-foster

tl;dr I have a process that passes around a lot of state. Doing this instead of using the process dictionary affects coding design decisions. What would you do?

I have a project that passes around a lot of state. For example, this tracks the processing of an OpenAPI description as operations and schemas are discovered and named.

So far, I’ve followed the best practice of always passing this state around as arguments rather than using the process dictionary. It makes a few things awkward, like the occasional function that doesn’t need to know or update state except in rare circumstances, but must pass it through anyway. Because this project is meant to be pluggable for its consumers, I’ve made certain choices to avoid passing state in and out of callbacks in situations where a consumer is likely to mismanage the data. This has secondary effects for the project’s architecture which, although workable, reduce flexibility.

As the author, I can foresee and enforce that all of the processing will occur in a single process. I can also provide a suite of safe state management functions that discourage any manual editing of the data. The flexibility of always having the state available to read and write is alluring.

What would you do? Why? If you’re on the side of never use the Process Dictionary, can you imagine a circumstance where you would?

Showing Posts 1 to 3

mpope

mpope

I’m on the side of ‘Use the Process Dictionary when it makes sense’. It is often easier to use the process dictionary to store and access ‘context constants’, like the pid of a parent or a user’s name or ID that will never change in the current context, and those processes die at the end of the context so they do not linger. Mutable state or it’s side effects caused by changes to Process Dictionary can be an absolute nightmare to debug. In your case being used from a single process as mentioned could be very difficult without care, what if two instances were being used in the same process for example.

So far, I’ve followed the best practice of always passing this state around as arguments rather than using the process dictionary.

I think I agree that there is some cumbersomeness with having to pass a state term to APIs even though in some cases it might not be used, and further returning it from all library functions. Consider this: passing the state and returning the state even if the library might not actually use it is easier for to remember than ‘Does this API require the state object or not…’. A consistent interface is useful.

The alternative to returning the {state, value} tuple is to use getters and having the state object perform all tracking:

state_new = OpenAPI.Processor.Type.from_schema(state_old)
value = OpenAPI.Processor.Type.get_schema_value(state_new)

discourage any manual editing of the data

Good luck :wink:

derek-zhou

derek-zhou

Then shouldn’t you just use a GenServer and hide the state from the outside?

al2o3cr

al2o3cr

An “intermediate” storage strategy might fit better: keep an ETS table (or several) that are passed into functions.

That would give the same “implicit write everywhere” mutability as the process dictionary, without the exactly-one-per-process limitation.

— All posts loaded —

Where Next? Top

Trending in Discussions Top

AstonJ
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
2977 92995 915
New
AstonJ
The obligatory hello world thread! Who are you and where are you from? :stuck_out_tongue:
4616 55835 594
New
caslu
I want to open this thread for you all to discuss and help those who really like Ash but are still hesitant to use it in a real project. ...
New
Herve37
We’re evaluating API mocking tools for OpenAPI-based projects and would love to hear what other teams are using. We’re particularly inte...
New
matt-savvy
Is there a word for the ~> symbol used in Version strings? Do you also just call it a Squiggle Arrow™ ?!
New
GES233
I’m posting this in response to Jose’s recent tweet (Cr. link) : People are sleeping on Elixir for a coding harness: Hot-code swappi...
New
nseaSeb
AcmeScript — Writing JS hooks as if I were still using Elixir I’ve been having fun building a little something over the last few days: Ac...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New
webofbits
Aludel - LLM Evaluation Workbench Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews