Welcome to MSDN Blogs Sign in | Join | Help

Instead of a simple switch statement ...

This is Luke's kind of code. I might be catching the virus ...

abstract class QIFParserBase {

    public enum LoadOptions {
        All,
        Prices,
        Securities,
        Transactions
    }

    static readonly Dictionary<LoadOptions, Action<QIFParserBase, string[]>> parseFuncs =
                                        new Dictionary<LoadOptions, Action<QIFParserBase, string[]>> {

        {LoadOptions.All, (q,c) => q.ParseAll(c)},
        {LoadOptions.Prices, (q,c) => q.ParsePricesBlocks(c)},
        {LoadOptions.Securities, (q,c) => q.ParseSecurityBlocks(c)},
        {LoadOptions.Transactions, (q,c) => q.ParseTransactionBlocks(c)}
    };

    public QIFParserBase(string fileName, LoadOptions opt) {

        string content = File.ReadAllText(fileName);

        string[] blocks = content.Split(new string[] { "!Type:", "!Option:" },
                                                        StringSplitOptions.RemoveEmptyEntries);

        parseFuncs[opt](this,blocks);
    }
Published Friday, August 31, 2007 1:54 PM by lucabol
Filed under:

Comments

# re: Instead of a simple switch statement ...

It looks so silly when someone /else/ does it....

Friday, August 31, 2007 5:16 PM by tfries

# re: Instead of a simple switch statement ...

Yes, let the functions flow through you. Add a bit of aliasing and easier delegate syntax and succinctness incarnate.

Friday, August 31, 2007 5:35 PM by MichaelGiagnocavo

# re: Instead of a simple switch statement ...

This is from python 101, except without all the <><<type>> business; It's an excellent way to seperate code from data.

Friday, August 31, 2007 11:29 PM by Bill Mill

# cute but unmaintainable

Please don't put that into production code.  This type of cute code will serve to hide the actual functionality or business reasons behind the code. It will greatly increase the cost to support and maintain production code.      It is simple when presented out of context here but when combined with dozens of similar approaches in a production system, it is unmaintainable.   I've seen this in three different environments C with structs of function pointers, C++ and C#.  

Monday, September 17, 2007 10:07 AM by Greg

# re: Instead of a simple switch statement ...

This looks a bit of a scripting solution. I'm not sure I like it. OK, it's a cool programming technique, but does it solve a problem?

In comparison to a switch statement:

1) The code executes a bit slower

2) It adds a layer of indirection, so it's more complex, thus harder to read

3) "It's an excellent way to seperate code from data". Is it?

The enum, and the Dictionary are still seperate... I would like to have java enums in c#. That way, you can combine the enum-items with the specific delegate.

That way, the code is shorter, one (very small) step faster than the code above, but above all: it's easier to understand (provided, you know the syntax of course).

http://java.sun.com/j2se/1.5.0/docs/guide/language/enums.html

Tuesday, September 18, 2007 8:53 AM by Doeke Zanstra
New Comments to this post are disabled
 
Page view tracker