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.

//thedailywtf.com/images/highlight/highlight.pack.js
hljs.initHighlightingOnLoad();

[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.


CodeSOD: Switching Daily syndicated from https://koresol.wordpress.com/

Author: NOVATTA

Thank you for visiting NOVATTA. My name is Phillips. I am an Internet solutions specialist and the man behind this idea. NOVATTA is a technology and how-to group. We discuss open source projects and review web technology, its business and culture and also provide breaking tech news.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: