Maybe Functions
blog.benwinding.com
Maybe Functions
1–10 of 103 posts
Re: Maybe Functions
#2Re: Maybe Functions
#3Re: Maybe Functions
#4Re: Maybe Functions
#5What on earth did I just read
Re: Maybe Functions
#6You can remove the null checks and the software will raise a null pointer exception. In the first example, could raise a NotLoggedInException.
It's still a maybe function, but you have a mechanism for expressing the why-notness of the function run, as opposed to returning a generic null.
As an aside, I prefer the "Unless" model of thinking vs the "Maybe" model of thinking. It's biased towards success. It presumes that the function is most likely to do something unless a precheck fails. filterBestFriendsUnless vs maybeFilterBestFriends. getUserUnless vs maybeGetUser. If we go this far down the rabbit hole, we can assume there's always an "unless". Programs run out of memory, stacks have limited depth. There are maybe conditions for which we can not account.
Re: Maybe Functions
#7There's an urge to return Optional but now you must check Optional.isPresent AND object != null.
Re: Maybe Functions
#81. Parse, don't validate (https://lexi-lambda.github.io/blog/2019/11/05/parse-don-t-va...)
2. Pipeline-oriented programming (https://fsharpforfunandprofit.com/pipeline/)
In my experience, the "best" code (defining "best" as some abstract melange of "easy to reason about", "easy to modify", "easy to compose", and "easy to test") ends up following the characteristics outlined by the sum of these three essays — strictly and rigorously elevating exceptions/failures/nulls to first-class types and then pushing them as high in the stack as possible so callers _must_ deal with them.
Re: Maybe Functions
#9Solution3 for their example:
function maybeRenderBestFriends() {
const user = maybeGetUser();
if(user!=null){
const friends = maybeGetFriends(user);
const bestFriends = maybeFilterBestFriends(friends);
return render(bestFriends);
}
return null;
}