netbeans - Java boolean method return -
i have boolean method in java program , i'm wondering why netbeans recommending making change code:
return isprime != 0;
what wrote was:
if (isprime == 0) { return false; } else{ return true; }
both work correctly cannot understand logic behind change netbeans suggesting. neither true or false being returned. explain me logic behind this? thanks
instead of
if (isprime == 0) { return false; } else { return true; }
try
return (isprime != 0);
it same thing, i'll show refactoring path. starting with
if (isprime == 0) { return false; } else { return true; }
is same
if (isprime != 0) { return true; } else { return false; }
which can reduced substitution, substituting 'isprime != 0' function 'doisprime()'
private boolean doisprime() { return isprime != 0; }
substituting in
if (doisprime()) { // guaranteed same above! return doisprime(); } else { return doisprime(); }
which can have both blocks reduced (as duplicated code)
if (doisprime()) { } return doisprime();
and reduced further removing if statement around empty block
return doisprime();
now undo substitution 'doisprime()' 'isprime != 0'
return isprime != 0;
there no need substitution; but, find better shows off reasoning if statement redundant.
Comments
Post a Comment