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 94592 917
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
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
heathen
Quite interesting article Google brought me. Didn’t find any mentions about it here. What do you think in general? Would you use togethe...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
AstonJ
Since we have deprecated our Erlang sections (as we have dedicated Erlang Forums now) let’s add this thread for those who’d like to post ...
New
maennchen
:warning: Security advisory: Decimal DoS vulnerability A vulnerability has been published for decimal where very large exponents can cau...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews