Which one is better Java coding style?
boolean status = true;
if (!status) {
//do sth
} else {
//do sth
}
or:
Former, of course. Latter is redundant, and only goes to show that you haven't understood the concept of booleans very well.
One more suggestion: Choose a different name for your boolean variable. As per this Java style guide:
is prefix should be used for boolean variables and methods.
isSet,isVisible,isFinished,isFound,isOpenThis is the naming convention for
booleanmethods and variables used by Sun for the Java core packages.Using the
isprefix solves a common problem of choosing bad boolean names likestatusorflag.isStatusorisFlagsimply doesn't fit, and the programmer is forced to chose more meaningful names.Setter methods for
booleanvariables must have set prefix as in:
void setFound(boolean isFound);There are a few alternatives to the
isprefix that fits better in some situations. These arehas,canandshouldprefixes:boolean hasLicense(); boolean canEvaluate(); boolean shouldAbort = false;