How do you name unused variables? If even just having to make that choice annoys you (it's not just me, right?), read on for how #JavaNext will fix that and make pattern matching (how's that related?!) more maintainable in the process. 🧵
TL;DR: Unused variables suck. They're rare now, but will be more prevalent soon. JEP 443 proposes to replace them with _ for cleaner code and better pattern matching. "This 🧵 should've been 📰" ⇝ nipafx.dev/inside-java-ne… "This 🧵 should've been 📽️" ⇝ youtube.com/watch?v=nP1k41…
@nipafx map.computeIfAbsent(key, ignoreMe -> new ArrayList<>());
@nipafx @ammbra1508 Is it ok to use this for Map.computeIfAbscent()? String name = ...; map.computeIfAbscent(name, _ -> new Person(name)); Or is better to do this? map.computeIfAbscent(name, name2 -> new Person(name2));
@nipafx I usually delete unused variables 🤔 who keeps them ans why?
@nipafx I use "_" and then cry it doesn't pass the syntax check, and then cry some more.
@nipafx In Google, naming these `unused*` makes them exempt from our unused-variable checker. This is a better approach than `@ SuppressWarnings` because if it does become used the user will rename it and won't have some cruft to remember to go back and clean out.
@nipafx Usually just _unused for generic case, _ignored for exceptions, _k/_key for computeIfAbsent. With ide configured to suppress warning names prefixed with underscore
@nipafx Funny how Java is always 10 years behind C#
@nipafx Usually $ ``` locks.computeIfAbsent(key, $ -> new ReentrantLock()); ```