How to Write Bulletproof Code: Mastering If Statements, Error Handling, and Null Safety
This article explores practical programming philosophies for writing robust, maintainable code by using exhaustive two‑branch if statements, proper error handling, disciplined null‑pointer management, and avoiding over‑engineering, offering concrete examples and guidelines for Java and other languages.
Programming is a creative art that requires diligent practice; the following principles aim to guide developers toward writing code that is both reliable and maintainable.
Write Unbreakable Code
Prefer if statements with two branches—one for the true case and one for the false case—to ensure every possible condition is handled. Avoid omitting else branches, as doing so can hide corner cases and lead to “spaghetti” logic.
if (...) {
if (...) {
...
return false;
} else {
return true;
}
} else if (...) {
...
return false;
} else {
return true;
}While collapsing duplicate else branches may look cleaner, it makes reasoning about control flow harder, especially when logical operators like && and || are involved, increasing the risk of missed cases.
String s;
if (x < 5) {
s = "ok";
} else {
s = "";
}This explicit assignment makes the value of s clear in both branches, resembling a functional style where a variable is assigned once.
Proper Error Handling
Always handle every possible return value, such as -1 from read in Unix, or exceptions in Java. Treat exceptions as part of a union type {String, MyException} and catch only the specific exception types you expect.
try {
foo();
} catch (A e) {
// handle A
}
try {
bar();
} catch (A e) {
// handle A
}Avoid catching the generic Exception type, which can mask unrelated errors and make debugging difficult.
Correct Null‑Pointer Handling
Adopt a disciplined approach to null: avoid returning null when possible, use exceptions or Optional types, and never store null in collections. Use Objects.requireNonNull() to enforce non‑null parameters early.
public static <T> T requireNonNull(T obj) {
if (obj == null) {
throw new NullPointerException();
} else {
return obj;
}
}Leverage annotations like @NotNull/@Nullable and language features such as Optional (Java 8) or optional binding in Swift to combine null checks with value extraction atomically.
Avoid Over‑Engineering
Focus on solving the immediate problem before planning for future extensions, resist premature abstraction for reuse, and keep code simple and obviously correct rather than adding unnecessary testing scaffolding.
Solve the current issue first, then consider future extensions.
Write usable code, then evaluate reuse needs.
Prioritize simple, obviously correct code before extensive testing.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
21CTO
21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
