CodeSOD: Switching Daily

This post was originally published on this site

The Daily WTF

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,

To read the full article click on the 'post' link at the top.