curiouscoderscommunity / curious-coders

0 stars 0 forks source link

Throw errors to control the flow of your code #36

Open bouwe77 opened 1 year ago

bouwe77 commented 1 year ago

I think it is super bad to throw errors yourself in your code, because it almost always means you take a shortcut, instead of just structuring your code in such a way that the will do and not do things, just by checking.

Example: Pass arguments to a function. When these arguments are invalid or unexpected, throw an error. Why not just prevent this from happening by input checking, type safety, or whatever tools you have nowadays, making your code much nicer to read, understand and maintain?

Often the "fix" is to have your code just do nothing when something occurs that should not happen. If that than means something does not work, you will create a test for it. And then fix it to make the test pass...

Throwing errors should only be done in really bad situations, and I think many situations can just be prevented.

And throwing errors is code you made, so you need to test it as well...

And just to be clear, this is not about runtime errors coming from code you don't control. This is really about you throwing exceptions in your own code.