Advantages of functions as modules

100 views
Skip to first unread message

Sameer Joshi

unread,
Oct 2, 2014, 4:20:37 AM10/2/14
to nod...@googlegroups.com
Hello Team!

I wanted to know, what are the advantages (if there are) of calling functions from modules using modules.exports as opposed adding all the functions to the main app JS file.

I am looking for a simple comparison or pro's and con's for points apart from that it gives logical modularity and readability.

Thanks a ton!

Angel Java Lopez

unread,
Oct 2, 2014, 10:32:13 AM10/2/14
to nod...@googlegroups.com
Short answer, English is not my strong point ;-)

A keyword: modularization

Instead of populate the global environment, each module exposes the functions that needed to be consumed, without worrying of name conflicts with other modules.

And there are more benefits:

- Each module tries to cover a single domain of problem, module trends to be small in node.js ecosystem (except if it is a framework)
- The internal representation is in the module, only the top of the iceberg is exposed via module.exports
- Notably, you will see few classes exposes. Instead of "new mymodule.MyClass()" you will see "mymodule.createObject(...)", factory methods, that encapsulates the real implementation

Don't forget the NPM and versioning system: each module can consume exactly the required version of other module. Then, A could consume B...@0.1.0, and C could consume B...@0.1.1, without problem. If B module populated the global environment, such separation could not be possible. NPM is the "secret weapon" in Node.js ecosystem.

Paraphrasing David Hilbert: "No one will drive us from the paradise which Node.js modules created for us"

Angel "Java" Lopez
@ajlopez


--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
To post to this group, send email to nod...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/1503ae1e-db83-46c7-968f-65896a9e618c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Floby

unread,
Oct 8, 2014, 6:10:51 AM10/8/14
to nod...@googlegroups.com
Everything AJ said  +

-> Testability
Reply all
Reply to author
Forward
0 new messages