Transcript
Page 1: Null Is the Saruman of Static Typing

Null is the Saruman of static typing

by @AntyaDev

Page 2: Null Is the Saruman of Static Typing
Page 3: Null Is the Saruman of Static Typing

class EmailContact { public string EmailAddress { get; set; } public bool IsEmailVerified { get; set; } }

anyone can set this to true

Rule 1: If the email is changed, the verified flag must be reset to false.

Rule 2: The verified flag can only be set by a special verification service

Page 4: Null Is the Saruman of Static Typing

abstract class EmailContact { public string EmailAddress { get; set; } }

class VerifiedEmail : EmailContact { }

class UnverifiedEmail : EmailContact { }

void SendEmail(VerifiedEmail email)

Page 5: Null Is the Saruman of Static Typing
Page 6: Null Is the Saruman of Static Typing

Our API

public Process GetProcessByName(string name)

var process = _winApiService.GetProcessByName(name)

if (process != null)

Unfortunately nothing forces us to handle this

Can be null?

Can return null?

var id = process.Id

Page 7: Null Is the Saruman of Static Typing

Concentrating on where the problem

is???

API ???

Type System

Page 8: Null Is the Saruman of Static Typing

Our API

public Nullable<Process> GetProcessByName(string name)

var process = _winApiService.GetProcessByName(name)

explicit nullable value

var id = process.Id // compile time error, process.Value.Id

Page 9: Null Is the Saruman of Static Typing

Avoid forcing errorsif (process != null)

Unfortunately nothing forces us to handle this

F# (pattern match expression)

match potentialValue with| None -> 0 // if you forget this line, the compiler complains| Some value -> value * value

if (process.IsSome)

Page 10: Null Is the Saruman of Static Typing

var customer = _customerService.GetCustomerById(12345);

if (customer != null){ var account = customer.GetAccount("blablabla"); if (account != null) { var interest = account.GetLastInterest(); if (interest != null) { ……..

Nested if’s problem

Page 11: Null Is the Saruman of Static Typing

Why?

What?How?

MONAD

Page 12: Null Is the Saruman of Static Typing

Our API

public Process StartProcess(string filePath)

Can throw exception?

public Exception<Process> StartProcess(string filePath)

explicit exception value

var process = _winApiService.StartProcess(filePath)

if (process.HasException) Logger.Log(process.Exception)

Page 13: Null Is the Saruman of Static Typing

http://habrahabr.ru/post/200100/

Proof

Page 14: Null Is the Saruman of Static Typing
Page 15: Null Is the Saruman of Static Typing
Page 16: Null Is the Saruman of Static Typing

By using Maybe{T} in my code I promise that all the code will be following good citizenship principles and will keep all object references non-null (initialized to some value from the default). This promise is to be uphold by a self-discipline and unit tests.This helps to avoid all null references completely.

Page 17: Null Is the Saruman of Static Typing

Optional<T> in Java 8

Page 18: Null Is the Saruman of Static Typing

Thanks@AntyaDev


Top Related