I think it'd be a good idea to add some style guide text about the use of Optional (
bug). Please share any thoughts / opinions on the following (not a suggestion for text to add, but first trying to align on what the stance should be):
1. Optional methods that accept lambdas should be banned.
E.g.: myOptional.ifPresent(x -> x.foo());
Rationale: The lambda makes this 10x bigger than an "if" in binary size, and it also requires an object allocation.
2. Optional Boxed types should be banned
E.g. Optional<Integer>
* Better to use @Nullable Integer.
Rationale: The use of "Integer" outside of a collection already implies "this can be null".
Rationale: Boxing a boxed type makes for two layers of unboxing.
Rationale: @IntDef works with Integer, but not with Optional<Integer> (I think)
3. Optional should be banned for reference types
E.g.: Always use @Nullable
Rationale: No risk of using banned optional methods if we don't use optional (this would make the guidance very simple)
Rationale: Cuts down on wrapping / unwrapping verbosity
Rationale: Compared to std::optional, NPEs are not security bugs, and std::optional does not require a heap allocation.
4. Allow Optional only for return types
Rationale: There seems to be some
vocal proponents of this, although I don't really see why it's different form parameters / fields.
For some concrete examples, here are some actual conversion CLs wrt Optional: