Optional is in air due to coronavirus, everything is becoming optional like optional public gathering , optional work from home, optional travel etc.
I though it is good time to talk about real "Optional" in software engineering that deals with NULL reference.
Tony Hoare confessed that he made billion dollar mistake by inventing Null. If you have not seen his talk then i will suggest to have look at Null-References-The-Billion-Dollar-Mistake.
I will share some of the anti pattern with null and how it can be solved using abstraction like Optional or MayBe.
For this example we will use simple value object that can have some null values.
This value object can have null value for email & phone number.
Scenario: Contact Person on both email and phone number
Not using optional
First attempt will be based on checking null like below
This is how it has been done for years. One more common pattern with collection result.
Use optional in wrong way
This is little better but all the goodness of Optional is thrown away by adding if/else block in code.
Always Happy optional
It is good be happy but when you try that with Optional you are making big assumption or you don't need optional.
Nested property optional
For this scenario we will extend Person object and add Home property. Not everyone can own home so it is good candidate that it will be not available .
Lets see how contacting person scenario work in this case
This is where it start to become worse that code will have tons of nested null checks.
Priority based default
for this scenario we first try to contact person on home address and if it is not available then contact on office address.
Such type of scenario require use of advance control flow for early return and makes code hard to understand and maintain.
These are some of the common pattern where optional are not used or used in wrong way.
Optional usage patterns
Lets look at some of good ways of using optional.
Make property optional based on domain knowledge
It is very easy to makes property optional.
Yes it is allowed to make get Optional, no one will hang you for that and feel free to do that without fear. Once that change is done we can write something like below
It looks neat, first step to code without explicit if else on application layer.
Use some power of Optional
Optional is just like stream, we get all functional map,filter etc support. In above example we are checking for OptIn before contacting.
Always happy optional
Always happy optional that calls "get" without check will cause runtime error on sunday midnight, so it advised to use ifPresent
Nested Optional
Flatmap does the magic and handles null check for home and convert insurance object also.
Priority based default
This example is taking both home & office address and pick the first one that has value for sending notification. This particular pattern avoids lots of nested loops.
Else branch
Optional has lots of ways to handle else part of the scenario like returning some default value(orElse) , lazy default value (orElseGet) or throw exception(orElseThrow).
What is not good about optional
Each design choice has some trade off and optional also has some. It is important to know what are those so that you can make careful decision.
Memory overhead
Optional is container that holds value, so extra object is created for every value. Special consideration is required when it holds primitive value. If some performance sensitive code will be impacted by extra object creation via optional then it is better to use null.
Memory indirection
As optional is container , so every access to value need extra jump to get real value. Optional is not good choice for element in array or collection.
No serialization
I think this is good decision by Jdk team that does not encourage people to make instance variable optional. You can wrap instance variable to Optional at runtime or when required for processing.
I though it is good time to talk about real "Optional" in software engineering that deals with NULL reference.
Tony Hoare confessed that he made billion dollar mistake by inventing Null. If you have not seen his talk then i will suggest to have look at Null-References-The-Billion-Dollar-Mistake.
I will share some of the anti pattern with null and how it can be solved using abstraction like Optional or MayBe.
For this example we will use simple value object that can have some null values.
This value object can have null value for email & phone number.
Scenario: Contact Person on both email and phone number
Not using optional
First attempt will be based on checking null like below
This is how it has been done for years. One more common pattern with collection result.
Use optional in wrong way
This is little better but all the goodness of Optional is thrown away by adding if/else block in code.
Always Happy optional
It is good be happy but when you try that with Optional you are making big assumption or you don't need optional.
Nested property optional
For this scenario we will extend Person object and add Home property. Not everyone can own home so it is good candidate that it will be not available .
Lets see how contacting person scenario work in this case
This is where it start to become worse that code will have tons of nested null checks.
Priority based default
for this scenario we first try to contact person on home address and if it is not available then contact on office address.
Such type of scenario require use of advance control flow for early return and makes code hard to understand and maintain.
These are some of the common pattern where optional are not used or used in wrong way.
Optional usage patterns
Lets look at some of good ways of using optional.
Make property optional based on domain knowledge
It is very easy to makes property optional.
Yes it is allowed to make get Optional, no one will hang you for that and feel free to do that without fear. Once that change is done we can write something like below
It looks neat, first step to code without explicit if else on application layer.
Use some power of Optional
Optional is just like stream, we get all functional map,filter etc support. In above example we are checking for OptIn before contacting.
Always happy optional
Always happy optional that calls "get" without check will cause runtime error on sunday midnight, so it advised to use ifPresent
Nested Optional
Flatmap does the magic and handles null check for home and convert insurance object also.
Priority based default
This example is taking both home & office address and pick the first one that has value for sending notification. This particular pattern avoids lots of nested loops.
Else branch
Optional has lots of ways to handle else part of the scenario like returning some default value(orElse) , lazy default value (orElseGet) or throw exception(orElseThrow).
What is not good about optional
Each design choice has some trade off and optional also has some. It is important to know what are those so that you can make careful decision.
Memory overhead
Optional is container that holds value, so extra object is created for every value. Special consideration is required when it holds primitive value. If some performance sensitive code will be impacted by extra object creation via optional then it is better to use null.
Memory indirection
As optional is container , so every access to value need extra jump to get real value. Optional is not good choice for element in array or collection.
No serialization
I think this is good decision by Jdk team that does not encourage people to make instance variable optional. You can wrap instance variable to Optional at runtime or when required for processing.
No comments:
Post a Comment