Result is all I need

Result vs Exceptions: Semantics and Performance

  • Several comments compare Rust’s Result/? style to exceptions: behavior is similar, but implementation differs.
  • Exceptions can optimize for fast happy-path and pay cost only on the slow path (stack unwinding, stack traces).
    By contrast, Result forces a branch on every call site, plus extra bookkeeping if you want traces.
  • Others argue branch prediction makes that overhead negligible in many real cases and that mixing “expected failures” into exception mechanisms leads to frequent use of the slow path anyway.

When to Use Result vs Exceptions

  • Common view:
    • Expected, recoverable conditions (e.g., email already taken, invalid input, timeouts) → Result / explicit return value.
    • Truly exceptional / invariant-violating situations → exceptions or panics that terminate the current unit of work.
  • Misuse patterns are criticized, especially “catch-log-rethrow everything” and using exceptions as regular control flow.
  • There’s a long subthread debating terminology: “error” vs “exception” vs “bug,” business-rule violations vs environment-rule violations, and how exceptions relate to programmer mistakes. No consensus; the line is acknowledged as partly conventional.

Kotlin, Java, and Library Ecosystems

  • Kotlin’s built-in Result<T> is widely mentioned:
    • Pros: explicit error handling, works nicely with suspend functions and reactive APIs.
    • Cons: error constrained to Throwable, pushing developers back into exception-based domain modeling and incurring stack-trace overhead.
  • Multiple alternatives are suggested: third-party Result<T,E>, Arrow’s typed errors, Scala/Vavr Try/Either.
  • Some use Result extensively in multiplatform API clients, but note friction when exposing them to JavaScript or Java consumers.

Checked Exceptions vs Result

  • Several note that Result<T,E> is conceptually similar to Java’s checked exceptions: a function effectively returns T | E.
  • However, Java’s model is criticized for:
    • Poor integration with generics and functional APIs (streams, higher‑order functions).
    • Verbose propagation and wrapping, leading many to convert checked to unchecked exceptions.
    • Implicit propagation vs Rust-style explicit ?.
  • Others miss checked exceptions and see Result as mainly a syntactic/ergonomic shift, not a new idea.

Ergonomics, Style, and Critiques of the Example

  • Some like monadic chaining (map, flatMap, fold) for clarity and composability; others find it unreadable and harder to debug than straightforward if/else or try/catch.
  • Comments point out flaws in the article’s sample code (duplicated parameters, unclear service behavior, missing logging) and argue that Result alone doesn’t fix deeper design issues.
  • Several argue Result works best in languages with good union types and exhaustive pattern matching; without that, nesting and boilerplate grow.