SSW.CleanArchitecture icon indicating copy to clipboard operation
SSW.CleanArchitecture copied to clipboard

♻️ Using Result object as control flow instead of exceptions

Open christoment opened this issue 2 years ago • 2 comments

Premise:

  • Exceptions are slower than Result objects (https://youtu.be/a1ye9eGTB98 - quick benchmark shows ~1us slower)
  • Using Exceptions as a control flow is a questionable approach, since it allows code to jump from nested method all the way to the caller (questionable = both good and bad)
  • Using result objects everywhere means more codes to write

Consideration:

  • Using mixed method is a brain overload for a small performance benefit
  • We haven't seen any big performance problems yet on our clients

Proposed decision:

  • Keep using exceptions for now - keep it simple
  • When there's a need arise (e.g. performance requirement, validation requirement) - handle it case-by-case

christoment avatar Mar 13 '23 06:03 christoment

TODO: @christoment write a rule + maybe CTF

Hona avatar Mar 13 '23 07:03 Hona

Nick Chapsas has a good video on this: https://www.youtube.com/watch?v=YbuSuSpzee4

He leverages Language Extensions library: https://www.nuget.org/packages/LanguageExt.Core

Another option is Fluent.Results: https://www.nuget.org/packages/FluentResults

danielmackay avatar May 23 '23 11:05 danielmackay

Closing as this is a duplicate of #137

danielmackay avatar Oct 21 '24 04:10 danielmackay