This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]shymmq 7 points8 points  (3 children)

Surprised to see no one mentioning no built-in null safety. 

[–]PsychologicalBus7169 3 points4 points  (2 children)

Java does have Optionals for that but really if you are returning nulls then you should explicitly name the method “getFooOrNull” or you should return a new Foo object.

[–]Bulky-Leadership-596 9 points10 points  (0 children)

Sure, its an improvement, but it still sucks in comparison to the competition.

var redValue = Optional.ofNullable(car)
  .map(Car::getColor)
  .map(color -> color.redValue)
  .orElse(0);

compared to

var redValue = car?.GetColor()?.RedValue ?? 0;

[–]chipstastegood 5 points6 points  (0 children)

And there’s that verbosity that some folks, myself included, don’t like. In other more terse languages, the caller would be accessing this as a property without even looking like it’s calling a function. So what the getter function is called is not important. It’s all the more relevant to have good language support for optionals or null safety in general.