let’s do the timewaste again – or just another example on how some popular JavaScript libraries or frameworks freak me out

Let’s talk about code quality and about quality of documentation. One example. I’m using underscore. So there is ‘findWhere’ on collections. All you can get is “Just like where, but directly returns only the first model in the collection that matches the passed attributes.”

Ok, fine, you might say, but what if it doesn’t find an element? It’s not like this never happened before. Shouldn’t that ‘rare’ case be covered in the documentation? Given that ‘where’ returns an array and that might be empty, the case what’s the first element then could be quite interesting.

So let’s look at the implementation:

_.findWhere = function(obj, attrs) {
    return _.find(obj, _.matcher(attrs));

Oooookay. Didn’t the docs say it’s like ‘where’? It starts to get confusing here. What does ‘where’ look like then?

_.where = function(obj, attrs) {
    return _.filter(obj, _.matcher(attrs));

Doesn’t look like almost the same thing to me. Anyway, leave that behind, let’s proceed. What does ‘find’ look like, you might ask my dear friend?

 _.find = _.detect = function(obj, predicate, context) {
    var keyFinder = isArrayLike(obj) ? _.findIndex : _.findKey;
    var key = keyFinder(obj, predicate, context);
    if (key !== void 0 && key !== -1) return obj[key];

WTF! Even my linter flashes the code all red at that place already: “anonymous function does not always return a value”. Yes, the function does not return anything in case no element is found. I know, ‘fuck types’ you might think, but no, fuck you and your laziness. This is just crap and the opposite of what’s good. Code quality, I haz it! #not

Comments are closed