Re:Effective Go
by closeparen on Sunday October 03, 2021 @02:24PM
(#28739322)
When a controller calls a couple of gateways and a database access layer, it is almost never holding resources. I would agree that testing error handling is more interesting when there are resources to clean up or fallback logic to implement. That's just very rarely the case. Mostly I just need to bail out of the request.
Java has try-with-resources and Python has context managers, I wouldn't consider either of them harder to use correctly than defer.
This isn't theoretical. I review a lot of Python code and I would say in over 20% of cases I see a try block with a `finally` longer than two lines, it mistakenly uses a variable that might not be set when an exception is thrown earlier than the writer expected.