Programming with Result

Hey everyone,

a few days back, I started a thread asking for feedback on articles about using algebraic data types in Kotlin. A number of the comments diverged towards the topic of Result, both in Kotlin and various other languages, which was, funnily enough, the next topic I wanted to get feedback on anyway.

So here we go. Each article is about a 4-5 min read (friend links, as always):

  1. Motivation

Discussing all the problems with exceptions, how they’re GOTO in disguise, break basic OOP principles and what we actually use them for. A teaser of something better than exceptions.

  1. Returning a Result

How to solve all the problems of exceptions by instead returning values. An introduction to the Result class, and how it makes handling unexpected errors a breeze. A quick note about how it relates to actual functional programming.

  1. kotlin.Result

A short tour of the most important standard library functions for working with Result – general transformation using fold(), retrieving values using getOrThrow(), getOrElse()/getOrDefault(), mapping success using map()/mapCatching(), mapping failure using recover()/recoverCatching() and peeking using onSuccess()/onFailure().

  1. Combining and Composing Results

Writing actual business code using multiple functions that return Result, with a short note on railway oriented programming, monads, and sequencing. Converting other types into Result.

  1. Considerations

What the true performance cost of exceptions is and how this relates to using Result. Things to keep in mind when using Result in a multithreaded or coroutine context.

Questions/Topics for discussion:

  • I’m interested in practical experience using Result in production:
    • Do you?
    • If you do, do you fully embrace it, i.e. basically start each non-trivial method with runCatching (or Either.catch, if Arrow is your thing)? Is this something you would advocate in Kotlin, and why or why not?
    • Are there any caveats or other considerations that aren’t immediately apparent, apart from the ones I mentioned?
  • I’d be grateful for feedback on the content of the articles as well. Especially the parts where:
    • I diss exceptions – what I write seems at least ostensible, and searching around, it would seem that articles sharing the same view are not uncommon, but I’m still not sure this is an uncontentious point of view
    • I try to make connections with functional programming – most of the things I mention I sorta-kinda understand intuitively, but I’m not confident that my formulations are what they’re supposed to be

Thanks!

submitted by /u/shadow5827193
[link] [comments]