News for hackers. Stuff that matters.
Re:Away from Exceptions: Errors as Values by yakubin on Monday August 30, 2021 @06:46AM (#28354374)
Tbh, to me the first one is easier to read. There's less jumping.

But both are terrible. It should just be a bunch of if (...) { ... } else if (...) { ... } else { ...} etc. with no mutation of variables (what are all those v += 1 for?).

Re:Away from Exceptions: Errors as Values by Joker_vD on Monday August 30, 2021 @07:41AM (#28354639)
Those v += 1 implement the exact specification given above:

    1. Parse an integer N from a string.
    2. If N is NaN, fail with an error. Otherwise, increment N by 1.
    3. If N is > 3, fail with an error. Otherwise, increment N by 1.
    4. If steps 1-3 failed, set N to 3.
    5. Increment N by 1.
Those are quite strange requirements, and the resulting second code looks strange too, but... it faithfully and obviously correctly represents the given specification.
Re:Away from Exceptions: Errors as Values by Jonathanks on Monday August 30, 2021 @04:36PM (#28360587)
The example is strange and contrived. I couldn't torture the logic enough to demonstrate how readability breaks down with exceptions. Reading through the comments, I realise that even if the code is unreadable with exceptions, it can be rewritten to be readable. I updated the article to highlight not only readability, but also compositionality. I also tried to draw parallels with Promises (which are also monadic). I tried to target the article to intermediate programmers that may not already know most of the concerns raised in the comments, and I surely can't cover enough space on error handling without writing a book.

Thank you for the comments. I've learned more from reading the comments.

Re:Away from Exceptions: Errors as Values by Cthulhu_ on Monday August 30, 2021 @08:35AM (#28354981)
It's the kinda assignment where you should look at a higher level - what does it do? What is N used for? What are the possible inputs?

I mean if you start with a set of tests instead of pseudocode written down in text you could probably write something smarter.