I used to overuse Optional in Java. Then I learned when not to use it. Optional is great for: • Return types • Avoiding null checks • Making intent clear But using it everywhere can actually make code worse. ❌ Don’t do this: class User { Optional<String> email; } Why? • Makes serialization messy • Complicates getters/setters • Adds noise where it’s not needed ✅ Better approach: Optional<String> findEmailByUserId(Long userId); Rule of thumb I follow now: 👉 Use Optional at the boundaries, not inside your models. Java gives us powerful tools, but knowing where to use them matters more than just knowing how. Clean code is less about showing knowledge and more about reducing confusion. What’s one Java feature you stopped overusing after some experience? #Java #CleanCode #BackendDevelopment #SoftwareEngineering #LearningInPublic #OptionalInJava #Optimization
I never used it in fields of method parameters 😮😲
Helpful
Nice one Rashmi Arya 👏 Optional was designed mainly as a return type to clearly signal that a value may be absent and to reduce null checks at call sites. Using it in fields or method parameters often causes more issues than benefits, especially with serialization frameworks. It also pushes the responsibility of handling absence into places where it’s not always needed. Like most Java features, it’s powerful but works best when used in the right place.