Chapter 12
Home
Deeply nested if blocks are hard to read. An early exit
checks for "bad" cases first and returns immediately, leaving the main logic
unindented and clear.
Compare these two versions of the same function. Both are correct — but the second one is much easier to follow.
if (condition_a) {
if (condition_b) {
if (condition_c) {
// actual logic
}
}
}
if (!condition_a) return; if (!condition_b) return; if (!condition_c) return; // actual logic
A triangle is valid if all three sides are positive and the sum of any two sides is greater than the third.
Sample input: 3 4 5 Expected output: true
bool is_valid(int a, int b, int c) {
if (a > 0 && b > 0 && c > 0) {
if (a + b > c && a + c > b && b + c > a) {
return true;
}
}
return false;
}
bool is_valid(int a, int b, int c) {
if (a <= 0 || b <= 0 || c <= 0) return false;
if (a + b <= c) return false;
if (a + c <= b) return false;
if (b + c <= a) return false;
return true;
}
fn is_valid(a: i32, b: i32, c: i32) -> bool {
if a > 0 && b > 0 && c > 0 {
if a + b > c && a + c > b && b + c > a {
return true;
}
}
false
}
fn is_valid(a: i32, b: i32, c: i32) -> bool {
if a <= 0 || b <= 0 || c <= 0 { return false; }
if a + b <= c { return false; }
if a + c <= b { return false; }
if b + c <= a { return false; }
true
}
else block.
if blocks are a code smell — consider refactoring.