Java coding hacks developers should know 🧵 1) Use Map.ofEntries() for creating multi-pair immutable maps Why: Shorter, immutable, safer and avoids accidental mutation.
2) Use Optional. map().orElse() instead of manual null handling Why: Eliminates null checks, reads better, and chains smoothly with other logic.
3) Use Enum.valueOf() + name() for safe enum conversion Why: Cleaner, and lets you easily serialize/deserialize enums in APIs, DB, or files.
@theskilledcoder The first seems more readable!
@theskilledcoder This is not the goal of Optional, which is a data wrapper not a control flow operator. When implementing an algorithm it is better to write a null check with a simple if than using an Optional.
@theskilledcoder "Use Optional. map().orElse() instead of manual null handling" Nah, that is actually not recommended. Only use Optional if you receive an Optional.
@theskilledcoder Not sure you should do this just because it compiles/works. I like your other examples though!
@theskilledcoder Sorry, it's a bad pattern. You should use it only if you already got an optional User. Otherwise use ? operator as it is cleaner, concise with no overhead. Even if you need chaining - there's a better way by using instanceof with patterns: