3 ms·
Yessss... I'm a big advocate of using error objects rather than throwing exceptions. I think the industry over-uses throwing exceptions and it's basically turne
by g_delgado14 4y ago
Yessss... I'm a big advocate of using error objects rather than throwing exceptions. I think the industry over-uses throwing exceptions and it's basically turned into the new GOTO statement.
I wrote an article about this as well but for TypeScript [0] and I've got a relatively popular npm package that does what I mention in the article [1]
---
[0] - https://gdelgado.ca/type-safe-error-handling-in-typescript.html#title https://gdelgado.ca/type-safe-error-handling-in-typescript.h...
[1] - https://www.npmjs.com/package/neverthrow https://www.npmjs.com/package/neverthrow
- throwaway290 4y agoI'm not surprised. TS exceptions feel limiting and awkward and I don't like using them, compared with Python's. Add to that, TS's powerful typing makes it easier to precisely type return values compared to Python's annotations, but still you cannot type what function throws in TS. In Go, I tend to use the return error pattern. In Python, I raise and try. With TS, I do both (but slightly more confident with returning errors). IMO, rule of thumb: try not to surprise the developer after you, who most likely will be hired for knowing this language not this project (note: that developer may be future yourself). When in Rome...
- g_delgado14 4y ago... do as the Roman's do! Agreed :)
- int_19h 4y agoIt really depends on the language. In Python, exceptions are idiomatic even for non-fatal failures, the runtime is optimized for that performance-wise, and it's what API clients expect.
- Supermancho 4y ago> I think the industry over-uses throwing exceptions and it's basically turned into the new GOTO This is a misunderstanding of what's bad about GOTO versus jumping to a label. eg Functions are labels you GOTO. The difference between a GOTO instruction and a Function call, for the purposes of differentiating the methodology of code routing, is that a Function is part of a stack that returns in a serial fashion. ie Each element of the stack returns to the parent before disappearing, which can be though of as "unwinding". An exception unwinds a Function stack without restriction (eg no consideration of signatures like args/returns), until it is caught in that stack, making them conceptually and practically different from GOTOs.