开发者

How to avoid code duplication [closed]

开发者 https://www.devze.com 2023-01-28 08:44 出处:网络
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references,or expertise, but this question will likely solicit debate, a
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 10 years ago.

I have three C# methods that almost perform identically. More specifically there is a large amount of code repetition but at the end, they perform different functions.

This is obviously very inefficient, so what's开发者_JS百科 the best way to cut down on code duplication? Should I place it all in one method and use a switch & enum for the different functionality at the end? Alternatively, is there some kind of way you can have 3 separate classes and inherit the shared bit from another? I've done a fair bit of reading about this and from what I can gather, this is only used to gain properties from a whole different class.


Without seeing the code its hard to give a concrete example, but it sounds like you want to pass a delegate instance into the method.


From my point of view you should sit down with a piece of paper and a pen. Draw out every piece of functionality your methods perform. Remember that a function/method should do one and only one thing.

After you brake down every piece of functionality you will start to see things that are repeated, these should be grouped together in one function.


I have three C# methods that largely do identical things. There is much code repetition but at the end, they both perform different functions.

by reading this line , i am thinking your three methods are having different implementation based upon some different scenarios.

so i would suggest to see Stratgey Pattern once which may be of useful. See here


It's a good sign that it bugs you.

This is obviously very inefficient

The inefficiency (a little extra memory usage) is the least important part these days. What's crucuial is that it's a maintainence nightmare. For instance, if you find a bug in the routine and fix it, you need to remember to fix the duplicate code, too. You definitely want to factor out duplicate code when you can.

Alternatively, is there some kind of way you can have 3 separate classes and inherit the shared bit from another? I've done a fair bit of reading about this and from what I can gather, this is only used to gain properties from a whole different class.

A subclass can directly access all public and protected members of it's superclass. For instance, if you had an Animal class with a Poop() method, Dog and Cat subclasses could share that pooping code.

But there are a lot of ways of skinning this cat. Hard to say what's best without more detail.


Can you do something like the following :

    public void MethodBase()
    {
        Console.WriteLine("1");
        Console.WriteLine("2");
        Console.WriteLine("3");
        Console.WriteLine("4");
        Console.WriteLine("5");
    }

    public void Method1()
    {
        MethodBase();
        Console.WriteLine("Method 1");
    }

    public void Method2()
    {
        MethodBase();
        Console.WriteLine("Method 2");
    }

    public void Method3()
    {
        MethodBase();
        Console.WriteLine("Method 3");
    }


There is no solution that would work in all cases, show us the code.

The problem is that when you put the code into one function and switch a little part of the behaviour somewhere inside, you raise the code complexity, and sometimes it’s better to keep some duplicated code than that.

Almost the same argument works for inheritance. You can easily stuff the common code into a common ancestor class, but if you start routinely use inheritance as a code-reuse tool, your design may slip into a world of pain. Inheritance means that code in one piece of code can affect behaviour in other pieces of code and before you know it, you will have an interlinked ball of code that’s almost impossible to change without breaking.

It’s usually best when you can extract or abstract some meaningful common part of the code from those three functions. Something that makes sense, like a high-level pattern, instead of just a few common lines that do no coherent work. A simple rule that may help you is naming the extracted code: Is there an obvious, natural way to name the chunk? Does it obviously fit into one of your classes? If it does, it might be a good solution. If you have hard time naming it and it could go pretty much anywhere, you might want to think a bit more.


You can try to generalize the method by making it a higher-order or a generic function. Other approaches that comes to mind are design patterns like Template method which lets you define the skeleton of the algorithm, and let subclasses redefine certain steps.


public MyCustomObject MethodBase(MyCustomObject myObj) 
{ 
    myObj.Name="FixedName";
    myObj.Surname="FixedSurname";
    myObj.Type = Types.Human;
    return myObj;
} 


public MyCustomObject SetOld(MyCustomObject  myObj) 
{ 
    MethodBase(); 
    myObj.IsOld = True;
    return myObj;
} 

public MyCustomObject SetYoung(MyCustomObject myObj) 
{ 
    MethodBase(); 
    myObj.IsOld = False;
    return myObj;
} 

public MyCustomObject SetIsDead(MyCustomObject myObj) 
{ 
    MethodBase(); 
    myObj.IsDead = True;
    return myObj;
} 
public void MainMethod(enum OperationType)
{
    MyCustomObject myObj = new MyCustomObject();
    switch(OperationType)
    {
        case OperationTypes.Old:
            myObj = SetOld(myObj);
            break;
        case OperationTypes.Young:
            myObj = SetYoung(myObj);
            break;
        case OperationTypes.Dead:
            myObj = SetDead(myObj);
            break;
    }
}


Nobody mentioned lambda-functions, they are must to know for .NET 3.5 developers:

public MyCustomObject SetYoung(MyCustomObject myObj) 
{ 
    GeneralMethod(myObj => myObj.IsOld = False);
    return myObj;
} 

public void GeneralMethod(Func<TSourceObjectType,TResultObjectType> func)
{
    MyCustomObject myObj = new MyCustomObject(); // MyCustomObject implements 'TSourceObjectType'
    TResultObjectType res = func(myObj);
}
0

精彩评论

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