5 ms·
If you use Phoenix, using types at the data model level using changesets and then trickling them down all the way to the UI is a very good compromise. As change
by neya 4mo ago
If you use Phoenix, using types at the data model level using changesets and then trickling them down all the way to the UI is a very good compromise. As changesets provide type validations out of the box too.
- steve_adams_86 4mo agoDo changesets incur a runtime cost?
- cultofmetatron 4mo agoyes, they do. its minimal though
- asa400 4mo agoEcto Changesets[0] are runtime constructs, yes. They're similar to libaries like Pydantic, if you're familiar with Python. [0] - https://ecto.hexdocs.pm/Ecto.Changeset.html https://ecto.hexdocs.pm/Ecto.Changeset.html
- steve_adams_86 4mo agoYes, this is exactly what I was wondering, thanks. Another version of this that I love is Effect Schema in TypeScript land. The runtime costs aren’t trivial, especially on large datasets, but I’ve come to love this pattern a lot.
- sph 4mo agoNot sure what you mean here. Changesets are used to validate user input before interaction with business logic or your database; of course data validation has a runtime cost, in any language. Please don't use changesets to enforce some kind of type system between system components. In case you do not trust your own code, Elixir is strongly typed (though not static typed), there are test cases, there's dialyxir and if still you cannot stop yourself from passing a number where a string will do, the process will crash, log a message for you to fix the bug, and get restarted by a supervisor. I get why people are obsessed with static typing on "normal" languages, where bugs cause system downtime, but the Erlang platform gives you so many guarantees that even if you somehow make a mistake, it is never catastrophic. Gradual typing in Elixir is a nice cherry on top of the runtime, not the cornerstone to robust OTP software.
- nesarkvechnep 4mo agoYeah, one of the worst practices. I've been working with Elixir professionally for 6 years now and I still see this sh*t everywhere. Bad APIs, bad UIs because someone coupled themselves to the database structure and can't escape. List of memberships? Keep them as a list with the same fields as the junction table. Top-level APIs taking maps with string keys as "params" so they can very easily be cast for a changeset.
- neya 4mo agoThis was the only out of box solution when Elixir didn't support types. So, if you really did Elixir professionally for 6 years, you'd know that by now. > Bad APIs, bad UIs because someone coupled themselves to the database structure and can't escape. If you don't commit yourself to the database structures you defined at the time of application creation, then it just reflects poor planning and architecture overall as that is one of the very first things you do. What you describe is an approach a lot of NoSQL fans use - use whatever works then, worry about datatypes later on. That's how you shoot yourself in the foot. > List of memberships? Keep them as a list with the same fields Again, using embeds_many or has_many works well too, using changesets - which is my point exactly. Not sure where the disagreement is here. Your account is full of just ragebait comments at a quick glance, so I'm just going to leave it here.
- bbkane 4mo agoI haven't used Elixer but tt's generally a good idea for the UI to have a different data model than the database (even if it means you initially type almost the same thing twice and have to write a tedious translation layer). This lets you evolve each part independently and use the "native" types frontend vs backend, which happens surprisingly frequently as the app grows
- Kaliboy 4mo agoPhoenix does have that. ViewModels. I don't think its required to use though, but we always do.
- 4mo ago