How to refactor this IRC data analyzer?

0

I have this method monster that has 1500 lines of code full of conditions and cases.

Method (You did not copy the method here because it will demand all the screen and it would be annoying to scroll - The link is fixed to a commit so that future updates do not break it)

From what I saw in these situations, the ideal is to apply the factory pattern. But could it be further simplified?

    
asked by drielnox 04.03.2016 в 13:51
source

1 answer

3

What I notice is that there is a huge case that could be eliminated if you implement something like:

public class ParseData{
    public string nick {get;set;}
    public string msg {get;set;}

} 
interface IIRCParse{
    ParseData Parse();
}

public IRCParse001 : IIRCParse{

    public ParseData Parse()
    {
        //implementacion
    }
}

public IRCParse006 : IIRCParse{

    public ParseData Parse()
    {
        //implementacion
    }
}

//resto implementaciones

Each code is parsed separately in a class, and all inherit from an interface. And you would use it by means of:

Dictionary<string, IIRCParse> ircParses = new Dictionary<string, IIRCParse>(){
    {"001", new IRCParse001()},
    {"006", new IRCParse006()},
    //resto
};

IIRCParse parseInstance = ircParses[ircData[1]]
ParseData data = parseInstance.Parse();

in this way you avoid the% co_of huge% and you can separate each code in a different implementation The challenge is to normalize the parse for all the codes, which return the same set of data as an answer

In the example use a switch but you use some library of IoC (Unity, Ninject, StruncturaMap, autofac, etc) even better, since in this you could map the code to an implementation and apply the resolve to determine that Parser instance applies.

    
answered by 04.03.2016 в 17:43