thojanssens1
Do you think it’s not a bad practice for context functions to systematically accept either the struct or the ID through two clauses:
def do_something(%Foo{} = foo) do
# do something
end
def do_something(foo_id) do
do_something(get_foo!(foo_id))
end
Trending in Questions
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
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
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
New
Latest Phoenix Threads
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ai
- #ecto-query
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #metaprogramming
- #hex











Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
thojanssens1
I actually came up with some thoughts after posting this, and I think it’s not a good practice, because context functions can have more than one argument, and the more functions with multiple schemas as arguments, the less it really makes sense at some point.
I’ve inherited a codebase that mixes up passing IDs and schemas, and I think it creates some inconsistency. I would go for enforcing only passing schemas.
baldwindavid
I would also like to know how others are handling this sort of thing. I prefer passing a struct for the pattern-matching assurances. However, sometimes I only have an ID and the extra query to turn that into a struct seems like an unecessary query.
As an example, suppose I want to filter accounts by property. There are actually four different ways I typically would want to query that resource:
This could be done via four separate functions:
filter_accounts_by_propertyfilter_accounts_by_property_idfilter_accounts_by_propertiesfilter_accounts_by_property_idsOr, this could be done with a single
filter_accounts_by_propertyfunction with multiple heads that accepts a schema, an ID, multiple schemas, or multiple IDs.I like the explicitness of the separate function names. On the other hand, if it is happening a lot the simplicity of a single function that knows how to handle these four common argument structures might be nice. This is something that could easily be a reusable convention in multiple cases. Naively, there could be a common function that converts the argument to a list of IDs with something like…
Then just use that anytime you want to support the four common queries. Example:
Part of me thinks it is against the spirit of elixir in terms of explicitness and the other part thinks it’s a little bit madness to continually repeat this ceremony.
thojanssens1
Maybe a simple convention might be: if that context function only uses the id, the argument is the id. Otherwise argument is always schema.
This then also gives some more information about the context function and what it really consumes.
sasajuric
I personally don’t think this is a bad practice, and I prefer it to
get_foo_by_bar,get_foo_by_baz& co. If the function accepts multiple args and you want to have polymorphism based only on the first one, you can do something like:So basically, you push the branching deeper.
Including a typespec can help the reader:
edisonywh
Yeah this is something unclear to me too, I do both versions, either fetching the resource for a schema context function, or extracting the ID out from schema. I don’t have a good guideline so they are all over the place right now..
It’s inevitable that sometimes you get only an ID to work with, so I am leaning towards the context functions with ID.
Something I’m experimenting recently is using my Condiment library, GitHub - edisonywh/condiment: 🍡 Add flavors to your context function without the hassles. · GitHub, so I would handle both cases (mostly only using it for resource fetching, so does not apply to all context functions).
Then again, it’s really not ideal, I am starting to use more IDs so would be nice to see what people are doing.
stefanchrobot
Good question! I lean towards ids since most of my context functions are consumed by controllers, so I don’t want any extra calls in the controller.
al2o3cr
In my experience even controllers that receive IDs have to check to see if the user is authorized to interact with / see the specified rows, so they wind up having to fetch the structs anyways.
sasajuric
You need to make the same check if the request arrives through web socket, graphql, raw tcp, iex, test, or any other interface, and so IMO this check is a domain concern and it should be done in the context.
eahanson
I would lean towards having the caller of
do_somethingcallget_foo!()explicitly, like:Now when reading the calling code, it’s really clear whether or not
get_foo!gets called, which can be nice ifget_foo!is expensive, perhaps making a database or network call. That clarity would be especially nice when reading code that callsdo_somethinga large number of times with the samefoo.stefanchrobot
Exactly my way of thinking. But I have a feeling that doing authorization in the web layer (as a plug) is a common practice. Personally I prefer my contexts returning
:unauthorizedand handle that with an action fallback. This makes controllers super thin to the point where testing them becomes questionable.