Assume I'm working on a JavaScript application and make extensive use of the module pattern with one file per module. Now suppose I have two folders/modules: one for module A and another for module B. Module A uses module B, and B is not used by any other module. Should I put B inside of A, or should they be equals? What are the advantages/disadvantages of each decision?
Should I do this...
- A
- B (only used by A)
... or this ...
- A
- B (only used by A)
I'm looking for an answer that lists the benefits/tradeoffs for each option.
Here's a starting point based on my experience:
A containing B
Benefits:
- The most intuitive/common decision
- Ensures the location of B is immediately apparent (really, this isn't that hard either way) - no digging through lots of modules in project root
Tradeoffs:
- Doesn't encourage developers to think of B as a standalone module, often leading to excessive coupling
- When making changes, it's easier to overlook an existing module that can be reused with minimal effort (because it's nested inside of a subfolder)
With modern JavaScript, I think the tradeoffs outweigh the benefits, but I may be missing something. I'm hoping to find some kind of universal truth – maybe even one that can be applied to similar concepts (like packages, DLLs, namespaces, etc.).