Check out the following code:
@Override
public int compareTo(final Intersection o) {
if (o == null)
return 0;
double distance = t;
double distance2 = o.t;
if (distance == distance2)
return 0;
return distance2 > distance ? -1 : 1;
}
All seems well however the fact that 0 may be returned on two different conditional occasions does somewhat bother me. If I do however move the variable assignments of distance
and distance2
to the top, my IDE warns me that
if (o == null)
return 0;
Would then be 'dead' code. If that's the case, should null even be checked in this scenario?
What I mean is:
@Override
public int compareTo(final Intersection o) {
double distance = t;
double distance2 = o.t;
if (o == null)
return 0;
if (distance == distance2)
return 0;
return distance2 > distance ? -1 : 1;
}