Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoiding code duplication -- best approach

I've got two methods with the same list and types of arguments and almost the same body but each of them calls another function to fetch list of elements. To be more precise:



    public void method1 (int a, int b) {
            //body (the same in both of methods)
            List<SomeObject> list = service.getListA(int c, int d);
            //rest of the body (the same in both of methods)
        }

        public void method2 (int a, int b) {
            //body (the same in both of methods)
            List<SomeObject> list = service.getListB(int c, int d, int e);
            //rest of the body (the same in both of methods)
        }

What is the best approach to the problem avoiding code duplication in that case? I thought about Strategy pattern, but there is a problem with difference in argument list.

UPDATE:



    public void method1 (int a, int b) {
            //body (the same in both of methods)
            int c = some_value;
            List<SomeObject> list = service.getListA(a, b, c);
            //rest of the body (the same in both of methods)
        }

        public void method2 (int a, int b) {
            //body (the same in both of methods)
            int c = some_value;
            int d = another_value;
            List<SomeObject> list = service.getListB(a, b, c, d);
            //rest of the body (the same in both of methods)
        }

So some variables are local and some are passed through arguments.

like image 245
Lukas Chojnacki Avatar asked Jan 16 '13 22:01

Lukas Chojnacki


People also ask

How inheritance helps prevent code duplication?

Inheritance is one of the key concepts of object orientated programming (OOP). Objects are created using classes, but that's not all. A class can inherit the methods and variables from another class. This principle helps us to avoid duplicate code.

What refactoring can be used when a method is too long has duplicated code?

If the duplicate code is inside a constructor, use Pull Up Constructor Body. If the duplicate code is similar but not completely identical, use Form Template Method. If two methods do the same thing but use different algorithms, select the best algorithm and apply Substitute Algorithm.

How do you avoid code duplication in C++?

The conventional approach to reduce this kind of code duplication is to move the common code to a member function, which can be called from all the constructors. Usually, that member function is called init.

How does object Oriented Development eliminate duplication?

The idea behind object oriented programming is to reduce code duplication by encouraging reuse. And inheritance allows us to reuse code by moving common, core elements into base classes that are extended by objects that do similar jobs.


1 Answers

Factor them out into additional methods.

public void method1 (int a, int b) {
        MyClass myClass = method3(a, b);
        List<SomeObject> list = service.getListA(myClass.getC(), myClass.getD());
        method4(list);
}

public void method2 (int a, int b) {
        MyClass myClass = method3(a, b);
        List<SomeObject> list = service.getListB(myClass.getC(), myClass.getD(), myClass.getE());
        method4(list);
}

public MyClass {
    private final int c;
    private final int d;
    private final int e;
    ...
}

public MyClass method3(int a, int b) {
    // body
    return new MyClass(c, d, e)
}

public void method4(List<SomeObject> list) {
    // rest of body
}
like image 140
Alex DiCarlo Avatar answered Sep 22 '22 05:09

Alex DiCarlo