Re: Records — Dealing with nullable values

Remi Forax forax at univ-mlv.fr
Sun Dec 15 11:10:41 UTC 2019


----- Mail original -----
> De: "Stephen Colebourne" <scolebourne at joda.org>
> À: "amber-dev" <amber-dev at openjdk.java.net>
> Envoyé: Dimanche 15 Décembre 2019 02:07:45
> Objet: Re: Records — Dealing with nullable values

> As a data point, we at OpenGamma follow the get/optional pattern 100%.
> We never return null from a data object, always optional. And we never
> use Optional as the field value. And very rarely is Optional used as a
> method/constructor parameter, because doing so is very unfriendly to
> your caller.

Hi Stephen,
wrapping my head around what you are saying, if you have a getter that returns an Optional and rarely a constructor or a setter that takes Optional,
it means that your class is mutable, right ?

> 
> From a quick search:
> Subsystem a: 1035 beans which contain 4895 properties, of which 400
> properties follow the get=optional pattern (~8%)
> Subsystem b: 1124 beans which contain 2694 properties, of which 126
> properties follow the get=optional pattern (~5%)
> 
> Records are clearly not beans, they are effectively something new
> (nominal tuples) that Java devs will have to learn not to overuse.
> (And I suspect there will be a lot of overuse). ie. Arash, I'd
> question whether its really the right choice to switch from Lombok to
> records, despite the shorter syntax.

yes,
we should communicate more on that, records are not beans, i repeat, records are not beans !
you will be disappointed if you try to replace all your beans by records.

Personally, i'm fine with record components being declared as Optional but in the future when Optional will be retrofitted to be an inline class :)

I suppose the closest you can come with a record to emulate the pattern you and Arash described is

  record Person(String name, Optional<Gender> gender) {
    public Person {
      Objects.requireNonNull(name);
    }
    public Person(String name) {
       this(name, Optional.empty());
    }
  }

the compact constructor guarantee that name is never null, the other constructor sweet the pill for callers.

[...]

> 
> Stephen

Rémi


More information about the amber-dev mailing list