Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.
public boolean checkNameStartsWith(List<Foo> foos) {
    for (Foo foo : foos) {
        if (!(foo.getName().startsWith("bar"))) {
            return Boolean.FALSE;
        }
    }
    return Boolean.TRUE;
}

There are a number of different ways to do this, what do people prefer and why?

share|improve this question

5 Answers

I'd change Boolean.FALSE to false and Boolean.TRUE to true. Using the constants objects from Boolean is rather unnecessary since the method returns with primitive boolean.

share|improve this answer
I had got into the habit of always referencing the static constants as consumes less memory. Other people don't then ? – NimChimpsky Nov 7 '12 at 11:04
5  
a reference to an object consuming less memory then a primitive? Do you have tests verifying that? Even if it is true, did you actually encounter a case where it did matter? – Jens Schauder Nov 7 '12 at 11:19
@NimChimpsky: Using Boolean.FALSE and TRUE is better than creating new objects with new Boolean(false) but here the return type is boolean primitive, not an object reference. – palacsint Nov 7 '12 at 11:27
@JensSchauder a reference to lots and lots of primitives across my whole application, consumes less memory than one Object reference, was my thinking. – NimChimpsky Nov 7 '12 at 11:39
6  
there are no references to primitives. Just references or primitives. In case of a local boolean the difference surely is neglectable. If you have huge volumes of those (like huge Arrays) one should do some tests. For everything else, use the one easiest on the eye. – Jens Schauder Nov 7 '12 at 11:49

Though for loop is enough, you can rewrite using Guava if you like lambdas:

public boolean checkNameStartsWith(List<Foo> foos){
    return Iterables.all(foos, new Predicate<Foo>() {
        public boolean apply(Foo foo) {
            return foo.getName().startsWith("bar");
        }
    });
}
share|improve this answer

In addition to the other answers, I suggest adding a second parameter to the checkNameStartsWith method named prefix, e.g.

public boolean checkNameStartsWith(List<Foo> foos, final String prefix) {
    for (Foo foo : foos) {
        if (!foo.getName().startsWith(prefix)) {
            return false;
        }
    }
    return true;
}
share|improve this answer

I know it probably doesn't matter from a performance standpoint (due to branch prediction and compiler optimization), but I still prefer this form:

public boolean checkNameStartsWith(List<Foo> foos) {
    for (Foo foo : foos) {
        if (foo.getName().startsWith("bar")) continue;

        // Mismatch found
        return false; 
    }
    return true;
}

I think it's because it makes it easier to see that you're absolutely done with the iteration once you've determined there's no match. Otherwise, you might have something like this:

public boolean myMethod(List<Foo> foos, final String prefix) {
    for (Foo foo : foos) {
        if (!foo.getName().startsWith(prefix)) {
            // do something complex (many lines of code)
        }
        // here you could possibly do something else, regardless of condition true/false
    }
    return true;
}

You don't know unless you search for the end of the loop whether something is still done if the condition is false.

share|improve this answer

Boolean.FALSE and Boolean.TRUE should be definitely replaced with true and false because of auto-boxing. You don't need an additional Integer.intValue() call for each invocation.

I'm pretty sure Oracle HotSpot will optimize this automatically, but the code readability still suffers.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.