0

I'm starting to learn about JavaScript code organization using ES6 modules and webpack. Something I don't understand is how using them is possible without making the modules tightly coupled to each other. Doesn't using exported code makes it inherently impossible to create loosely coupled modules?

Chestnut
  • 1
  • 1
  • 2
    May you share your definition of tightly/loosely coupled code? – evolutionxbox May 26 '20 at 10:01
  • In case of modules, I understand that a tightly coupled module would be a module that is dependent on another module to be able to run properly. Say, I have a module which handles changes in DOM and another modules uses those changes to do its thing – Chestnut May 26 '20 at 10:05
  • What’s loosely couple code? – evolutionxbox May 26 '20 at 10:09
  • That would be code that is able to run on its own and even if it uses some exported functionality, it doesn't directly know where it comes from so it doesn't completely crash if the source is changed or removed – Chestnut May 26 '20 at 10:16
  • By those definitions, I can’t tell why exporting code would make it neither loosely nor tightly coupled. Instead it’s the code using the exports which could become tightly coupled. – evolutionxbox May 26 '20 at 10:22
  • Am I not understanding loose/tight coupling correctly? It's just that right now I'm working on a study project which requires the code to be separated in modules and also warns not to make them rely on each other so much that removing one would require changing the other. But if I use an exported function and then delete the source, wouldn't that mean that the remaining module can no longer perform its tasks? Is it just a matter of making moderate amount of connections? – Chestnut May 26 '20 at 10:31
  • I would ask your tutor. It seems I don’t understand either. – evolutionxbox May 26 '20 at 10:32
  • Just been reading up on the topic, and it still seems to me that tight/loose-ness of code has little to do with exports/imports, but more to do with how that code it written https://www.reddit.com/r/learnjavascript/comments/30mnra/tight_vs_loose_coupling_examples/ – evolutionxbox May 26 '20 at 11:03
  • I guess it makes sense. Thanks a lot! It's been really bugging me – Chestnut May 26 '20 at 11:29
  • There's a formula?! https://en.wikipedia.org/wiki/Coupling_(computer_programming)#Module_coupling – evolutionxbox May 26 '20 at 11:30
  • 1
    "*warns not to make them rely on each other so much that removing one would require changing the other.*" - that doesn't quite make sense. Removing arbitrary code from your program is always expected to break something, and will require fixes elsewhere. Coupling refers to *how many* changes will become necessary, and while lower is better, zero is not possible - at some point the parts *must* refer to each other. In general, you should program against an interface instead of a concrete value - but only in places where it makes sense. How the code is distributed across modules is separate. – Bergi May 26 '20 at 12:42

0 Answers0