Contexts - a barrier too high for newbies?

I’m not really sure if my thoughts should really be posted here, or in the other thread, or a new thread. If it’s not appropriate please feel free to move it.

As someone from a self-trained, and more sysadmin background, the explanation from @slashdotdash is spot on how I feel I should be using contexts. I totally get it in theory. And I suspect many people do.

However in my tiny team of me, myself and I (working on a personal project), I’m finding I’m getting literally anxious about Doing It Right™. Which I know is dumb, but I do wonder if this is how other people feel and what’s causing some of the discussion towards contexts.

I’m hoping a small example might help out where I’m personally getting “stuck” -

Assuming a simple todo web app (because this is 2017, what else?) - users can only see their own todos, unless they’re marked as an admin.

My fictional, web only, system is behind authentication. When a request is made with a token/cookie/whatever currently I can either verify and load the user from the DB, assigning a user struct to the conn (my user struct has some kind of role/permissions/flag/whatever that dictates they’re an admin). Or I can just assign the user id to the conn.

If I just had the user_id on the conn, when I call Todos.list_all I have to load the user from the DB for a second time to determine their permission. My brain screams at the inefficiency of doing it again when I literally just did it to verify the user hasn’t been disabled/whatever. Or if I had a whole user struct loaded, I can just pass that as an argument to list_all - probably using pattern matching to behave differently for admins and standard users.

My anxiety comes in here.

I feel with contexts that I probably shouldn’t really be passing whole user structs around, as I’m coupling my account/login/whatever context to many other contexts. But the other part of my brain tells me querying the DB multiple times for shit I already have, or could have collected when the initial request was made, is dumb. The app isn’t going to become huge. There’s no need to worry about it. By passing around a struct for a user I can use 2 functions with different pattern matching to deal with the permissions differently, and it feels sane. But kinda dirty.

The anxiousness paralyzes me and I have this endless back and forth in my head.

The second part of my anxiety starts coming in my general lack of decent experience with general API design and whilst I totally understand that this is not specific to “contexts”, I suspect is also a factor. Contexts force me to learn and experiment about what is good/better API design. Say I wanted to start paginating the todos, because admins now have a lot of todos (what with being able to see everyone’s). Adding extra arguments for page_number and items_per_page will land me in trouble eventually as I add more and more arguments for specific things. Having a map of arguments feels opaque. The anxiety comes back.

Ultimately I don’t feel like I’m really experienced enough to make these decisions and I end up flipping back and forth between competing designs without ever achieving something.

Edit: These are obviously my personal feelings of inadequacy at play here. I’m not trying to shit on the direction the framework is going. I actually think it’s a good idea, because it will eventually make me better at writing and understanding code (and therefore a better sysadmin I hope). I just feel seeing the same kind of explanations over and over again isn’t really helping me, but adding to my anxiousness of “shit you’re doing it wrong, son”.

9 Likes