Null Is the Saruman of Static Typing
-
Upload
globallogic-ukraine -
Category
Engineering
-
view
327 -
download
0
description
Transcript of Null Is the Saruman of Static Typing
![Page 1: Null Is the Saruman of Static Typing](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b745d24a795955768b45e8/html5/thumbnails/1.jpg)
Null is the Saruman of static typing
by @AntyaDev
![Page 2: Null Is the Saruman of Static Typing](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b745d24a795955768b45e8/html5/thumbnails/2.jpg)
![Page 3: Null Is the Saruman of Static Typing](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b745d24a795955768b45e8/html5/thumbnails/3.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b745d24a795955768b45e8/html5/thumbnails/4.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b745d24a795955768b45e8/html5/thumbnails/5.jpg)
![Page 6: Null Is the Saruman of Static Typing](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b745d24a795955768b45e8/html5/thumbnails/6.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b745d24a795955768b45e8/html5/thumbnails/7.jpg)
Concentrating on where the problem
is???
API ???
Type System
![Page 8: Null Is the Saruman of Static Typing](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b745d24a795955768b45e8/html5/thumbnails/8.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b745d24a795955768b45e8/html5/thumbnails/9.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b745d24a795955768b45e8/html5/thumbnails/10.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b745d24a795955768b45e8/html5/thumbnails/11.jpg)
Why?
What?How?
MONAD
![Page 12: Null Is the Saruman of Static Typing](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b745d24a795955768b45e8/html5/thumbnails/12.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b745d24a795955768b45e8/html5/thumbnails/13.jpg)
http://habrahabr.ru/post/200100/
Proof
![Page 14: Null Is the Saruman of Static Typing](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b745d24a795955768b45e8/html5/thumbnails/14.jpg)
![Page 15: Null Is the Saruman of Static Typing](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b745d24a795955768b45e8/html5/thumbnails/15.jpg)
![Page 16: Null Is the Saruman of Static Typing](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b745d24a795955768b45e8/html5/thumbnails/16.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b745d24a795955768b45e8/html5/thumbnails/17.jpg)
Optional<T> in Java 8
![Page 18: Null Is the Saruman of Static Typing](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b745d24a795955768b45e8/html5/thumbnails/18.jpg)
Thanks@AntyaDev