[LRUG] Comparable Null Objects
m at rkcoleman.co.uk
Thu Jul 16 08:51:39 PDT 2015
I think null objects are good for solving your initial problem, but that's
as far as it goes.
How about creating your own class for representing dates, HelpfulDate, that
can either be constructed from a Date or a NoExpectedDate. It delegates
behaviour to these classes for everything except <=>.
Your Event class then contains only HelpfulDates that always know how to
On 16 July 2015 at 13:54, Duncan Stuart <dgmstuart at gmail.com> wrote:
> Awesome - thank you (and everyone) for taking the time to explain.
> On 16 July 2015 at 13:40, Tom Stuart <tom at codon.com> wrote:
>> On 16 Jul 2015, at 13:03, Duncan Stuart <dgmstuart at gmail.com> wrote:
>> > Because ordering on Dates in Ruby requires actual Dates and not
>> Date-like things, forcing us into an undesirable implementation?
>> Yes, I think that’s the underlying problem.
>> However, many of the techniques for “eliminating” nil checks or
>> instance-of checks don’t really eliminate them, only encapsulate them. The
>> condition that drives the decision about which class to instantiate (or
>> whatever) has to go somewhere. So, for me, the principle is about doing
>> those checks sooner, so that the rest of the system can lean on
>> polymorphism — which is just a different kind of conditional that gets
>> hidden from the caller — instead of having to be directly aware of the
>> For example, you could “eliminate” the checks here by having two
>> different classes of date-ranking object for events with & without dates,
>> and designing their #<=> methods so that the two kinds of object
>> interoperate nicely during a heterogeneous sort, etc. But you’d still need
>> a condition somewhere to decide which of those classes to use for ranking a
>> given event.
>> So it is a trade-off, and the guiding principle is how tightly you want
>> to contain the information that an event may be missing its date.
>> Chat mailing list
>> Chat at lists.lrug.org
>> Archives: http://lists.lrug.org/pipermail/chat-lrug.org
>> Manage your subscription: http://lists.lrug.org/options.cgi/chat-lrug.org
>> List info: http://lists.lrug.org/listinfo.cgi/chat-lrug.org
> Chat mailing list
> Chat at lists.lrug.org
> Archives: http://lists.lrug.org/pipermail/chat-lrug.org
> Manage your subscription: http://lists.lrug.org/options.cgi/chat-lrug.org
> List info: http://lists.lrug.org/listinfo.cgi/chat-lrug.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Chat