Simple boolean gate with two TriState inputs As part of a code review I came across this isDummy(a,b)-Method and the question, whether it is more readable as a conjunction, or a disjunction:

Original snippet:

public static boolean isDummy(TriState a, TriState b) {
    return (a.is(ZERO) || b.is(ZERO)) &amp;&amp; (a.is(NULL) || b.is(NULL));
}


Suggested change:

public static boolean isDummy(TriState a, TriState b) {
    return a.is(ZERO) &amp;&amp; b.is(NULL) || a.is(NULL) &amp;&amp; b.is(ZERO);
}


Both of the above return true if one TriState is ZERO and the other is NULL.

Both do exactly the same; however, the latter imho clearly states, that just two possible combinations are a dummy.
The first one could - without knowing that ZERO is not a VALUE and TriState#is(TriState) does an equality check - possibly suggest, that four possible combinations are dummy values.

As you can see in TriState's constructor the author's intention was to implement a helper mechanism to check Doubles for its "valueness". Infinity and NaN aren't taken into account:

enum TriState {
    NULL, ZERO, VALUE;

    public static TriState of(Double number) {
        if (number == null) {
            return TriState.NULL;
        } else if (number == 0d) {
            return TriState.ZERO;
        } else {
            return VALUE;
        }
    }

    private boolean is(TriState other) {
        return this == other;
    }

    /**
    * Tests if this object is {@link #VALUE} and the given TriState is not {@link #VALUE}.
    *
    * @param other the TriState to test against
    * @return true if this object is {@link #VALUE} AND the given TriState is not {@link #VALUE}. False otherwise.
    */
    public boolean andNot(TriState other) {
        return this.is(VALUE) &amp;&amp; !other.is(VALUE);
    }

    /**
    * Compares two TriState objects to find out if exactly one is ZERO and exactly one is NULL. This method is commutative.
    *
    * @param a a TriState instance to check
    * @param b a TriState instance to check
    * @return true, if one of the given TriState objects is {@link #ZERO} while the other is {@link #NULL}. False otherwise.
    */
    public static boolean isDummy(TriState a, TriState b) {
        return (a.is(ZERO) || b.is(ZERO)) &amp;&amp; (a.is(NULL) || b.is(NULL));
    }
}


While the mere existence of that Enum instead of just using Double instances directly is a whole different matter, I would be interested if there are any known best practices regarding that boolean expression.

Which of the both is easier to understand?
The readability can be improved by using explanatory variables, (of course changing the variable names as they make sense to you).

public static boolean isDummy(TriState a, TriState b) {
    boolean oneOfTheInputsIsZero = a.is(ZERO) || b.is(ZERO);
    boolean oneOfTheInputsIsNull = a.is(NULL) || b.is(NULL);
    return oneOfTheInputsIsZero &amp;&amp; oneOfTheInputsIsNull;
}


First one makes it obvious that the function is commutative, in the second one you have to check the LHS and RHS are indeed the mirror image of each other.

Although the two functions may evaluate the same thing they don't say the same thing.
First one says "There is at least one zero and there is at least one null."
Second one says "There is one zero and the other one is null." (which matches the javadoc)

But the comments sometimes also lie. To check the veracity of the comment consider how would you write the function if it took more parameters.

They still say what they say before but now they don't evaluate the same coincidentally.

public static boolean isDummy(TriState a, TriState b, TriState c) {
    return (a.is(ZERO) || b.is(ZERO) || c.is(ZERO))
        &amp;&amp; (a.is(NULL) || b.is(NULL) || c.is(NULL));
}


public static boolean isDummy(TriState a, TriState b, TriState c) {
    return a.is(ZERO) &amp;&amp; b.is(NULL) &amp;&amp; c.is(NULL) 
        || a.is(NULL) &amp;&amp; b.is(ZERO) &amp;&amp; c.is(NULL) 
        || a.is(NULL) &amp;&amp; b.is(NULL) &amp;&amp; c.is(ZERO);
}