iteration cover image

iteration

Alternatives to Exceptions

Nov 2, 2018
40:10

Alternatives to Exceptions


  • Multiple return values in failures can be helpful -
  • Represent a process as an object “Download Provisionment” - This is a really interesting idea…. “MessageSend.new” - would actually handle a lot of the issues in the SMS interface concept we’ve been talking through.
  • “It’s tempting to raise an exception every time you get an answer you don’t like. But exception handling interrupts and obscures the flow of your business logic. Consider carefully whether a situation is truly unusual before raising an Exception.”
  • Diagnostics- don’t fail SO aggressively early that you don’t capture the diagnostics.

Examples of when to not "fail fast":

  1. test suites

Sometimes you just need a way to proceed through steps and have the system tell you what parts succeeded and what parts failed

Sideband data

When communication failures without resorting to exceptions, we need a sideband: a secondary channel of communication for reporting meta-information about the status and disposition of a process.

Multiple return values

hehe, this reminds me of stuff I do in javascript

  • ruby supports multiple return values in the form of array splatting. in JS, you could do this with destructuring

    def foo
    result = 42
    success = true
    [result, success]
    end

    result, success = foo
    puts "#{success ? 'succeeded' : 'failed'}; result #{result}"

Or you can use an open struct

def foo
  OpenStruct.new(:result => 42, :status => :success)
end

Output parameters

Caller-supplied fallback strategy

  • if we're not sure we want to terminate the execution of a long process by raising an exception, we can inject a failure policy into the process

    def make_user_accounts(host, failure_policy=method(:raise))
    # ...
    rescue => error
    failure_policy.call(error)
    end

    def provision_host(host, failure_policy)
    make_user_accounts(host, failure_policy)
    end

    policy = lambda { |e| puts e.message }
    provision_host("192.168.1.123", policy)

Picks

JP:

John:

Slack Video Calling + Collaboration

Get the Snipd
podcast app

Unlock the knowledge in podcasts with the podcast player of the future.
App store bannerPlay store banner

AI-powered
podcast player

Listen to all your favourite podcasts with AI-powered features

Discover
highlights

Listen to the best highlights from the podcasts you love and dive into the full episode

Save any
moment

Hear something you like? Tap your headphones to save it with AI-generated key takeaways

Share
& Export

Send highlights to Twitter, WhatsApp or export them to Notion, Readwise & more

AI-powered
podcast player

Listen to all your favourite podcasts with AI-powered features

Discover
highlights

Listen to the best highlights from the podcasts you love and dive into the full episode