Fork me on GitHub
#re-frame
<
2021-10-26
>
alekszelark14:10:18

Hi! Is it ok to use global vars like default-db here https://github.com/day8/re-frame/blob/4e0286caac6260b2811a31778f59b6ed156f5c61/examples/todomvc/src/todomvc/events.cljs#L119 in event handlers? Or it is just for the sake of learning purposes there? I don’t think it’s a good approach to use in real apps, it leads to impurity in the first place. However, my college has a different opinion, and he encourages using global vars like default-db in event handlers.

p-himik15:10:44

100% OK. default-db is immutable. The fact that it's a global thing is irrelevant - as long as it's kept as an immutable constant. After all, you're relying all the time on things like assoc - and it's just a global var itself.

alekszelark17:10:16

Thanks. I understand that it is immutable — a constant. But if you have many such constants which are using in many event handlers and subs, it’s getting harder to maintain the code.

p-himik17:10:41

If you extract every single constant thing to the top-level - sure, I agree. But if it's used multiple times or semantically belongs to some existing namespace - definitely extract and name it.

p-himik17:10:55

In that particular case that you linked default-db absolutely deserves to be put into db.cljs - it belongs there. All DB-related things go there.