dsdshcym / dsdshcym.github.io

https://yiming.dev/
1 stars 1 forks source link

How "let it fail" leads to simpler code - Yiming Chen #28

Open utterances-bot opened 2 years ago

utterances-bot commented 2 years ago

How "let it fail" leads to simpler code - Yiming Chen

The biggest lesson I learned from the "Let it fail" philosophy, is to distinguish expected errors and unexpected errors. So we can safely ignore the unexpected, and only handle the expected errors in our business logic.

https://yiming.dev/blog/2022/07/10/how-let-it-fail-leads-to-simpler-code/

leed25d commented 2 years ago

you said:

When you really pass 0 as the dividend, the runtime doesn't know how to proceed.

I think that might have meant

When you really pass 0 as the divisor, the runtime doesn't know how to proceed.

Unless I have completely mistaken the point you are making.

c4710n commented 2 years ago

Thanks for sharing this. ;)

Not long ago, I pondered over this question, too.


Here are my notes:

"Let it crash" is not equal to "Let everything crash". Expected errors are parts of your core logic, too. Don't let it crash.

To summarize, in BEAM-based languages:

  1. Your code should handle all the expected conditions which include:
    • the happy path of the core logic.
    • expected errors (In a sense, they are also the happy path of the core logic, because they are expected, they don't send you into a panic about the unknown)
  2. All the unexpected errors should be handled by supervisors. The possible operations are:
    • logging the unexpected errors for troubleshooting later (If these errors can be handled by your code, try to transform them as expected errors)
    • saving the state of processes
    • restarting the related processes
    • ……
dsdshcym commented 2 years ago

@leed25d You are totally right, thank you for pointing it out!

It's funny because I actually googled dividend when I wrote that sentence, and somehow I decided to use it. 😂

Anyway, it should be fixed now. Appreciated 🙏

dsdshcym commented 2 years ago

@c4710n That's a great summary

If you don't mind, may I add it as the TL;DR section at the beginning of the article?

c4710n commented 2 years ago

@dsdshcym That's totally fine.

c4710n commented 2 years ago

@dsdshcym And, feel free to edit it.