<div dir="ltr">Many thanks Tom - this makes a lot of sense. <br><br>One thing you could maybe help me understand:<br>I have this bit of dogma in my head that type checking is evil - including nil-checking. I haven't deeply understood why this is, but thinking about it but my best guess is that it's an application of DIP (?) <span style="color:rgb(0,0,0);font-family:sans-serif;font-size:13.3000001907349px;line-height:16.625px;background-color:rgb(249,249,249)">“Depend upon Abstractions. Do not depend upon concretions.”<br><br>I get that most 'rules' have scenarios where they doesn't apply, and that the key is to understand the underlying principles behind that rule - I'm just not sure what they are.<br><br>In this case is it a trade-off, or does it just not apply? </span><div><span style="color:rgb(0,0,0);font-family:sans-serif;font-size:13.3000001907349px;line-height:16.625px;background-color:rgb(249,249,249)">Because this class is genuinely concerned with Dates (or NoExpectedDates?) and no other type? </span></div><div><span style="color:rgb(0,0,0);font-family:sans-serif;font-size:13.3000001907349px;line-height:16.625px;background-color:rgb(249,249,249)">Because ordering on Dates in Ruby requires actual Dates and not Date-like things, forcing us into an undesirable implementation? <br><br>Would something like `</span><span style="color:rgb(0,0,0);font-family:sans-serif;font-size:13.3000001907349px;line-height:16.625px;background-color:rgb(249,249,249)">when</span><span style="font-size:12.8000001907349px"> Date, Numeric` be preferable to `when </span><span style="font-size:12.8000001907349px">NoExpectedDate` be preferable at all?<br></span><span style="font-size:12.8000001907349px"><br>Cheers<br><br>Duncan</span></div></div><div class="gmail_extra"><br><div class="gmail_quote">On 16 July 2015 at 10:27, <span dir="ltr"><<a href="mailto:tom@codon.com" target="_blank">tom@codon.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">On 15 Jul 2015, at 22:44, Duncan Stuart <<a href="mailto:dgmstuart@gmail.com">dgmstuart@gmail.com</a>> wrote:<br>
> Do you think I'm applying the idea incorrectly or inappropriately?<br>
<br>
</span>FWIW I do think you’re misapplying the idea, for the simple reason that it doesn’t really help here. The null object pattern is helpful when there’s some protocol that can sensibly be replicated by the null object; your `def to_s; 'Unknown'; end` is a good example. But there’s no easy way for a null object to conform to the expectations of Date#<=>, so it’s not going to make things better.<br>
<br>
My advice would be to avoid trickery entirely and take a different piece of advice from Sandi’s talk: extract a role. Make a new kind of object whose sole responsibility is the (not entirely trivial) logic of ordering things by date when some of them may not have dates.<br>
<br>
There are a few ways of doing this, but one example might be to have a DateRank class:<br>
<br>
DateRank = Struct.new(:maybe_date) do<br>
def <=>(other)<br>
if maybe_date.nil?<br>
1<br>
elsif other.maybe_date.nil?<br>
-1<br>
else<br>
maybe_date <=> other.maybe_date<br>
end<br>
end<br>
end<br>
<br>
(Substitute `case maybe_date … when NoExpectedDate` for `if nil?` if you choose to use null objects for other reasons — the two ideas are orthogonal.)<br>
<br>
And then you can do `events.sort_by(&DateRank.method(:new))` or whatever.<br>
<br>
I talk about this idea a bit in <a href="http://codon.com/a-lever-for-the-mind#poker" rel="noreferrer" target="_blank">http://codon.com/a-lever-for-the-mind#poker</a> if you’re interested.<br>
<br>
Cheers,<br>
-Tom<br>
<div class="HOEnZb"><div class="h5">_______________________________________________<br>
Chat mailing list<br>
<a href="mailto:Chat@lists.lrug.org">Chat@lists.lrug.org</a><br>
Archives: <a href="http://lists.lrug.org/pipermail/chat-lrug.org" rel="noreferrer" target="_blank">http://lists.lrug.org/pipermail/chat-lrug.org</a><br>
Manage your subscription: <a href="http://lists.lrug.org/options.cgi/chat-lrug.org" rel="noreferrer" target="_blank">http://lists.lrug.org/options.cgi/chat-lrug.org</a><br>
List info: <a href="http://lists.lrug.org/listinfo.cgi/chat-lrug.org" rel="noreferrer" target="_blank">http://lists.lrug.org/listinfo.cgi/chat-lrug.org</a><br>
</div></div></blockquote></div><br></div>