Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Architecturally speaking, how should I replace an extremely large switch statement with something more manageable?

EDIT 1: Forgot to add the nested property curve ball.

UPDATE: I have chosen @mtazva's answer as that was the preferred solution for my specific case. In retrospect, I asked a general question with a very specific example and I believe that ended up confusing everyone (or maybe just me) as to what the question was exactly. I do believe the general question has been answered as well (see the Strategy pattern answers and links). Thanks everyone!

Large switch statements obviously smell and I have seen some links on how you could do this with a dictionary that maps to functions. But I'm wondering if there is a better (or smarter way) to do this? In a way, this is a question I've always sort of had rolling around in the back of my head but never really had a good solution to.

This question stemmed from another question I asked earlier: How to select all the values of an object's property on a list of typed objects in .Net with C#

Here is an example class I'm working with (from an external source):

public class NestedGameInfoObject
{
    public string NestedName { get; set; }
    public int NestedIntValue { get; set; }
    public decimal NestedDecimalValue { get; set; }
}

public class GameInfo
{
    public int UserId { get; set; }
    public int MatchesWon { get; set; }
    public long BulletsFired { get; set; }
    public string LastLevelVisited { get; set; }
    public NestedGameInfoObject SuperCoolNestedGameInfo { get; set; }
    // thousands more of these
}

Unfortunately, this is coming from an external source... imagine a HUGE data dump from Grand Theft Auto or something.

And I want to get just a small cross section of a list of these objects. Imagine we want to be able to compare you with a bunch of your friends' game info objects. An individual result for one user would look like this:

public class MyResult
{
    public int UserId { get; set; }  // user id from above object
    public string ResultValue { get; set; }  // one of the value fields from above with .ToString() executed on it
}

And an example of what I want to replace with something more manageable (believe me, I DON'T want to be maintaining this monster switch statement):

const int MATCHES_WON = 1;
const int BULLETS_FIRED = 2;
const int NESTED_INT = 3;

public static List<MyResult> GetMyResult(GameInfo[] gameInfos, int input)
{
  var output = new List<MyResult>();

  switch(input)
  {
    case MATCHES_WON:
        output = gameInfos.Select(x => new MyResult()
         {
            UserId = x.UserId, 
            ResultValue = x.MatchesWon.ToString()
         }).ToList<MyResult>();
      break;

    case BULLETS_FIRED:
        output = gameInfos.Select(x => new MyResult()
         {
            UserId = x.UserId, 
            ResultValue = x.BulletsFired.ToString()
         }).ToList<MyResult>();
      break;

    case NESTED_INT:
        output = gameInfos.Select(x => new MyResult()
         {
            UserId = x.UserId, 
            ResultValue = x.SuperCoolNestedGameInfo.NestedIntValue.ToString()
         }).ToList<MyResult>();
      break;

    // ad nauseum
  }

  return output;
}

So the question is are there any reasonable ways to manage this beast? What I'd really like is a dynamic way to get this info in case that initial object changes (more game info properties are added, for instance). Is there a better way to architect this so it's less clumsy?

like image 267
longda Avatar asked Sep 13 '11 04:09

longda


People also ask

What can I use instead of a switch statement?

Luckily, JavaScript's object literals are a pretty good alternative for most switch statement use-cases I can think of. The idea is to define an object with a key for each case you would have in a switch statement. Then you can access its value directly using the expression you would pass to the switch statement.

What can be use instead of switch-case in C++?

In this case, you could also use std::array<int, 5> , which, unlike std::vector<int> , can be constexpr .

How do I change a switch-case?

Start by moving the plastic pieces from one shell to the new shell and screwing them in. Place the new shell on the back of the Switch itself, and screw it down in the reverse order you removed the screws. Ensure they are all in tight and you haven't missed any screws, then turn on your Switch and give it a whirl.


2 Answers

I think your first sentence eluded to what is probably the most reasonable solution: some form of dictionary mapping values to methods.

For example, you could define a static Dictionary<int, func<GameInfo, string>>, where each value such as MATCHES_WON would be added with a corresponding lambda that extracts the appropriate value (assuming your constants, etc are defined as shown in your example):

private static Dictionary<int, Func<GameInfo, string>> valueExtractors =
    new Dictionary<int, Func<GameInfo, string>>() {
        {MATCHES_WON,   gi => gi.MatchesWon.ToString()},
        {BULLETS_FIRED, gi => gi.BulletsFired.ToString()},
        //.... etc for all value extractions
    };

You can then use this dictionary to extract the value in your sample method:

public static List<MyResult> GetMyResult(GameInfo[] gameInfos, int input)
{
  return gameInfo.Select(gi => new MyResult()
         {
            UserId = gi.UserId, 
            ResultValue = valueExtractors[input](gi)
         }).ToList<MyResult>();
}

Outside of this option, you could potentially have some sort of file/database/stored lookup with the number and the property name, then use reflection to extract the value, but that would obviously not perform as well.

like image 175
mtazva Avatar answered Sep 28 '22 06:09

mtazva


I think this code is getting out of hand a bit. You're effectively using constants to index properties - and this is creating fragile code that you're looking to use some technique - such as - reflection, dictionaries, etc - to control the increased complexity.

Effectively the approach that you're using now will end up with code like this:

var results = GetMyResult(gameInfos, BULLETS_FIRED);

The alternative is to define an extension method that lets you do this:

var results = gameInfos.ToMyResults(gi => gi.BulletsFired);

This is strongly-typed, it doesn't require constants, switch statements, reflection, or anything arcane.

Just write these extension methods and you're done:

public static class GameInfoEx
{
    public static IEnumerable<MyResult> ToMyResults(
        this IEnumerable<GameInfo> gameInfos,
        Func<GameInfo, object> selector)
    {
        return gameInfos.Select(gi => gi.ToMyResult(selector));
    }

    public static MyResult ToMyResult(
        this GameInfo gameInfo,
        Func<GameInfo, object> selector)
    {
        return new MyResult()
        {
            UserId = gameInfo.UserId,
            ResultValue = selector(gameInfo).ToString()
        };
    }
}

Does that work for you?

like image 23
Enigmativity Avatar answered Sep 28 '22 08:09

Enigmativity