Ok before I start explaining my question I want you to know that I know about the design idea behind Optional and that it isn't intended to be used in fields or collections, but I have programmed a lot in Kotlin currently and really dislike using null.
So I have a Node based editor like in the Unreal Engine and each node has ConnectionBoxes, which can be either free or are occupied by a Connection.
So there are different ways to express this one of which is using a map that maps each ConnectionBox to a Connection like:
Map<ConnectionBox, Connection> connectionEndPoints;
and Connection could be null if the ConnectionBox is free. I don't like this, since an other developer doesn't know about the function of this Map and that it may return null for an existing ConnectionBox.
So I am tempted to use a:
Map<ConnectionBox, Optional<Connection>> connectionEndPoints;
which displays the intend much better and you can even read it like:
"This ConnectionBox may have a Connection attached."
My question is: Why should I not do this, even though it shows the intend much more clearly and prevents NPEs. Every SO-thread and every blog post says this is bad style and even the compiler say that I shouldn't do it with a warning.
On request here is a SO-thread that discourages use of Optional as a field or Collection value: Uses for Optional
And here is the warning (as it turns out it is a warning from IntelliJ):
Warning: Optional used as type for field {fieldName}
Ok after recommending that the Connection reference should lie within the ConnectioBox the question just shifts.
Why is:
class ConnectionBox {
Optional<Connection> connection;
...
worse than
class ConnectionBox {
Connection connection; //may be null
...
Unless I make a comment you can't see that you may run into a NPE and I dislike comments explaining code that could explain itself.