Rohit Bajaj
asked on
Avoiding null checks in java
Hi,
I have the following code :
if (task.getCompletor() != null) {
task1.setCompletorGuid(tas k.getCompl etor().get UserGuid() );
}
The getCompletor returns a User - :
public WorkflowUser getCompletor() {
return completor;
}
what are the ways i could avoid the null check.
One way i can think of to user a NullWorkflowUser class so instead of directly returning the completor from the getter i return this object in case its null this way i dont have to have a null check and i could write the definitions which will be null for the methods inside it.
I have read about Optionals but never used it.
Can that be used here.
Which is a better approach the NullWorkflowUser or the Optional one.
And any other alternatives in java.
Thanks
I have the following code :
if (task.getCompletor() != null) {
task1.setCompletorGuid(tas
}
The getCompletor returns a User - :
public WorkflowUser getCompletor() {
return completor;
}
what are the ways i could avoid the null check.
One way i can think of to user a NullWorkflowUser class so instead of directly returning the completor from the getter i return this object in case its null this way i dont have to have a null check and i could write the definitions which will be null for the methods inside it.
I have read about Optionals but never used it.
Can that be used here.
Which is a better approach the NullWorkflowUser or the Optional one.
And any other alternatives in java.
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.