开发者

Refactoring switch statement for Data to different types of data

开发者 https://www.devze.com 2023-01-27 21:23 出处:网络
My mission is to refactor a switch statement that was poorly written (it makes the cyclomatic complexity spike).In short, there is a class tha开发者_JAVA百科t parses a file for various values.

My mission is to refactor a switch statement that was poorly written (it makes the cyclomatic complexity spike). In short, there is a class tha开发者_JAVA百科t parses a file for various values.

class foo
{
    //a sampling of the fields.  Each have their appropriate property
    private string _name;
    private short _location;
    private int _lineNumber;
    private List<string> _siblings;

    internal foo (StreamReader reader)
    {
        _siblings = new List<string>()
        while (!reader.EndofFile)
        {
            switch (reader.ReadLine())
            {
               case "Name":
                  _name = reader.ReadLine();
                  break;
               case "Location":
                  _location = short.Parse(reader.ReadLine());
                  break;
               case "Line Number":
                  _lineNumber = int.Parse(reader.ReadLine());
                  break;
               case "Brother":
               case "Sister":
                  _siblings.Add(reader.ReadLine());
                  break;
               //etc
            }
        }
    }
    //Other methods and such
}

I have read up on the topic and while there seems to be plenty of help, it all seems to be pointing at the Strategy design pattern, which (I believe) would overkill my problem. In my project, there are multiple classes like this, with some of them having upwards of 25 case statements (so kudos to those who can come up with an idea of and interface or abstract class)

I have thought about using a Dictionary<String, TValue> as described by John Sonmez, but then what would TValue be?

Any help would be greatly appreciated.


First of all, reader.ReadLine() is really not part of the switch statement here so I would advise that you just read lines two by two and pass to another class to handle. (first line seems to define what it is and second has the value).

Your handler will contain the action. If you do not want to use Strategy - which is easy and perhaps you should - have the value of the Dictionary as delegates each implementing a strategy:

 Dictionary<string, Action<string>> dic = new Dictionary<string, Action<string>>();
 dic.Add("Father", ((x)=> // somthing);
 dic.Add("Brother", ((x)=> // somthing);
 dic.Add("Sister", ((x)=> // somthing);


Two options.

If there was a convention that the data read from the line matched the name of a property, you could by convention populate the property via reflection. Alternatively, you could use an attribute on the property that corresponded to the expected value you would read from the file.

Hope that helps or at least points you in the right direction :)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号