Boolean Zen is all about using boolean values efficiently and concisely.
The following code shows a tempting way to write an if statement based on boolean value.
if (test == true) {
// do some work
}
Note that test
itself is a boolean
value. When it is
true
, we are asking whether
true == true
.
true == true
will evaluate to true
,
but remember that the
if
branch will execute as long as what is in its parentheses evaluates to true. So
we can actually use
test
directly:
if (test) {
// do some work
}
test
,
don’t check for
test == false
.
Instead, use
!test
to check that the opposite of
test
evaluates to true
.Here’s an example that uses what we learned in the previous section about simplifying boolean expressions.
if (test) {
return true;
} else {
return false;
}
There is actually a much more concise way to do this. If we want to return
true when test is true and false when test is false, then we actually just want
to return the value of
test
.
return test;
In general, make your use of booleans to simplify conditional and boolean expressions. This is something to look out for whenever you have conditions or boolean values.
if (someTest) {
System.out.println("hello!");
}
if (!someTest) {
System.out.println("I'm redundant");
}
||
and just write
the behavior once.if (max < result) {
return max;
}
if (max == 0) {
return max;
}
if (max < 0) {
// do nothing
} else {
...
}
Don't use if / else statements or ternary operators to to assign boolean values the result of a boolean expression.
boolean meetsReq;
if (score > = MIN_SCORE) {
meetsReq = true;
} else {
meetsReq = false;
}
boolean meetsReq = false;
if (score > = MIN_SCORE) {
meetsReq = true;
}
boolean meetsReq = score >= MIN_SCORE;
When writing loops, choose loop bounds or loop conditions that help
generalize code the best. For example, the code before this
for
loop is unnecessary.
System.out.print("\*");
for (int i = 0; i < 4; i++) {
System.out.print("\*");
}
Instead, why not just run the loop one extra time? This way we avoid writing duplicate behavior.
for (int i = 0; i < 5; i++) {
System.out.print("\*");
}
If you have something that only happens once (maybe at the end of a bunch of repeated tasks), then don’t put code for it inside of your loop.
for (int i = 0; i < 4; i++) {
if (i != 3) {
System.out.println("working hard!");
} else {
System.out.println("hardly working!");
}
}
for (int i = 0; i < 3; i++) {
System.out.println("working hard!");
}
System.out.println("hardly working!");
Similarly, a loop that always runs one time is also a bad use of a loop. When reviewing the logic of your code, it might be helpful to check if a loop can be reduced to an if or just deleting the loop entirely.
Avoid adding extra if/else cases to recursive methods when there are more generalized conditions to use.
index == list.length - 1
.
If we omit this and just use the first base case, the behavior will actually
still generalize and be correct, but our code will be more concise and
easier to reason about.private static int sum(int[] list, int index) {
if (index == list.length) {
return 0;
} else if (index == list.length - 1) {
return list[index];
} else {
return list[index] + sum(list, index + 1);
}
}
private static int sum(int[] list, int index) {
if (index == list.length) {
return 0;
} else {
return list[index] + sum(list, index + 1);
}
}
public static int sum(int[] list) {
if (list.length < 1) {
return 0;
} else {
return list[0] + sum(list, 1);
}
}
private static int sum(int[] list, int index) {
if (index == list.length) {
return 0;
} else {
return list[index] + sum(list, index + 1);
}
}
public static int sum(int[] list) {
return sum(list, 0);
}
private static int sum(int[] list, int index) {
if (index == list.length) {
return 0;
} else {
return list[index] + sum(list, index + 1);
}
}