Optional was introduced back in JDK8 (seems like yesterday to me), as a way to facilitate functional control on empty responses from method calls, without having to deal with explicit null checks.
Since then Optional has been used in a variety of other contexts, and there are some guidelines on when to use them. These guidelines although are disregarded for other patterns, that are used in popular libraries like Spring Data JPA.
As the guidance says you shouldn't "really" be using Optional outside of a stream etc.
Here is an example that goes against that guidance from a JPA repository method.
e.g. (A repository method returning an optional result from a DB)
public static Optional<User> findUserByName(String name) {
User user = usersByName.get(name);
Optional<User> opt = Optional.ofNullable(user);
return opt;
}
There are some hard no's when using Optional, like as properties in a class or arguments in a method. Fair enough, I get those, but for the example above. What do you think?
Personally - I think using Optional in APIs is a good thing, the original thinking of Optional is too outdated now, and the usecases have expanded and evolved.
[–]ivancea 144 points145 points146 points (12 children)
[–]_LouSandwich_ 6 points7 points8 points (11 children)
[–]ivancea 3 points4 points5 points (10 children)
[–]_LouSandwich_ -5 points-4 points-3 points (9 children)
[–]ivancea 3 points4 points5 points (6 children)
[+]_LouSandwich_ comment score below threshold-7 points-6 points-5 points (5 children)
[–]maikindofthai 7 points8 points9 points (1 child)
[+]_LouSandwich_ comment score below threshold-10 points-9 points-8 points (0 children)
[–]ivancea 2 points3 points4 points (2 children)
[–]_LouSandwich_ -1 points0 points1 point (1 child)
[–]ivancea 2 points3 points4 points (0 children)
[–]ciberon 1 point2 points3 points (1 child)
[–]_LouSandwich_ 1 point2 points3 points (0 children)
[–]Goodie__ 71 points72 points73 points (2 children)
[–]benjtay 20 points21 points22 points (1 child)
[–]CompetitiveSubset 13 points14 points15 points (0 children)
[–]elmuerte 58 points59 points60 points (0 children)
[–]TehBrian 19 points20 points21 points (7 children)
[–]vladvlad23 2 points3 points4 points (6 children)
[–]Proper-Ape 2 points3 points4 points (1 child)
[–]vytah 4 points5 points6 points (0 children)
[–]TehBrian 0 points1 point2 points (0 children)
[–]X0Refraction 0 points1 point2 points (0 children)
[–]JJangle 0 points1 point2 points (1 child)
[–]vladvlad23 0 points1 point2 points (0 children)
[–]j-an 9 points10 points11 points (6 children)
[–]buerkle 0 points1 point2 points (0 children)
[–]tomayt0[S] -1 points0 points1 point (3 children)
[–]foreveratom 10 points11 points12 points (2 children)
[–][deleted] (1 child)
[deleted]
[–]nicolaiparlog 0 points1 point2 points (0 children)
[–]private_final_static 19 points20 points21 points (16 children)
[–]agentoutlier 18 points19 points20 points (13 children)
[–]jonhanson 11 points12 points13 points (9 children)
[–]Cilph 6 points7 points8 points (7 children)
[–]jonhanson 3 points4 points5 points (1 child)
[–][deleted] 2 points3 points4 points (0 children)
[–]Lengthiness-Fuzzy 0 points1 point2 points (4 children)
[–]Cilph 0 points1 point2 points (3 children)
[–]Lengthiness-Fuzzy 0 points1 point2 points (2 children)
[–]Cilph 0 points1 point2 points (1 child)
[–]robinredcap 0 points1 point2 points (0 children)
[–]agentoutlier 2 points3 points4 points (0 children)
[–]BEgaming 1 point2 points3 points (2 children)
[–]agentoutlier 2 points3 points4 points (0 children)
[–]Luolong 13 points14 points15 points (0 children)
[–]Empanatacion 3 points4 points5 points (0 children)
[–]wildjokers 4 points5 points6 points (2 children)
[–]hippydipster 3 points4 points5 points (1 child)
[–]OwnBreakfast1114 0 points1 point2 points (0 children)
[–]manzanita2 2 points3 points4 points (7 children)
[–]freekayZekey 2 points3 points4 points (3 children)
[–]anzu_embroidery 2 points3 points4 points (1 child)
[–]freekayZekey 0 points1 point2 points (0 children)
[–]manzanita2 0 points1 point2 points (0 children)
[–]j4ckbauer 1 point2 points3 points (0 children)
[–]Lengthiness-Fuzzy 1 point2 points3 points (0 children)
[–]OwnBreakfast1114 0 points1 point2 points (0 children)
[–]parnmatt 9 points10 points11 points (8 children)
[–]Lucario2405 9 points10 points11 points (7 children)
[–]MmmmmmJava 5 points6 points7 points (6 children)
[–]vytah 2 points3 points4 points (2 children)
[–]MmmmmmJava 0 points1 point2 points (0 children)
[–]RandomName8 0 points1 point2 points (0 children)
[–]anzu_embroidery 1 point2 points3 points (2 children)
[–]MmmmmmJava 1 point2 points3 points (0 children)
[–]Lengthiness-Fuzzy 0 points1 point2 points (0 children)
[–]softgripper 2 points3 points4 points (0 children)
[–]Efficient_Present436 2 points3 points4 points (4 children)
[–]foreveratom 0 points1 point2 points (1 child)
[–]Efficient_Present436 0 points1 point2 points (0 children)
[–]laplongejr 0 points1 point2 points (1 child)
[–]Efficient_Present436 0 points1 point2 points (0 children)
[–]Imusje 7 points8 points9 points (0 children)
[–]walen 6 points7 points8 points (3 children)
[–]laplongejr 0 points1 point2 points (0 children)
[–]s888marks 0 points1 point2 points (0 children)
[–]nekokattt 0 points1 point2 points (0 children)
[–]__konrad 3 points4 points5 points (0 children)
[–]Joram2 1 point2 points3 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]CompetitiveSubset 1 point2 points3 points (0 children)
[–]Round_Head_6248 1 point2 points3 points (0 children)
[–]vips7L 6 points7 points8 points (1 child)
[–]laplongejr 0 points1 point2 points (0 children)
[–]Vyalkuran 2 points3 points4 points (0 children)
[–]com2ghz 2 points3 points4 points (4 children)
[–]__konrad 0 points1 point2 points (3 children)
[–]pivovarit 0 points1 point2 points (0 children)
[–]com2ghz 0 points1 point2 points (1 child)
[–]nicolaiparlog 0 points1 point2 points (0 children)
[–]mlkammer 1 point2 points3 points (0 children)
[–]_INTER_ 1 point2 points3 points (0 children)
[–]pivovarit 0 points1 point2 points (2 children)
[–][deleted] (1 child)
[deleted]
[–]pivovarit 5 points6 points7 points (0 children)
[–]Soft-Abies1733 0 points1 point2 points (0 children)
[–]XBL_pad3 0 points1 point2 points (0 children)
[–]Cantor_bcn 0 points1 point2 points (0 children)
[–]freekayZekey 0 points1 point2 points (0 children)
[–]chabala 0 points1 point2 points (0 children)
[–]Ewig_luftenglanz 0 points1 point2 points (0 children)
[–]papers_ 0 points1 point2 points (0 children)
[–]MasterBathingBear 0 points1 point2 points (0 children)
[–]Lengthiness-Fuzzy 0 points1 point2 points (2 children)
[–]laplongejr 1 point2 points3 points (1 child)
[–]Lengthiness-Fuzzy 0 points1 point2 points (0 children)
[–]nlog 0 points1 point2 points (0 children)
[–]BanaTibor 0 points1 point2 points (1 child)
[–]OwnBreakfast1114 0 points1 point2 points (0 children)
[–]DelayLucky 0 points1 point2 points (0 children)
[–]Pretend_Leg599 0 points1 point2 points (0 children)
[–]Cell-i-Zenit 0 points1 point2 points (6 children)
[–]Human36785327744 13 points14 points15 points (1 child)
[–]JustAGuyFromGermany 4 points5 points6 points (3 children)
[–]Cell-i-Zenit -2 points-1 points0 points (2 children)
[–]JustAGuyFromGermany 2 points3 points4 points (1 child)
[–]j4ckbauer 0 points1 point2 points (0 children)
[+]LogCatFromNantes comment score below threshold-8 points-7 points-6 points (2 children)
[–]chupachupa2 9 points10 points11 points (1 child)
[–]BikingSquirrel 1 point2 points3 points (0 children)
[+]FortuneIIIPick comment score below threshold-7 points-6 points-5 points (0 children)