A long precarious way…

TL;DR; = This post is about improving the way you write deep null checks in C#, i.e. a chain of If-not-null checks along the object hierarchy.

I am sure there have been times in your developer life when you saw something like this, 


class Person
{
public Address Address { get; set; }
}

public class Address
{
public State State { get; set; }
}

public class State
{
public int PinCode { get; set; }
}
...
...
var p = new Person();
...
...

if (p.Address != null && p.Address.State != null)
{
var pin = p.Address.State.PinCode;
}


Ugh.., yes you have… The point is you have to go through a lot of checks in between to reach the PinCode field. In this example there are just 2 checks for Address and State, but there could be more complicated scenarios. While talking about this, I do understand by the Law of Demeter we should not be having such deep checks in the first place. But for this blog post sake, consider this as some hairy legacy code. Moving on, there is a way you could do the same thing but in a much more readable way. What I want to do is in case of a null instance anywhere in the chain of objects, the final value should be a default decided by me, without any null reference error. Here is my attempt,




public static class Extension
{
public static TResult With<TSource,TResult>(this TSource source, Func<TSource,TResult> evaluator, TResult failureValue)
where TSource : class
where TResult : class
{
if (evaluator == null)
throw new InvalidOperationException("Boom!!!");
var res = source != null ? evaluator(source) : failureValue;
return res ?? failureValue;
}
}

public class Person
{
public Address Address { get; set; }
public static Person NullInstance
{
get
{
return new Person() { Address = Address.NullInstance };
}
}
}

public class Address
{
public State State { get; set; }
public static Address NullInstance
{
get { return new Address() { State = State.NullInstance }; }
}

}

public class State
{
public int PinCode { get; set; }
public static State NullInstance
{
get
{
return new State() { PinCode = -1 };
}
}
}

...
...

var pin = this.With(x => p, Person.NullInstance)
.With(x => x.Address, Address.NullInstance)
.With(x => x.State, State.NullInstance).PinCode;


Definitely more readable huh.

Temptations of Scala…

As I continue my romance with Scala language, I decided to take up the rubber duck code from Head First Design Patterns. The code already is implemented using Strategy pattern as we know. Just check the C# code juxtaposed against the Scala code, (dont ask which one is C# code)


class Program
{
static void Main(string[] args)
{
var actualDuck = new Duck(new ActualFlyBehavior(),
new ActualQuackBehavior());
actualDuck.DoFly();
actualDuck.DoQuack();
var rubberDuck = new Duck(new NoFlyBehavior(),
new NoQuackBehavior());
rubberDuck.DoFly();
rubberDuck.DoQuack();
Console.ReadLine();
}
}

public class ActualFlyBehavior : IFly
{
public void Fly()
{
Console.WriteLine("I am actually flying!!!");
}
}

public class ActualQuackBehavior : IQuack
{
public void Quack()
{
Console.WriteLine("I am actually quacking!!!");
}
}

public class NoFlyBehavior : IFly
{
public void Fly()
{
Console.WriteLine("---");
}
}

public class NoQuackBehavior : IQuack
{
public void Quack()
{
Console.WriteLine("---");
}
}

public class Duck
{
private readonly IFly fly;
private readonly IQuack quack;

public Duck(IFly fly, IQuack quack)
{
this.fly = fly;
this.quack = quack;
}

public void DoFly()
{
fly.Fly();
}

public void DoQuack()
{
quack.Quack();
}
}

public interface IFly
{
void Fly();
}

public interface IQuack
{
void Quack();
}
object Starter extends App
{
println("Hello World!!!")
val actualDuck = new ActualDuck
actualDuck.doFly()
actualDuck.doQuack()
val rubberDuck = new RubberDuck
rubberDuck.doFly()
rubberDuck.doQuack()
}

abstract class Duck
{
def doFly()
def doQuack()
}


class ActualDuck extends Duck with Fly with Quack
{

}

class RubberDuck extends Duck
{
def doFly() {println("---")}
def doQuack() {println("---")}
}

trait Fly
{
def doFly(){println("I am actually flying!!!")}
}

trait Quack
{
def doQuack(){println("I am actually quacking!!!")}
}