Some people say that Optionals should only be used for API designs.
I think there is more to it and we can modernize our code. See this Nullpointer Exception. In this example we get rid of this:
//myList = null;
myList.add(myParam);
In favor of that:
Optional.ofNullable(myParam).ifPresent(myList::add);
//.of(myParam) would have thrown a Nullpointer if not present...
And for Exceptions, we get rid of this:
if (token == null) {
throw new MyException("message");
}
In favor of that:
Optional.ofNullable(token).orElseThrow(() -> new MyException("message"));
Donnerstag, 3. Mai 2018
Abonnieren
Kommentare zum Post (Atom)
NEW BLOG! http://cleancode.consulting/
Dear Reader, This Blog is closed and remains as an archive. Please find our new Blog at http://cleancode.consulting/
-
Resolve a Merge Conflict in GIT with IntelliJ Let us assume you are using Bitbucket or a similar tool to administrate and host your GI...
-
Just create a package-info.java file for your package in which you have your POJO: @javax.xml.bind.annotation.XmlSchema(namespace = ...
Keine Kommentare:
Kommentar veröffentlichen