Eliminating a Switch statement

problemsolving, programming


I was solving Make the Deadfish Swim on CodeWars, which seemed to require many if or switch (conditional) statements.

I was impressed with two Functional Programming in Javascript articles (Rethinking JavaScript: The if statement & Rethinking JavaScript: Eliminate the switch statement for better code), by Joel Thoms so I wanted to give it a try without conditional statements.

What was involved to solve the question without switch?


Use a dictionary & a lambda expression


Make the Deadfish Swim is about parsing a string of commands and perform operations on an initial value of 0.

Write a simple parser that will parse and run Deadfish. Deadfish has 4 commands, each 1 character long. 'i' will increment the value ( initially 0 ). 'd' will decrement the value. 's' will square the value. 'o' will output the value into the return array.

Invalid characters should be ignored.

Deadfish.Parse("iiisdoso") => new int[] {8, 64};

At a glance, a simple ifs or a  switch would be required to solve it.

Let's implement it using a switch statement.

"With" Switch


Iterative version matches against each command and performs operations.

cmd is a character in data, which is "iiisdoso" in the question.

The code iterates each command and operate on the sum.

How do we get rid of the switch statement?

We want to rid switch and there are two aspects to consider.

  1. What'd be a good Data Structure (DS) for matching and getting a value?
  2. How do  operate on a value?

For the first question, a hashtable/dictionary is a good fit because the time complexity for a key lookup is O(1).

For the latter, a lambda/anonymous function would do the job. (I am using C#, which provides Func<T, TResult>).

We all the pieces in place, let's look at the version without the switch statement.

"Without" Switch


The first thing I did was to create a dictionary, commands for a fast lookup.

And for the operation (values of commands), a lambda expression (which accepts an integer and returns an integer) is used.

data operates on each character (command) and operates on the valueusing Select LINQ statement.


First of all, why would I want to eliminate switch?

Instead of instructing how a problem needs to be solved, FP requires you to think in terms of what to solve.

I could've gone the whole nine yards and used a Command Design Pattern but it seemed like Object Oriented Programming trying to emulate Functional Programming (FP).

And also the problem is small enough to dabble with FP.

Parting Words

I am just getting started with FP way of writing programs in C# and started using LINQ heavily.

Still wrapping my head around on FP so I'd appreciate it if you can provide me pointers on how to go further with FP.

The source code is available on GitHub.