Possible Duplicate:
What is the preferred way to write boolean expressions in Java
Today, me and my colleague raked up an argument. Which is a better way to use the boolean variables in Java code along with if statements.
boolean foo=true
//1.
if(foo == false)
// do something
else
// do something else
//2.
if(!foo)
// do something
else
// do something else
I support [1], since I think it's more readable. What do you guys think?.
Number 2, along with "foo" having a descriptive name, so that the code reads well:
if (!hasPurple)
...
I find #2 more readable. I think every Java dev (I'm a C# dev) would know what ! means. I
While probably not the point here, I prefer to put my "true" block as the statement. If the condition is generally going to be false, then I name my variable to represent
if (notFoo)
// do something when
else
// do something else
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With