8 ms·
>TL;DR: Small modules are easy to reason about, and encourage code reuse and sharing across the entire community. How does that even remotely applies to the "
by coldtea 11y ago
>TL;DR: Small modules are easy to reason about, and encourage code reuse and sharing across the entire community.
How does that even remotely applies to the "is positive integer" test, and even more so to "it's positive" and "it's integer"?
What's next? is-bigger-than-5? word-starts-with-capital-letter? add-1-to-a-number?
- userbinator 11y agoadd-1-to-a-number This seems to be a very common example in educational materials about how to create a function in some programming language. Perhaps they should all be prefaced with "NOTE: you should not actually make such a function, because its triviality means that every time you use it you will be increasing the complexity of your code", or a longer discussion on when abstraction in general is appropriate and when it is not. Ditto for any other abstraction --- I've found there's too much of an "abstraction is good so use it whenever you can" theme in a lot of programming/software design books and not enough "why abstraction can be bad" sort of discussion, which then leads to the "you can never have too much" mentality and the resultant explosion of monstrous complexity. Apparently, trusting the reader to think about the negatives and exercise discretion didn't work so well...
- Stratoscope 11y ago> Apparently, trusting the reader to think about the negatives and exercise discretion didn't work so well... That's the whole point. If you use is-positive-integer, you won't have to think about the negatives!
- n0us 11y agoNo, you see "is-bigger-than-5" clearly does 4 different things. You need these modules: module.exports = exec("==="); module.exports = bigger(a, b){return a > b}; module.exports = () => return "than"; module.exports = (a) => a.search('ABCD...'); .. or something like that. These are all plugins of course.
- deleted 11y ago[deleted]
- lewisl9029 11y agoIt doesn't. Of course we have a number of packages that are truly trivial and should probably be inlined. Npm is a completely open platform. Anyone can upload any module they feel like. That doesn't mean anyone else will feel the need to use them. I think you'll find that the vast majority of small modules that are widely used are ones that also cover obscure corner cases and do benefit from widespread use and testing.
- iainmerrick 11y agoYou don't have to be widely used to be widely used, though. I bet there are tons of developers who would never dream of using left-pad or is-positive-integer, but nevertheless have copies of them (multiple copies?) on their computers due to transitive dependencies.
- Bjartr 11y agoIf you want to build is-bigger-than-5 yourself, I recommend pulling in fivejs[1] as a dependency. [1] https://github.com/jackdcrawford/five https://github.com/jackdcrawford/five
- agumonkey 11y agoLucky for you, I just made yeonatural.js, project template generator that can instanciate fully amd compatible <n>.js. We're doing a GSoC for efficient templating of real number though.
- hasenj 11y agoThis module is a joke, right? Right?
- awalGarg 11y agoI would have bet on "yes" until a day ago.
- dclowd9901 11y agoYes it's a joke. In fact it's a joke on this very matter.
- brentburgoyne 11y agofive.js was missing something. I fixed it. https://github.com/jackdcrawford/five/pull/234 https://github.com/jackdcrawford/five/pull/234
- tribaal 11y agoI laughed way too hard at this :) Well played.
- vmateixeira 11y agoNo way!
- ucho 11y ago
- imron 11y agoNot defending javascript because I dislike it immensely, but with javascript being a dynamically typed language, and with the way it handles implicit conversions between types, and with trickiness around NaN, Infinity and null objects, there are sufficient edge cases that writing a correct function to test if some variable contains a positive integer is not as trivial as it is with more sane languages, and is likely to trip up even experienced developers if they miss one or more of those edge cases. Having pre-written modules that account for all edge cases that have been battle-tested by hundreds, thousands, or even millions of users does have merit. The main problem here is with the Javascript language, which makes code evolve to things like 'is-positive-integer'.
- xorcist 11y ago(That's the whole point of using a dynamic language, what '>' actually does depends on the surrounding code. If you want '>' to type check you should be using a type safe language to begin with.) The issue I have with this is readability. Type '>' and I know exactly what it does, I know what implicit conversions are involved, and how it would react to a null object. Type 'isPositiveInteger' and I need to check. I can not read your code fluently anymore.
- pliny 11y agoWhat if sometimes I want the overloaded, type relevant '>', and sometimes I want to do '>' if the inputs are positive integers, and raise an exception otherwise?
- golergka 11y agoDo you seriously think that "word starts with a capital letter" is an easy function to write? I feel like you haven't spent enough time with Unicode.
- Fiaxhs 11y agoreturn string[0] === string[0].toUpperCase(); You're welcome!
- Arnavion 11y agoTIL 1 is a capital letter.
- sspiff 11y agostring[0] === string[0].toUpperCase() && string[0].toUpperCase() != string[0].toLowerCase();
- deadowl 11y agotypeof string == "string" && string.length && string[0] == string[0].toUpperCase() && string[0].toUpperCase() != string[0].toLowerCase();
- sspiff 11y agoMan, Javascript is AWESOME.
- deadowl 11y ago/\p{Lt}/.test(str) would be much more compact, but Unicode properties still aren't available in Javascript regular expressions. It doesn't look like they will be anytime soon. I guess they have some staging system and it's still at stage 0 (https://github.com/tc39/ecma262/blob/master/stage0.md https://github.com/tc39/ecma262/blob/master/stage0.md), grouped with lookbehind assertions, named captures, and mode modifiers.
- lmm 11y agoIf those functions are reusable or complex enough (partly due to deficiencies in javascript the language, sure) then why not?
- pauljohncleary 11y ago>add-1-to-a-number i++
- deadowl 11y agoThe three cases you state aren't nearly as hairy as determining whether something is a positive integer. Someone might do something like ~~n to truncate a float. That works fine until you go above 32 bits. Math.trunc should be used instead. Someone might do something like n >>> 0 === n, and using any bitwise operators will always bake in the assumption that the number is 32 bits. Do you treat negative 0 as a negative integer? Detecting negative 0 is hairy. So, to avoid bad patterns, it makes sense. For is-bigger-than-5(x), x > 5 is not hairy. For add-1-to-a-number(n), n + 1 is not hairy. For word-starts-with-capital-letter(word)? That one is actually pretty hairy. There are programmers that would write a regular expression /^[A-Z]/, or check the charCode of the first character being within the A-Z range, amongst other solutions. An appropriate solution, however, would be (typeof word == "string" && word.length && word[0] == word[0].toUpperCase() && word[0].toUpperCase() != word[0].toLowerCase()), because the toUpperCase and toLowerCase methods are unicode-aware, and presently you can't access unicode properties using Javascript's flavor of regular expressions.
- JdeBP 11y agoPeople have started to point out the existence of modules in this very vein. And, of course, write them. With two such together, you can probably get is-maybe-5 . * https://news.ycombinator.com/item?id=11351905 https://news.ycombinator.com/item?id=11351905 * https://news.ycombinator.com/item?id=11359302 https://news.ycombinator.com/item?id=11359302