3 ms·
Often people will ask how to follow these principles in the world of I/O. The answer is incredibly simple: with values. Return a DTO that encapsulates all the
by squirrelicus 8y ago
Often people will ask how to follow these principles in the world of I/O. The answer is incredibly simple: with values.
Return a DTO that encapsulates all the business information about the result state of the I/O call. Do not throw exceptions. Log them if you like, but you must return all the data necessary for the business logic to react to failure conditions, encoded in your own use case specific structure.
If you don't care about whether you failed due to timeouts or refused connections or query parse failures what have you, don't return that data, just return a hasError kind of property on the return structure (bonus points if your language supports discriminated unions, but this is not necessary). If the parent logic needs to react to timeouts and failures to connect differently, then catch those exceptions or state separately and return didTimeout or cantConnect flags separately.
Values values values
- the_gipsy 8y agoI would prefer a ‘Result<ValueType, ErrorType>’ over stuffing weird ‘hasError’, ‘didTimeout’, etc flags everywhere. It’s much safer to type-check at compile time that you have discriminated between result and error, than to hope that you are ‘if’-checking some flags at the right time at runtime.
- squirrelicus 8y agoThat is another way to accomplish the same goal. This trade-off is more aesthetic than architectural and is just a matter of language support