Sunday, June 3, 2018

Ternary all the things with C# 7




This code:
object NHibernate.UserTypes.IUserType.NullSafeGet(
    System.Data.Common.DbDataReader rs, string[] names,
    NHibernate.Engine.ISessionImplementor session, 
    object owner
)
{
    if (names.Length != 1)
        throw new System.InvalidOperationException("Only expecting one column...");

    object value = rs[names[0]];

    if (value is System.DateTime)
        return ((System.DateTime)value).ToUniversalTime();
    else
        return null;
}

Can be rewritten with C# 7's pattern matching:
object NHibernate.UserTypes.IUserType.NullSafeGet(
    System.Data.Common.DbDataReader rs, string[] names,
    NHibernate.Engine.ISessionImplementor session, 
    object owner
)
{
    if (names.Length != 1)
        throw new System.InvalidOperationException("Only expecting one column...");

    if (rs[names[0]] is System.DateTime value)
        return value.ToUniversalTime();
    else
        return null;
}

Nice, the variable declaration is inlined. It only means one thing, ternary!
object NHibernate.UserTypes.IUserType.NullSafeGet(
    System.Data.Common.DbDataReader rs, string[] names,
    NHibernate.Engine.ISessionImplementor session, 
    object owner
)
{
    if (names.Length != 1)
        throw new System.InvalidOperationException("Only expecting one column...");

    return rs[names[0]] is System.DateTime value ? value.ToUniversalTime() : (System.DateTime?) null;
}


And now that exceptions can be thrown inside of ternary statement? Ternary all the things!
object NHibernate.UserTypes.IUserType.NullSafeGet(
    System.Data.Common.DbDataReader rs, string[] names,
    NHibernate.Engine.ISessionImplementor session, 
    object owner
)
{            
    return 
        names.Length != 1 ? 
            throw new System.InvalidOperationException("Only expecting one column...")
        : rs[names[0]] is System.DateTime value ? 
            value.ToUniversalTime() 
        : 
            (System.DateTime?) null;
}


And who uses return statement in the 21st century? Use lambda expression syntax!
object NHibernate.UserTypes.IUserType.NullSafeGet(
    System.Data.Common.DbDataReader rs, string[] names,
    NHibernate.Engine.ISessionImplementor session, 
    object owner
)
=>
    names.Length != 1 ? 
        throw new System.InvalidOperationException("Only expecting one column...")
    : rs[names[0]] is System.DateTime value ? 
        value.ToUniversalTime() 
    : 
        (System.DateTime?) null;        

1 comment:

  1. the "var is type newVar" was a new trick to me in C#. It remind me of optional binding in Swift.
    And nicely played with the lambda finish. You forgot the mike-drop.

    ReplyDelete