[LRUG] Comparable Null Objects

Mark Coleman 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
compare eachother.

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
>> condition.
>>
>> 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...
URL: <http://lists.lrug.org/pipermail/chat-lrug.org/attachments/20150716/8744ee5b/attachment-0003.html>


More information about the Chat mailing list