top | item 38215193

(no title)

BMorearty | 2 years ago

> In Ruby it's common to use exceptions for control flow.

I think this is just plain incorrect. The example given later in this paragraph is the Rails `update` method--but the approach used in all canonical Rails examples and generators is the non-exception version of `update`.

discuss

order

ljm|2 years ago

The examples show you a simple way to do things that limit complexity, especially for learners. Any decently sized library or application switches to descriptive error classes once they become sufficiently complex, because stateful error handling (as ActiveRecord does it) is painful.

I'd argue that now Ruby has some kind of pattern matching, it can take the place of using exceptions for control flow. You can just return the class itself instead of raising it, then match on it.

thibaut_barrere|2 years ago

I do not find it incorrect at all, as a Ruby user since 2004. A lot of gems use exceptions for control flow and it is also common in apps or libraries I've written or maintained (just a data point).

lcnPylGDnU4H9OF|2 years ago

> A lot of gems use exceptions for control flow

Do you happen to have an example of such control flow to link to? Not that I don't believe you but I wonder if there's a difference in what is meant by "control flow" between commenters.

  begin
    do_stuff!
  rescue MyFirstLibraryError => error
    # handle first
  rescue MySecondLibraryError => error
    # handle second
  end
That could be "control flow" to one person and "error handling" to another. Or even both to a third person.

whalesalad|2 years ago

Ruby != Rails. There are a lot of bang! methods that will raise on error in Rails. But in general Ruby is just like Python in that it is indeed common to use exceptions for control flow.

BMorearty|2 years ago

I know Ruby != Rails but the example wasn’t mine. It was from the paragraph I quoted from the post, which conflates the two.

It may be true in Python--I don’t use it much--but I know Ruby well. It was my primary language for 13 years, at multiple companies. I taught it to over a thousand engineers at Airbnb over the course of five years. I still disagree that it is common to use exceptions for control flow.

Maybe it’s a matter of the interpretation of the word “common.” ¯\_(ツ)_/¯