Foo may potentially change and rather than have to update my code at separate locations, I was thinking it may be worth it to abstract this away to a global variable that is visible by both client and server.
My question is if this a desired pattern in Meteor and even general JS.
I understand I can abstract this away into a module, but that may be overkill in this case. I also think using session/reactive vars is unsafe as it can kinda lead to action at a distance. I’m not even gonna consider using settings.json as that should only be for environment variables.
using a function seems like way too much indirection.
Globals work great, right up until you want to port code around, or have to deal with a breaking change to an internal or external API.
it’s really useful to have imports just so you can see exactly what each module depends on.
As an example, at my work, we were using globals for everything when I started, which was really convenient! But it meant that as a new developer, I had no idea where things came from or how the whole thing ties together. Worse, when I started a new project using only some of the packages, nothing worked! Turns out some packages and libraries relied on globals from other packages. Of course, this would have been fine if they were declared correctly in Package.js, but the convenience of globals meant that past developers had forgotten to add the dependencies correctly.
This was also an issue when we started modernizing our codebase to ES6 and ES Modules. During which, we removed the global exports and then one by one added the imports everywhere.
This process exposed a lot of really tightly coupled code which didn’t need to be, so we refactored a bit to untangle the spaghetti and make sure that the modules worked independently from each other.
So there’s a trade off between the extra work of maintaining import/export statements and speed. Which side works for you likely depends on the size and lifetime of your project.
I still use globals for one-offs or prototyping, while in our main app (~25k lines) globals are banned
A nice modular way to do it is by constants or a constants object: