Hacker News new | ask | show | jobs
by Joker_vD 1749 days ago
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.
2 comments

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.

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.