user5515846
user5515846

Reputation:

How can I avoid code duplication by using OOP or SOLID principles?

I have the following class and interface for him:

public class A // int wrapper
{
    private int _a;

    public A(int a)
    {
        _a = a;
    }
}

interface IProgram
{
    int a();

    A b();
}

public class Program : IProgram
{
    public int a()
    {
       int b = 0;
       b++;
       return b;
    }

    public A b()
    {
        int b = 0;
        b++;
        return new A(b);
    }
}

Two methods do the same thing: just increase b.

How can I avoid code duplication and change my interface?

Upvotes: 0

Views: 145

Answers (1)

Tom Clelford
Tom Clelford

Reputation: 711

Create a private method which iterates int b and returns it, then call that method from a() and b()

Upvotes: 1

Related Questions