10 Paranoid Java Tricks Every Developer Should Know
This article shares a seasoned developer’s top ten obsessive Java programming techniques—ranging from placing string literals first to avoid NullPointerExceptions, never trusting early JDK APIs, treating -1 cautiously, enforcing final modifiers, and rigorously structuring switch statements—to help you write safer, more maintainable code.
This is a summary of over 20 years of experience from a foreign expert.
“Anything that can go wrong will eventually go wrong.”
That’s why many people favor defensive programming. Below are ten personal, somewhat obsessive Java programming tips that the author finds most useful.
1. Put string constants first
Place string literals on the left side of equals() comparisons to avoid accidental NullPointerExceptions.
Using Optional (Java 8) can also help handle potentially null values safely.
2. Don’t trust early JDK APIs
Early Java APIs were immature; you may encounter code that returns null for non‑directory paths. Always add null checks.
Javadoc clarifies that a null return indicates the path is not a directory.
Neglecting null checks violates best‑practice rules #5 and #6.
3. Don’t blindly trust “-1”
Early Javadoc for String.indexOf() states it returns –1 when the character is absent, but treating –1 as a universal sentinel can be risky.
In some contexts a different sentinel (e.g., –2) might be more appropriate.
4. Avoid accidental assignment
Even the best programmers can make this mistake; placing constants on the left side of an assignment reduces the risk.
5. Check for null and length
Always verify that collections, arrays, or other objects are non‑null and non‑empty before use.
6. Declare all methods as final
To prevent unintended inheritance, mark methods as final unless they are part of an interface.
Overriding final methods is discouraged and can lead to fragile code.
7. Declare all variables and parameters as final
Marking variables and parameters final helps avoid accidental reassignment.
Ideally, languages would use val for immutable variables, similar to Scala.
8. Don’t trust generics when overloading
Overly generic APIs can lead users to misuse Object types, causing compilation errors and runtime bugs.
Such misuse illustrates why cautious API design matters.
9. Always add a default case in switch statements
Including a default ensures graceful handling of unexpected values.
This is especially useful when new enum values are added.
10. Enclose each case block in braces
Wrapping each case in its own braces creates a distinct scope, preventing fall‑through bugs.
Without braces, variables declared in one case can be accessed from others, which is confusing.
Conclusion
Obsessive programming habits can make code appear verbose, but after two decades of experience they help avoid subtle bugs caused by language quirks and legacy APIs, leading to more reliable software.
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.
ITFLY8 Architecture Home
ITFLY8 Architecture Home - focused on architecture knowledge sharing and exchange, covering project management and product design. Includes large-scale distributed website architecture (high performance, high availability, caching, message queues...), design patterns, architecture patterns, big data, project management (SCRUM, PMP, Prince2), product design, and more.
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.
