Refactor Switch to Dictionary

by KodefuGuru 20. January 2010 14:48

In my post on Z3, I created an extension method for proving an equation. This extension method used a switch statement to analyze a result and return a string. This is a bad practice: the mapping of one type to another is trapped in code. What if I wanted to move this mapping to a configuration file, a resource file, or a database? Even if I didn’t want to do that, I feel an imperative switch statement isn’t as readable as a declarative statement. Favor Declarative over Imperative. Below is the original code snippet.

public static string Prove(this Context context, Term term)
 {
     context.Push();
     Term not = context.MkNot(term);
     context.AssertCnstr(not);
     switch (context.Check())
     {
         case LBool.False:
             return "valid";
         case LBool.Undef:
             return "unknown";
         case LBool.True:
             return "invalid";
     }
     return null;
 }

When you see case statements simply return or an assign an item, you know you can perform this refactoring. You will create a generic dictionary of both types and use it instead. Here is the refactored example.

public static string Prove(this Context context, Term term)
{
    var checkResults = new Dictionary<LBool, string>
    {
        { LBool.False, "valid" },
        { LBool.Undef, "unknown" },
        { LBool.True, "invalid" }
    };

    context.Push();
    Term not = context.MkNot(term);
    context.AssertCnstr(not);
    
    return checkResults[context.Check()];            
}

The great thing about this is that you have the mappings side by side. Since it is in a dictionary format, it is loadable from another location; the mapping is no longer tied to the code.

Tags: , , , , ,

Kodefu

Comments

1/22/2010 2:48:53 PM #

James Curran

You should probably move checkResults to be a static readonly field member, to avoid rebuilding it every time Prove() is called.

James Curran United States

Add comment




  Country flag

biuquote
  • Comment
  • Preview
Loading



Powered by BlogEngine.NET 1.6.0.0
Theme by Mads Kristensen

Whois KodefuGuru

Chris Eargle

Chris Eargle
.NET Community Champion

LinkedIn Twitter Technorati Facebook

MVP - Visual C#

 

INETA Community Champions
Friend of RedGate
Telerik .NET Ninja
Community blogs & blog posts

I am a #52er


World Map

RecentComments

Comment RSS

Tag cloud

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2010