3 ms·
I’m always amazed just how tolerant javascript’s import system is when I have circular imports. I guess maybe because it doesn’t care about modules and just car
by Waterluvian 5y ago
I’m always amazed just how tolerant javascript’s import system is when I have circular imports. I guess maybe because it doesn’t care about modules and just cares about specific elements that are being imported/exported.
When I do have a nasty circular dependency Webpack usually does a bad job telling me what’s wrong.
Though I should still treat circular imports as, at the very least, an organization code smell.
- brundolf 5y agoCircular imports in JS matter when the imported code is being called immediately at import time. If a file defines functions that later call functions from another file (and vice-versa), but those symbols will be populated before the function actually gets called, there's no problem
- Waterluvian 5y agoYeah. It’s so flexible. I get frustrated at Python (Django) serializers that legitimately need to depend on each other. And the answer on the forums is to create a near duplicate class.
- nyrikki 5y agoJavaScript does not provide namespaces by default which allows this. Python is built upon namespaces and cycles in dependencies, either viewed as graph theory or kSAT introduce that fun NP-complete problem of version hell. Using `need` in Javascript maintains the directed acyclic graph structure, but if you get fancy you will run into the same problems with circular depends in Python. Karp's 21 and/or SAT will catch up with you at some point if you don't respect the constraints that make the problem tractable. Note I am not saying I prefer or like pythons choices...but that they had to make one.
- Waterluvian 5y agoSorry what’s ‘need’ ? I can’t seem to find it as a keyword.
- BadInformatics 5y agoIIRC an explicit design goal was to enable circular deps (hence why imported bindings are considered "live". It's interesting to see this works in practice though, I've never tried using them myself.
- zarzavat 5y agoCircular imports in JS are fine and not a code smell, for instance in typescript you may have two classes that reference each other's types - obviously this is not a real import from JS's perspective but the point is that you should not have to care whether it is real or not. That's a world we don't want to live in. Circular imports are only ever a problem when you have code running when the module loads. Then you run into module load ordering issues. So avoid any side effects on module load and make all setup explicit.
- paulddraper 5y agoAllowing circular imports was the ugliest part of ECMAScript modules.