Whenever you have code like this:
if (condition) {
if (confition2) {
doSomeStuff();
}
}
You can rewrite it to:
if (condition && condition2) {
doSomeStuff();
}
Whenever you have code like this:
if (condition) {
doSomeStuff();
return value1;
} else {
doSomeOtherStuff();
return value2;
}
You can rewrite it to:
if (condition) {
doSomeStuff();
return value1;
}
doSomeOtherStuff();
return value2;
You can thus combine these two techniques. Whenever you have this:
if (condition) {
if (condition2) {
return value2;
}
}
return value1;
You can rewrite as:
if (condition && condition2)
return value2;
return value1;
#Use case
Notice how this complex code:
if (condition1) {
if (condition2) {
if (condition3) {
return value2;
} else {
return value1;
}
} else {
return value1;
}
} else {
return value1;
}
Can be greatly simplified:
if (condition1 && condition2 && condition3)
return value2;
return value1;
You can rewrite this:
for (int i = 0; i <= 100; i++) {
if (i % 2 == 0) {
System.out.println("Even: " + i);
} else {
System.out.println("Odd: " + i);
}
}
as...
for (int i = 0; i <= 100; i++) {
if (i % 2 == 0) {
System.out.println("Even: " + i);
continue;
}
System.out.println("Odd: " + i);
}
This is very useful for getting ride of "skip" clauses. For example, you can rewrite this:
for (int i = 0; i < 100; i++) {
if (condition1) {
if (condition2) {
if (condition3) {
doSomething();
}
}
}
}
As:
for (int i = 0; i < 100; i++) {
if (!(condition1 && condition2 && condition3))
continue;
doSomething();
}
Often times in programming, we find ourselves checking a number of "bail" cases before running a method. For example, we might check if a string is non-empty, and that it starts with a "(". Using these techniques can make such code much more readable.
Consider this difference between this:
if (str.length() != 0) {
if (str.charAt(0) == '(') {
// do
// a lot
// of stuff
// here.
// do
// a lot
// of stuff
// here.
// do
// a lot
// of stuff
// here.
// do
// a lot
// of stuff
// here.
// do
// a lot
// of stuff
// here.
// do
// a lot
// of stuff
// here.
return something;
}
}
return false;
And this:
if (str.length() == 0)
return false;
if (str.charAt(0) != '(')
return false;
// do
// a lot
// of stuff
// here.
// do
// a lot
// of stuff
// here.
// do
// a lot
// of stuff
// here.
// do
// a lot
// of stuff
// here.
// do
// a lot
// of stuff
// here.
// do
// a lot
// of stuff
// here.
return something;
Now, when you are reading through the method, the first thing you see is that we "bail" on two different cases. Once these are "out of the way", you can think about the long complicated task ahead of you. When you reach the bottom of the complex task, you no longer have to think "huh, which if statement does this } close?"
Nice!