When ‘null’ is not good enough

As an experienced .NET developer, you have to deal with NullReferenceException occurrences every day. Am I right? I firmly believe this is a design problem. Please, consider the following code sample:

public interface IEmployeeRepository
{
  Employee GeyById(string id);
}

class EmployeeRepository : IEmployeeRepository
{
  public Employee GetById(string id)
    => new DbContext().Find(id);
}

What should be the result of the GetById execution? An Employee instance, right? Unfortunately, this is not the case all the time. If there is no record with the specified id, the result would be null. Maybe you think that is ok, but I don’t think so.

The fact is the code is not entirely clear about what should be the expected result of the method execution. As an experienced developer, sometimes you will check the method result, but sometimes not…

My proposal would be to start using a container indicating that, at times, the result would be “no result.”

public interface IEmployeeRepository
{
  Option<Employee> GeyById(string id);
}

class EmployeeRepository : IEmployeeRepository
{
  public Option<Employee>; GeyById(string id)
    =>; new DbContext().Find(id);
}

Now, the code is clearer. Don’t you think?

What about the Option implementation

The Option Type is just a regular C# struct that provides implicit conversions and very basic functional operations.

The most interesting thing about this approach is that programmers consuming the EmployeeRepository from the previous example would not have direct access to the (possible) employee instance. The only way to get it would be thorugh the container.

public IActionResult Get(string id) => _repository.GetById(id)
  .Match<IActionResult>(
    some: employee => Ok(employee),
    none: () => NotFound()
  );

NullReferenceExcption is gone by design.

Nice, right? Option Type is available on my github.

Compartilhe este insight:

Elemar Júnior

Sou fundador e CEO da EximiaCo e atuo como tech trusted advisor ajudando diversas empresas a gerar mais resultados através da tecnologia.

Elemar Júnior

Sou fundador e CEO da EximiaCo e atuo como tech trusted advisor ajudando diversas empresas a gerar mais resultados através da tecnologia.

Mais insights para o seu negócio

Veja mais alguns estudos e reflexões que podem gerar alguns insights para o seu negócio:

Em tempos onde tanta gente parece saber muito sobre tanta coisa, é interessante resgatar o pensamento da polêmica professora Marilena...
In this post, I would like to share my current reading list. If you are reading some of these books,...
Há alguns anos, Alistair Cockburn propôs um padrão bem interessante. Citando suas palavras, a ideia central é: Permitir que a interação com uma aplicação seja idência...
Há muitos anos, tinha o hábito de fazer elogios públicos a tudo que achava que estava sendo bem-feito. Achava honestamente...
O comportamento que descrevo nesse post é extremamente nocivo a carreira de todo técnico. Geralmente resulta em desgaste desnecessário e...
What kind of optimizations could we expect from the C# compiler and the JIT? In this post, I would like...