I come from a heavy Java background; I like my optionals.
Quick thoughts on why to use Optionals
1. Avoiding Null Pointer Exceptions: Optionals provide a way to avoid null references and null pointer exceptions, which are a common source of bugs and crashes in software.
2. Improved Code Readability: When you see an Optional in a method's return type or as a parameter, it signals that the value might not always be there, making the code's behavior more apparent.
3. Encouraging Explicit Handling: When working with Optionals, developers are encouraged to explicitly handle both the presence and absence of a value, leading to more robust and predictable code.
public class MyOptional<T> {
private T value;
public MyOptional(T value) {
this.value = value;
}
public Boolean isPresent() {
return value != null;
}
public T get() {
return value;
}
}
We could use MyOptional(sObjectType/sObject) instead , but the strong casting could be verbose and error prone.
Perhaps I am looking at things wrong, curious to hear everyone's opinion :)
Thanks
Great post!
I think the best way to use Optionals in our environment, if at all, will depend on the specific use case. Hard to find a silver-bullet... async over-usage is an example
(I know you, personally, have experienced folks that use async as the default, where synchronous is an exception. We know this mindset does have its own treasure trove of associated issues.)
However, I think that they are a valuable tool that can help to improve the quality of code.
I guess what I'm saying is that your level of familiarity with the toolset provided is invaluable for the engineer-mindset of finding the best solution rather than the most familiar to the developer.