CodeSOD: Golfing Over a Log

Indirection is an important part of programming. Wrapping even core language components in your own interfaces is sometimes justifiable, depending upon the use cases.

But like anything else, it can leave you scratching your head. Sam found this bit of indirection in a NodeJS application:

var g = {}; g.log = console.log; g.print = g.util.print; g.inspect = g.util.inspect; g.l = g.log; g.i = g.inspect; g.ll = function(val) { g.l(g.i(val)); }

The intent, clearly, is to play a little code golf. g.ll(something) will dump a detailed report about an object to the console. I mean, that's the goal, anyway. Of course, that makes the whole thing less clear, but that's not the WTF.

The rather obvious problem is that this code just doesn't work. g.util doesn't exist, so quite a few of these lines throw errors. They clearly meant to reference the Node module util, which has inspect and print methods. They just slapped a g. on the front because they clearly weren't thinking, or meant to capture it, like g.util = require('util') or similar.

This module is meant to provide a bunch of logging functionality, and it has many many more lines. The only method ever used, from this snippet, is g.l, so if not for the fact that this errors out on the third line, most of the rest of the module would probably work.

Fortunately, despite being in the code base, and despite once having been referenced by other modules in the project, this module isn't actually used anywhere. Of course, it was still sitting there, still announcing itself as a logging module, and lying in wait for some poor programmer to think they were supposed to use it.

Sam has cleaned up the code and removed this module entirely. Who knows what else lurks in there, broken and seemingly unused?

[Advertisement] Utilize BuildMaster to release your software with confidence, at the pace your business demands. Download today!

This post originally appeared on The Daily WTF.

Leave a Reply

Your email address will not be published. Required fields are marked *