DEV Community

Discussion on: Code Smell 157 - Balance at 0

Collapse
 
jmfayard profile image
Jean-Michel πŸ•΅πŸ»β€β™‚οΈ Fayard • Edited

I have not read your complete series, but raising an exception is also an error for me :)
Throwing exceptions should only be done in exceptional cases where the reasonable thing to do is to crash.
The network not working on the other hand is just life and something to be dealt with.
So I would use a plein result type like all Either

Collapse
 
mcsee profile image
Maxi Contieri

Exceptions are for exceptional cases

The contrary is another smell

but 'network not working' is an exceptional case that, IMHO, should be handled by the UI.

and return codes are not an option

Collapse
 
jmfayard profile image
Jean-Michel πŸ•΅πŸ»β€β™‚οΈ Fayard • Edited

I was sure you handled that in your Impressive serie!

I completely agree with Use Exceptions just for unexpected situations and not for flow control because they are handy goto.

I go further than you though. By unexpected situations, I mean a programmer error (like your for each loop), or your VM going out of memory.

The network to fail on the other hand is a case that a programmer must totally expect. So I would use a Either<Failure, Success> to force him to always handle this case.

Obviously that works only in programming who supports lambdas and generics. If not fallback on the handy gotos.

Thread Thread
 
mcsee profile image
Maxi Contieri

can you elaborate on Either
is this some kind of flag boolean function ?

Thread Thread
 
jmfayard profile image
Jean-Michel πŸ•΅πŸ»β€β™‚οΈ Fayard

Going tomorrow on a trip, and short of time, but I've found a DEV article that sums it well