Instead of
if (isPrime == 0) {
return false;
} else {
return true;
}
try
return (isPrime != 0);
It is the same thing, I'll show you the refactoring path. Starting with
if (isPrime == 0) {
return false;
} else {
return true;
}
is the same as
if (isPrime != 0) {
return true;
} else {
return false;
}
which can be reduced by substitution, substituting 'isPrime != 0' with the function 'doIsPrime()'
private boolean doIsPrime() {
return isPrime != 0;
}
substituting in
if (doIsPrime()) {
// guaranteed to be the same as above!
return doIsPrime();
} else {
return doIsPrime();
}
which can have both blocks reduced (as duplicated code)
if (doIsPrime()) {
}
return doIsPrime();
And reduced further by removing the if statement around the empty block
return doIsPrime();
Now undo the substitution 'doIsPrime()' back to 'isPrime != 0'
return isPrime != 0;
There was no need to really do the substitution; but, I find it better shows off the reasoning the if statement is redundant.