CodeSOD: Switching Daily
A not uncommon pattern is to use a dictionary, or array as a lookup table in place of a switch or conditional. In some languages, like Python, there is no switch statement, and dictionaries are the main way to imitate that behavior.
In languages like JavaScript, where the line between objects and dictionaries is blurred to the point of non-existence, it's a common approach. A lot of switch statements can be converted to an object literal with functions as its values, e.g.:
var myVal = getMyVal();let lookup = {'foo': doFoo, 'bar': doBar};lookup[myVal]();
Cassi had a co-worker who was at least peripherally aware of this technique. They might have heard about it, and maybe they saw it used once, from a distance, on a foggy day and their glasses were covered with rain and they also weren't actually paying attention.
When they needed to convert a day, represented as a number from 1-7, into a day-as-a-string, they wrote some code which looks like this:
/** * Converts the given weekday number (Sunday = 1) to the weekday name * @param {number} dayNum * @return {string} */$company.time.dayNumToDayName = function(dayNum) {return /** @type {string} */ ($company.misc.shortSwitch(dayNum, [[1, 'Sunday'],[2, 'Monday'],[3, 'Tuesday'],[4, 'Wednesday'],[5, 'Thursday'],[6, 'Friday'],[7, 'Saturday']]));};
We'll get to shortSwitch in a moment, but let's just look at how they designed their lookup table. I'd say that they've reinvented the dictionary, in that these are clearly key/value pairs, but since the keys are indexes, what they've really done is reinvented the array so that they can have arrays which start at one.
Yes, it would be simpler to have just done return listOfDayNames[dayNum - 1], but with some bounds checking. Maybe that's what $company.misc.shortSwitch does, somehow?
/** * Simulates a switch statement (with fewer lines of code) by looping through * the given cases and returning the value of the matching case. * @template C * @template V * @param {C} switchVal - the value to switch on * @param {Array<(C|V)>!} cases - an array of [case, value] pairs * @return V */$company.misc.shortSwitch = function (switchVal, cases) {for(var i = 0; i < cases.length; i++) {if(cases[i][0] === switchVal) return cases[i][1];}};
Simulates a switch statement (with fewer lines of code), they say. In reality, they've simulated a lookup table with more lines of code, and as a bonus, this needs to inspect every element (until it finds a match), versus a lookup table which only needs to directly access the match.
Whether or not Cassi fixes the dayNumToDayName method, that's a small thing. As you can imagine, when someone writes a hammer like shortSwitch, that's gonna be used to turn everything into a nail. shortSwitch is called all over the codebase.
I can only assume/hope that Cassi will be switching jobs, shortly.
[Advertisement] Forget logs. Next time you're struggling to replicate error, crash and performance issues in your apps - Think Raygun! Installs in minutes. Learn more.