140 points · by fogus · 11y ago · 68 comments
[0]: https://github.com/blakeembrey/is-upper-case/blob/master/is-...
https://github.com/blakeembrey/swap-case
https://github.com/blakeembrey/is-upper-case
https://github.com/blakeembrey/title-case
https://github.com/blakeembrey/snake-case
https://github.com/blakeembrey/dot-case
https://github.com/blakeembrey/path-case
https://github.com/blakeembrey/camel-case
https://github.com/blakeembrey/param-case
https://github.com/blakeembrey/upper-case-first
https://github.com/blakeembrey/lower-case
https://github.com/blakeembrey/upper-case
https://github.com/blakeembrey/pascal-case
I could copy-paste from stack overflow, but I would have to maintain/test that function. I'd rather depend on something.
I could depend on a big "string-utils" library, but that would probably carry some useless baggage, and its scope may change or grow over time. Further, maybe it has a competitor module with a vehement following (like lodash vs underscore), which makes my module less appealing to them. This sucks for my module because I just want that one function.
The better alternative is just to depend on the exact, clearly named "to-camel-case" function which is very unlikely to ever change or grow in scope.
If it does change, then you need to integrate & test your dependencies, same as if it were part of your own codebase.
I think this point is often overlooked by the "I'll use someone else's code because I don't want to maintain it" folks. Code that is frozen doesn't rot: if you never change a piece of code and don't change its dependencies, it will continue working. Similarly, usually the reason you'd in-source a library is so you can change it at-will without getting some other maintainer to accept your patches. Most of the time, the effort that you save by using 3rd-party code is exactly equal to the time it would take to write & debug the library to get it to its current state, minus the time spent hunting for, learning, and integrating the library. Both of these are pretty easy to estimate when it's a one-line function; does it take you longer to find and npm install that one-line function than it does you to write a line of code?
They drive modularity through the pattern of change, so their judgement is based on the development, rather than the code itself.
"You want to keep things that change at the same time in the same module. Parts of a system that change rarely should be in different services to those that are currently undergoing lots of churn. If you find yourself repeatedly changing two services together, that's a sign that they should be merged."
Although that article is about services, believe it can be applied to any type of modules.
6,597 downloads in the last day
28,883 downloads in the last week
134,084 downloads in the last month
'use strict';
module.exports = function isObject(x) {
return typeof x === 'object' && x !== null;
};
Frankly, copy/paste is a superior code reuse mechanism at this scale.The part that really blows my mind? This project has had 57 commits to it! That's basically proof that the project management overhead exceeds a sane cost/benefit tradeoff.
The one advantage I see to having borderline-ridiculous packages such as this is that they do One Thing Well [1]. Yes you may copy/paste an isObject() function or roll your own, but then it becomes your responsibility to make sure it works for your project, and arguably far fewer eyes on it when it doesn't. As long as the project's maintainer is dedicated to making this the best way to determine whether a variable is an object, I have no problem using it.
Although, I do find the 57 commits irksome. Most of them are just upgrading dependencies (code style checkers, etc.), "fixing" indentation, and in one case 7 separate tweet-sized diffs to the README on the same day, as if this guy has git commit/push bound to Ctrl+S in his editor.
[1] http://www.catb.org/esr/writings/taoup/html/ch01s06.html
It's an alternative approach to software design which may feel alien if all you've ever used is copy-pasting for code re-use, or relying on large standard libraries and utility grab-bags.
Second, so what? So it's alright to copy and paste the function? It is alright if a coworker wrote the function and presented you with an interface? Would you have a problem if a single module that was "designed in isolation, documented in isolation and can be used in isolation" but was published internally?
Just today I had an issue where the Closure compiler decided my call to (set!) wasn't necessary, and so it removed it, completely breaking my app. The best solution I could find was a work around involving a pretty large let block. I was just happy to get it working, debugging optimized Closure compiled code is not fun at all.
I'm somewhat skeptical about a set! being eliminated unless you were setting a property of an object from a random JavaScript library you haven't provided an extern for. The above compiler settings would have showed this immediately.
This is the method I was dealing with: https://github.com/city41/bookends/blob/master/site/src/cljs...
In that let expression I'm digging my way down into a native JS object to monkey patch it. The equivalent (set! (.. knex -client -Runner -prototype -debug) ...) was being completely removed as far as I can tell. Same with an equivalent aset. That channel no longer got set up and my app stopped working. If I simply put that method back to set!, then it breaks again, very reproducible. When I added in a (.log js/console "hello") to the method, I could see that console log inlined where the method call was, but no other remnant of the method could be found.
I will play with :pretty-print and look into creating a small repro of what I'm seeing. But if I don't succeed with a smaller repro, then at the very least this app is already quite small.
EDIT: here is a little gist showing the difference I'm seeing if anyone is curious: https://gist.github.com/city41/12099b91e0fbb526b0dc
Max Ogden's last comment irks me. After a long and fairly productive discussion, he generalizes / calls out people, and doesn't explicate on what he's trying to point out.
I love the work they're doing, and the community they've fostered might be one of the most progressive in the industry, but they can be insanely pretentious at times.
(irony of responding to a comment about generalization noted)
I do agree with the "modularity shaming" idea though, the JS community does seem to be overzealous about that. However I think it better to err in that direction than the other direction.
For random JS libs Closure can't really do much better than Uglify.
I wish I could use ClojureScript in my day-to-day, but for now it's out of the question. So, for us, AMD [1] has been a god send. We can defer loading other major parts of our UI until the user actually uses that functionality. Doing this on our own would be impossible, but using AMD has really worked well for us.
But, here's to hoping I can actually use ClojureScript at some future point :)
In the 1.8 time frame the jQuery devs made a call for people who wanted to use Closure Compiler's ADVANCED_OPTIMIZATIONS to participate, but just didn't get any community interest. CCAO style is not typical JavaScript and it doesn't seem that a lot of people use it.
goog.require('goog.dom');
var dom = goog.dom;
But that defeats dead-code elimination and makes the compiler include goog.dom in its entirety (the Compiler only goes so far when determining if code is "dead"). So, any use of a goog.dom function will be fully qualified: var el = goog.dom.getElement(id);
Practically, you might use goog.dom.DomHelper instead to keep code compact, and because it stores a reference to the document object you're using.This surprisingly makes ClojureScript really nice for developing with the Closure Library (not that using any library that depends on mutable objects is particularly nice in Clojure), because you can write code like the following, and still benefit from dead code elimination:
(ns whatever.ns
(:require [goog.dom :as dom]))
;; ...
(let [el (dom/getElement id])
The Closure Library is impractical without dead code elimination because it includes so much functionality, and is not meant to be used as a single script dependency.This way, libraries such as JQuery, could be cached, and shared between websites.
besides writing it yourself (cue contractors going this is the only viable route) reuse and dead code elimination are necessary foundations to the next level of web apps for productivity of dev and responsive runtimes.
Browser still has to parse+run all of that JS though. Because JS-in-browsers is antimodular in that all previous JS on a given page influences all subsequently loaded JS, even if using just script tags with a URL. In a way this is very similar to the pains experienced by the C++ community for many years now.
#ifndef _SOME_FILE_H #define _SOME_FILE_H // code goes here #endif
Also any "module" can define any global variable, so name clashes (esp. with C libraries) are possible. Most languages with proper modules the "global" variables are module-wide.
An equally nasty issue happens with JS, where many libraries must be loaded in the proper order and not duplicated. There are a bunch of module systems but none is part of the spec. The good thing is that module systems are easy to make thanks to closures.
[1]: https://www.youtube.com/watch?v=4Xo9iH5VLQ0
[2]: http://www.open-std.org/Jtc1/sc22/wg21/docs/papers/2014/n404...
On the other hand, the consistent dedication to writing tiny libraries in JavaScript ecosystem is not a bug. Tree-shaking compilers make it somewhat harder to tell where the bloat is coming from, and that encourages people writing libraries to get sloppy.