Skip to main content

Posts

Showing posts with the label Clean Code

💡 Optional in Java 8 — NullPointerException's Worst Enemy (and Your Best Friend!)

🚀 Introduction Raise your hand if you've ever been attacked by this monster: java.lang.NullPointerException: Cannot invoke "getName()" because "user" is null We’ve all been there. You think your code is clean... But null is silently hiding, waiting to crash your app. 💣 So, Java 8 gave us a gift: 🎁 Optional<T> — A smart wrapper to help us handle missing values without crashing the app. Let’s understand this with simple words, diagrams, examples, and yes — a few fun moments! 😄

🔥 Top 20 Java Coding Standards vs Anti-Patterns — With Real Examples & Fixes!

🔥 Top 20 Java Coding Standards vs Anti-Patterns — With Real Examples & Fixes! Welcome to a no-nonsense, funny, and deeply educational guide that’ll help you avoid bugs, write cleaner code, and ace interviews. Let’s roll! 🎯 🎯 1. Avoid Wildcard Imports (import.*) ✅ Best: import java.util.List; import java.util.ArrayList; ❌ Worst: import java.util.*; 💥 What goes wrong: - Conflicting classes (like java.util.Date vs java.sql.Date) - IDE slows down due to unnecessary type loading - Harder for humans to read 😂 "I use * because I believe in surprises!" — Future you debugging a Date conflict ⚡ 2. Fail Fast Design if (user == null) throw new IllegalArgumentException("User can't be null"); user.getName(); // Boom! NPE gift 🎁 🧠 Catch bugs early, easier to debug. 🛡️ 3. Immutable DTOs public record UserDTO(String name, int age) {} public class UserDTO { public String name; public int age; } ...