HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Enumerating new articles whose publication start date has passed

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
newwhosepassedenumeratingdatehaspublicationarticlesstart

Problem

(cn.PublishEnd == null || cn.PublishEnd < DateTime.Now)


vs

(cn.PublishEnd ?? DateTime.MinValue < DateTime.Now)


Which is more readable?
I'm inclined towards the second form, but something tells me I'm wrong.

Context:

namespace Damnation.Website.Main.Business.Extensions
{
    public static class CommunityNews
    {
        public static IEnumerable Published(this ObjectSet table)
        {
            return table.Where(cn => cn.PublishStart > DateTime.Now && DateTime.Now > (cn.PublishEnd ?? DateTime.MinValue)).OrderByDescending(cn => cn.PublishStart);
        }
    }
}


Types

DateTime PublishStart
DateTime? PublishEnd

Solution

As for me, the first form is more readable because it clearly describe the condition. It's easy to read and understand. The second form is a little confusing. The first thought I had was: What is the DateTime.Zero? Is this a default value for PublishEnd? If yes then why not to use the DateTime.MinValue? Maybe it's not a default value and I get it wrong somewhere? Of course these thoughts took only seconds but nevertheless.
In general I think that ?? should be used to return the default value with expected type. The default value of the PublishEnd should be of DateTime type. But the second form distorts the meaning of ?? and returns bool type instead, and it’s confusing.

Edited:
If you want to read is like you said: "if now is greater than (end date or earliest date)"
then you should write it like:

(DateTime.Now > cn.PublishEnd ?? DateTime.MinValue)


And this, third form is better than first and second.

Code Snippets

(DateTime.Now > cn.PublishEnd ?? DateTime.MinValue)

Context

StackExchange Code Review Q#4283, answer score: 4

Revisions (0)

No revisions yet.