Boolean checking in the 'if' condition
Which one is better Java coding style?
boolean status = true;
if (!statu开发者_如何学运维s) {
//do sth
} else {
//do sth
}
or:
if (status == false) {
//do sth
} else {
//do sth
}
I would suggest that you do:
if (status) {
//positive work
} else {
// negative work
}
The ==
tests, while obviously redundant, also run the risk of a single =
typo which would result in an assignment.
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
,isOpen
This is the naming convention for
boolean
methods and variables used by Sun for the Java core packages.Using the
is
prefix solves a common problem of choosing bad boolean names likestatus
orflag
.isStatus
orisFlag
simply doesn't fit, and the programmer is forced to chose more meaningful names.Setter methods for
boolean
variables must have set prefix as in:
void setFound(boolean isFound);
There are a few alternatives to the
is
prefix that fits better in some situations. These arehas
,can
andshould
prefixes:boolean hasLicense(); boolean canEvaluate(); boolean shouldAbort = false;
If you look at the alternatives on this page, of course the first option looks better and the second one is just more verbose. But if you are looking through a large class that someone else wrote, that verbosity can make the difference between realizing right away what the conditional is testing or not.
One of the reasons I moved away from Perl is because it relies so heavily on punctuation, which is much slower to interpret while reading.
I know I'm outvoted here, but I will almost always side with more explicit code so others can read it more accurately. Then again, I would never use a boolean variable called "status" either. Maybe isSuccess or just success, but "status" being true or false does not mean anything to the casual reader intuitively. As you can tell, I'm very into code readability because I read so much code others have written.
The first one, or if (status) { /*second clause*/ } else { /* first clause */ }
EDIT
If the second form is really desired, then if (false == status) <etc>
, while uglier, is probably safer (wrt typos).
It really also depends on how you name your variable.
When people are asking "which is better practice" - this implicitly implies that both are correct, so it's just a matter of which is easier to read and maintain.
If you name your variable "status" (which is the case in your example code), I would much prefer to see
if(status == false) // if status is false
On the other hand, if you had named your variable isXXX (e.g. isReadableCode), then the former is more readable. consider:
if(!isReadable) { // if not readable
System.out.println("I'm having a headache reading your code");
}
The former. The latter merely adds verbosity.
The first one. But just another point, the following would also make your code more readable:
if (!status) {
// do false logic
} else {
// do true logic
}
Note that there are extra spaces between if
and the (
, and also before the else
statement.
EDIT
As noted by @Mudassir, if there is NO other shared code in the method using the logic, then the better style would be:
if (!status) {
// do false logic
}
// do true logic
My personal feeling when it comes to reading
if(!status) : if not status
if(status == false) : if status is false
if you are not used to !status reading. I see no harm doing as the second way.
if you use "active" instead of status I thing if(!active) is more readable
First style is better. Though you should use better variable name
This is more readable and good practice too.
if(!status){
//do sth
}else{
//do sth
}
精彩评论