DEV Community

Cover image for Stop Using Exceptions for Control Flow: Railway-Oriented Programming in C#
Spyros Ponaris
Spyros Ponaris

Posted on

Stop Using Exceptions for Control Flow: Railway-Oriented Programming in C#

An expired coupon or insufficient credit is not a system crash—it is normal business behavior. Throwing exceptions for routine mistakes buries domain logic under nested if/else blocks, slows down execution, and forces callers to guess what might fail.

Railway-Oriented Programming (ROP) models workflows as a two-track railway:

  • Success Track: Data moves forward step by step through clean, linear functions.
  • Failure Track: Any failed rule switches tracks and skips all remaining steps—with zero throw or try/catch blocks.
Input ──► [ Validate ] ──► [ Get Coupon ] ──► [ Check Rules ] ──► [ Calculate ] ──► Success<decimal>
               │                  │                  │
               ▼                  ▼                  ▼
         (Failure Track) ─────────────────────────────────────────────────────────► Failure(Errors)

Enter fullscreen mode Exit fullscreen mode

Full Source Code: Explore the complete C# implementation, conditional workflows, and runnable console demo on GitHub at stevsharp/RailwayOrientedCSharp.


Why Traditional Exception Flow Hurts

  • Dishonest Signatures: A method returning Task<decimal> promises a number every time while hiding four or five hidden exceptions inside.
  • Fail-Fast Validation: Traditional guard clauses stop at the very first mistake, forcing users to fix and resubmit one error at a time.
  • Mixed Concerns: Logging, auditing, and side effects end up tangled directly inside business calculations.

How the Railway Pipeline Works

Instead of throwing exceptions, methods return a Result or Result<T> object that explicitly represents either a success value or a list of errors. Extension combinators handle the track-switching automatically:

  • Combine: Merges multiple validation checks so all input errors return together.
  • Bind / BindAsync: Chains operations that can fail, bypassing the step if already on the failure track.
  • BindIfAsync: Executes an optional async step only when a specific condition is met.
  • EnsureAsync: Evaluates a business rule and switches to the failure track if it fails.
  • MapAsync: Transforms the inner value when on the success track.
  • TapAsync: Runs side effects like logging or auditing without altering the pipeline data.

Old Way vs. New Way

Dimension Exceptions & if/else Railway-Oriented Programming
Return Type Task<decimal> (hides errors) Task<Result<decimal>> (explicit)
Validation Stops at first error Collects all errors via Combine
Control Flow Nested if/else + throw Flat pipeline with auto short-circuit
Side Effects Mixed into domain logic Isolated in TapAsync
Unit Testing Requires exception assertions Simple checks on IsSuccess and Errors

Rule of thumb: Use ROP for expected domain and validation errors. Keep throw strictly for unexpected system crashes (dropped database connections, null bugs).


References & Further Reading

Top comments (0)