0

Is it worth to make better code (ClassB) instead of going with two separate check methods? So main question is: am I right when assuming that Class A is a bit faster than Class B, or is its difference doesn't really matter and passing variables (List in this situation) doesn't really affect productivity, even if these lists had 1000 objects each? Sorry if this question is dumb.

 public class ClassA implements Runnable {

    ArrayList<Obj> list1;
    ArrayList<Obj> list2;

    boolean checkList1() {
        for (Obj str : list1) {
            if (str.check()) {
                return true;
            }
        }
        return false;
    }

    boolean checkList2() {
        for (Obj str : list2) {
            if (str.check()) {
                return true;
            }
        }
        return false;
    }

    @Override
    public void run() {
        checkList1();
        checkList2();
    }
}

OR

 public class ClassB implements Runnable {

    ArrayList<Obj> list1;
    ArrayList<Obj> list2;

    boolean checkAnyList(ArrayList<Obj> list) {
        for (Obj str : list) {
            if (str.check()) {
                return true;
            }
        }
        return false;
    }

    @Override
    public void run() {
        checkAnyList(list1);
        checkAnyList(list2);
    }

}
abvgde
  • 41
  • 6
  • A comment next to that : the list is *checked* if ONLY one element is *checked* ? or you need to have ALL elements of the list checked ? – azro Aug 22 '17 at 13:31
  • 1
    If you think how many items are in the list matters for how long it takes to pass it to a function, you might want to read through [Is Java “pass-by-reference” or “pass-by-value”?](https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value?rq=1) and/or read a book on Java. – Bernhard Barker Aug 22 '17 at 13:47
  • This is plain example, but yes, it was intended to break cycle if check() returns true. Ok, I get it: having a "getter" method is equivalent to just accesing public value as well as passing vars equals to accesing fields in terms of performance speed. – abvgde Aug 22 '17 at 14:30

5 Answers5

1

You are doing the same thing so there is no difference.

NikNik
  • 2,191
  • 2
  • 15
  • 34
1

Doing the same thing.So there is no difference

P S M
  • 1,121
  • 12
  • 29
1

The main point is code duplication - you are doing the exact same thing twice, plz don't. Whenever you will have to change something - there are at least two places you might need to that - easy to forget.

The more interesting thing is that ultimately the second approach could be faster, since there is a single method that the JVM JIT has to optimize; let's say inline it.

Eugene
  • 117,005
  • 15
  • 201
  • 306
1

You could test that yourself but in the end the difference is next to non-existant so I'd go with the easier to read and maintain version (which I'd say would be version 2).

As of Java 8 that could be something like this as well:

//Note that this doesn't match your code exacly but you should get what I mean.
//Since you didn't provide any hint on how you'd use the return values of your methods I'm working on assumptions here.
boolean result = list1.stream().anyMatch(o -> o.check() ) &&
                 list2.stream().anyMatch(o -> o.check() ) ;

A probably better variant, since refactoring wouldn't need you to change multiple predicates, might be this:

//Changes to how you'd check your elements would just require you to change this predicate.
Predicate<? super Obj> predicate = o -> o.check();
boolean result = list1.stream().anyMatch( predicate ) &&
                 list2.stream().anyMatch( predicate ) ;
Thomas
  • 87,414
  • 12
  • 119
  • 157
1

In the end, you should just measure it. For that, you can use a high precision timer like System.nanoTime()

Without measuring I would prefer classB. The following issues make classB more interesting

  • avoiding nearly duplicated code
  • more flexible when introducing more lists
  • JIT compiler improves code depending on the number of calls. So the method in classB reaches much earlier the threshold to improve.
  • it is not likely that a static available list is faster than a referenced one. The little advantage of classA not to pass a list, does only save a very little startup time.

Just wait until I get real figures....

stefan bachert
  • 9,413
  • 4
  • 33
  • 40