Advertisement
Guest User

Untitled

a guest
Jan 26th, 2015
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. 1. “null is used in java to represent uninitialized fields”
  2.  
  3. I thought this is what constructors are for, isn’t it?
  4. So either you can initialize a field in the constructor or it has a meaning in your domain to
  5. leave that field empty and in this 2nd case it should be represented with an Optional.
  6.  
  7. 2. “It is not possible to write a method that takes either an X or an Optional”
  8.  
  9. These methods clearly have 2 different semantics and then it is correct that they are 2
  10. totally separated method. In the first case you pass a value of type X that for sure is
  11. there, in the second a value of type X that could be also possibly empty. The fact that
  12. you have to distinguish these cases forces you to correctly reason on them.
  13.  
  14. 3. “Callers of your method are forced to unpack the optional immediately”
  15.  
  16. You have no idea of how the map and flatMap methods work, right? ;)
  17.  
  18. 4. “Any pre-existing API that uses null to signal no-value”
  19.  
  20. “Absence of a signal should never be used as a signal” – J. Bigelow, 1947
  21.  
  22. 5. “For example, java.util.Map has a get() method, and if anything ought to return an
  23. Optional, surely that should! Unfortunately, it doesn’t and it can’t be made to do so”
  24.  
  25. Optional.ofNullable(map.get(key));
  26.  
  27. it isn’t that difficult in the end ;)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement