Skip to content

One-line node modules #10

Closed
Closed

Description

@hemanth

Lot of people don't get the benefits of one line modules, what would you say to those who tend to critic a lot about node modules that have just a line in them?

Activity

  1. daryl commented on Jul 1, 2015

    @daryl

    I'm curious about this too, I see some modules and wonder why they're at all necessary.

    This one (not by you) is a good example.

  2. barangutan commented on Jul 1, 2015

    @barangutan

    I'm also curious about this. In particular, referring to user-home as one of your examples?

  3. sindresorhus commented on Jul 1, 2015

    @sindresorhus
    Owner

    Blog post.

    tl;dr You make small focused modules for reusability and to make it possible to build larger more advanced things that are easier to reason about.

  4. arthurvr commented on Jul 1, 2015

    @arthurvr

    This is why I wish you had a blog :p I never was able to explain it that well. Good job.

  5. changed the title [-]About one line node modules.[/-] [+]About one-line node modules[/+] on Jul 1, 2015
  6. changed the title [-]About one-line node modules[/-] [+]One-line node modules[/+] on Jul 1, 2015
  7. sarbbottam commented on Jul 1, 2015

    @sarbbottam

    👍

  8. barangutan commented on Jul 1, 2015

    @barangutan

    I guess my question (and this is a legitimate question as I'm relatively new to nodejs!) was, do you see a legitimate reason for splitting user-home out from os-homedir? I mean, obviously you do as you've done it that way, but I was wondering why you just didn't just include a cached version in the original os-homedir

  9. bahmutov commented on Jul 1, 2015

    @bahmutov

    I often find that these simple tasks, like finding user's home folder, turn out to be very non-trivial and hairy the more the code gets used. Making a separate module right away allows to properly test and fix bugs, add features etc, as new issues absolutely will arise.

  10. spacegauch0 commented on Jul 1, 2015

    @spacegauch0

    👍

  11. sindresorhus commented on Jul 1, 2015

    @sindresorhus
    Owner

    I guess my question (and this is a legitimate question as I'm relatively new to nodejs!) was, do you see a legitimate reason for splitting user-home out from os-homedir? I mean, obviously you do as you've done it that way, but I was wondering why you just didn't just include a cached version in the original os-homedir

    The user-home module was made long before os-homedir. os-homedir was made recently as a polyfill of the forth-coming built-in API in Node. I just made user-home depend on it so it will benefit from the native API. I could of course deprecate user-home now, but there are so many projects depending on it now, it really isn't worth it. I also like how in user-home it's cached by default instead of having to call it as a function. However what you're asking isn't really relevant to the small module thinking. This is just a side-effect of evolution.

  12. barangutan commented on Jul 1, 2015

    @barangutan

    Ok, thanks for the clarification! That makes a lot of sense. I just stumbled upon user-home today and my curiosity aligned perfectly with this AMA.

  13. sindresorhus commented on Jul 1, 2015

    @sindresorhus
    Owner

    I often find that these simple tasks, like finding user's home folder, turn out to be very non-trivial and hairy the more the code gets used.

    👍 Every time :p

    Everyone; Keep in mind that I've failed doing the right thing hundreds of times before realizing making a separate module would be a better thing.

  14. 26 remaining items

  15. sindresorhus commented on Jul 3, 2015

    @sindresorhus
    Owner

    LOC is pretty much irrelevant.

    I utterly disagree with this, specially if you are coming from:

    That statement is context-sensitive, but to clarify: LOC is irrelevant for whether something should be a module or not.

    long, crappy, and unnecessarily complex code is just bad.

    Did not intend to imply the contrary. The point I was trying to make is that code is worth being a module even though it's just one line, not that hundreds are necessarily good. That really depends.

    Unnecessarily complex code is obviously bad, but it's also easy to think something is unnecessarily complex without seeing the full picture. There have been a lot of lightweight jQuery clones, but they all seems to miss why jQuery is large, it actually fixes a lot of browser bugs. Sometimes code is complex because it has to be. It might handle more edge cases which are found over time and makes the code more durable.

  16. icyflame commented on Jul 3, 2015

    @icyflame

    Sometimes code is complex because it has to be. It might handle more edge cases which are found over time and makes the code more durable.

    Totally! 👍

  17. Rich-Harris commented on Jul 3, 2015

    @Rich-Harris

    I wrote an article expressing a counter-argument over here, and @sindresorhus invited me to link to it: https://medium.com/@Rich_Harris/small-modules-it-s-not-quite-that-simple-3ca532d65de4.

    It's not a direct rebuttal of the points made here, but rather an attempt to articulate some concerns I have about the small modules philosophy. This (excellent) discussion prompted me to publish them.

  18. shidhincr commented on Jul 4, 2015

    @shidhincr

    👍 I'm loving this discussion .. :)

  19. nickedes commented on Jul 8, 2015

    @nickedes

    Awesome 👍

  20. samarpanda commented on Jul 19, 2015

    @samarpanda

    @hemanth Great question, @sindresorhus Fantastic answer. 👍 Awesome 💯

  21. halhenke commented on Aug 26, 2015

    @halhenke

    Late unwanted perspective:

    I think theres no hard and fast rule that I can think of to answer the question. Generally i think modularity has huge advantages but there is a point where it potentially becomes unhelpful. People have voiced reasons concerning the friction of discovering and vetting packages. I also think that sometimes when we refactor code with the goal of removing complexity we actually just end up shuffling complexity into a different part of your application. I dont think anyone wants to look at a package.json file with 1000 top level dependencies and try to figure out whats going on. Obviously thats an extreme scenario but I think it illustrates the idea that the returns from maximising modularity can diminish to the point where they are exceeded by the accumulated minor pains associated with managing all these dependencies.

    Also the thought of what a new npm package name might look like in 5 years time is vaguely terrifying.

  22. janeluck commented on Nov 16, 2015

    @janeluck

    The power of Organizational Resources

  23. deanrad commented on Dec 22, 2015

    @deanrad

    Very true and appealing, but there are definitely costs to module creation and maintenance you don't mention.
    Here is the rundown of the line count for your 'one-line' negative-zero module. (Edit: And I see you don't have a .travis.yml either so add some lines for doing that, and some time for checking that out.)

    wc *
           4      18      89 index.js
          21     172    1119 license
          32      57     559 package.json
          36      56     624 readme.md
          12      20     259 test.js
         105     323    2650 total
    

    Some costs can be alleviated with good tools (e.g. yeoman generator for modules); others like crafting tests and Readmes cannot. I'd love a world where people did what you said- but the real and percieved costs mean that not doing so is rational in many cases.

  24. sindresorhus commented on Dec 22, 2015

    @sindresorhus
    Owner

    There's indeed a slight overhead doing it this way, but I think the benefits outweigh that.

    others like crafting tests and Readmes cannot

    You'd hopefully be writing tests and docs for the code anyways.

  25. bahmutov commented on Dec 22, 2015

    @bahmutov

    Plus one can target what files are published into NPM package using "files" or ".gitignore" or ".npmignore" settings, see http://glebbahmutov.com/blog/smaller-published-NPM-modules/ - the strict boundaries among the packages are really worth the slight increase in size, in my opinion

  26. halhenke commented on Dec 30, 2015

    @halhenke

    Just as a thought - perhaps this sort of stuff might be more useful at a community level if it was part of a larger library of tools - something like a micro-lodash?

    The primary benefit would be one of discoverability/visibility - people would know where to look if they wanted to check if a oneliner existed for their particular use case and over time people would become more and more familiar with what was availabe. Then of course the individual one liners could be available as individual packages much as lodash functions are today.

    While "programming via require/import" seems a little weird to me at first thought, there are benefits as mentioned around testability etc. The more I think about it the more a oneliners library seems like it could be pretty cool.

  27. Repository owner locked and limited conversation to collaborators on Mar 24, 2016
  28. added a commit that references this issue on Jun 26, 2019
    8a2bfaf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions