There is no value in checking if a service is alive. Absolutely not. Why?
if(serviceIsAlive()) { callService(); } else { handleFailure() }
Do you see a problem with this snippet? What happens if the service is disconnected between checking the serviceβs health and the time it was called? This is a race condition and an error awaiting its appearance. So what you need to do, even if you can check the status of the service, is:
if(serviceIsAlive()) { try { callService(); } catch(CommunicationException) { handleFailure(); } } else { handleFailure(); }
But in this block, the handleFailure () call is in two different places - we have two different ways to handle the same error condition - which seems bad. Thus, it can be safely reduced to:
try { callService(); } catch(CommunicationException) { handleFailure(); }
source share