Avoiding nulls, immutability, object state
immutability, java, oop
Solution
Well you need to consider what you want the representation to be - not just the constructor signature. I suspect you will need to use a null reference (or at least something similar) for the field. But you could then have two constructors:
User(birthday) // Unmarried user
User(birthday, marriageDate) // Married user; marriageDate must not be null
As the user of an API, I'm not sure that I'd really like that though - I think I'd rather allow `marriageDate` to be null. In particular, it would be annoying to have to write:
LocalDate marriageDate = getMarriageDateOrNull();
User user = marriageDate == null ? new User(birthday)
: new User(birthday, marriageDate);
Alternatively, as people can get married more than once, you could always take an `Iterable<LocalDate> marriageDates`, then a never-married user would have an empty sequence :) (You'd need to consider married-then-divorced and married-then-widowed users though. Modelling real life is hard.)
Problem
I like the concept of immutability. I also like the concept of no nulls (and if possible also no NullDesignPattern, no NullObjects... as much as possible). But what about the following scenario: I have an object `User`, which has two fields: `birthday` and `dateInLifeMarried` (could be any similar field; the important thing is that at first this field is `null` and changes at some point in object life). As it's immutable I want both fields to be in the constructor: ``` public User(birthday, dateInLifeMarried) ``` Now: - I don't want to pass `null` to the second parameter - I don't want to add a setter becuase its immutable - i don't want to call the constructor with NullObjectPattern instead of `null` Am i just contradicting myself or is there an elegant way to have it which i'm not thinking of?