Article 58P46 CodeSOD: A Poor Facsimile

CodeSOD: A Poor Facsimile

by
Remy Porter
from The Daily WTF on (#58P46)

For every leftpad debacle, there are a thousand utility belt" libraries for JavaScript. Whether it's the venerable" JQuery, or lodash, or maybe Google's Closure library, there's a pile of things that usually end up in a 50,000 line util.js file available for use, and packaged up a little more cleanly.

Dan B had a co-worker who really wanted to start using Closure. But they also wanted to be abstract", and loosely coupled", so that they could swap out implementations of these utility functions in the future.

This meant that they wrote a lot of code like:

initech.util.clamp = function(value, min, max) { return goog.math.clamp(value, min, max);}

But it wasn't enough to just write loads of functions like that. This co-worker knew" they'd need to provide their own implementations for some methods, reflecting how the utility library couldn't always meet their specific business needs.

Unfortunately, Dan noticed some of those reimplementations" didn't always behave correctly, for example:

initech.util.isDefAndNotNull(null); //returns true

Let's look at the Google implementations of some methods, annotated by Dan:

goog.isDef = function(val) { return val !== void 0; // tests for undefined}goog.isDefAndNotNull = function(val) { return val != null; // Also catches undefined since ==/!= instead of ===/!== allows for implicit type conversion to satisfy the condition.}

Setting aside the problems of coercing vs. non-coercing comparison operators even being a thing, these both do the job they're supposed to do. But Dan's co-worker needed to reinvent this particular wheel.

initech.util.isDef = function (val) { return val !== void 0; }

This code is useless, since we already have it in our library, but whatever. It's fine and correct.

initech.util.isDefAndNotNull = initech.util.isDef; 

This developer is the kid who copied someone else's homework and still somehow managed to get a failing grade. They had a working implementation they could have referenced to see what they needed to do. The names of the methods themselves should have probably provided a hint that they needed to be mildly different. There was no reason to write any of this code, and they still couldn't get that right.

Dan adds:

Thankfully a lot of this [functionality] was recreated in C# for another project.... I'm looking to bring all that stuff back over... and just destroy this monstrosity we created. Goodbye JavaScript!

proget-icon.png [Advertisement] Keep the plebs out of prod. Restrict NuGet feed privileges with ProGet. Learn more. TheDailyWtf?d=yIl2AUoC8zAGblZS9YDgQQ
External Content
Source RSS or Atom Feed
Feed Location http://syndication.thedailywtf.com/TheDailyWtf
Feed Title The Daily WTF
Feed Link http://thedailywtf.com/
Reply 0 comments