Be Kind to Future-You
An early mentor of mine described the starting point of a development effort, feature implementation, or a debugging session as
The Point of Maximum Ignorance
Besides it being the point where the client wants a fixed-price bid, it is also the point where Past You can do the most good for Future You
When one is coding a feature and comes to the error handling, one has a choice: throw an Exception("Something went wrong")
or pay some of the debugging work forward. That point of error handling is The Point of Penultimate Wisdom1(PoPW)
The code knows
- what it was trying to do
- what step went wrong
- what values it was using at that step
- (in many cases) what code was calling it and the values it passed in
- and more
One can find wisdom after something went wrong by furiously2 instrumenting the code and deploying it into production, by swimming upstream in the logs to find the butterfly wingbeat that started the particular process, and by stepping through a half-dozen procedure calls to get to the point of failure to find the values and the exact point of failure, but the Point of Penultimate Wisdom already knows all the things. The choice is whether to have the PoPW communicate it in the original error message
Pre-loading debug information — as an investment — minimizes the operational work needed during the chaos, pressure, and confusion of a failure. While not all the work is guaranteed to return dividends, its presence (like insurance) allows some confidence that failures can be diagnosed quickly (as opposed to being a terrifying Complete Unknown)
One can catch every exception and explicitly log exactly what was happening (and re-raising the exception, one hopes). Modern systems automatically provide a lot of information about errors, even in production builds, without added debugging information. One must balance the code-clutter of reporting the details of a detectable error3 against the future value of finding a problem quicker
While the person debugging a failure might not be the person who last touched the code, it does not really matter
Eagleson’s Law: Any code of your own that you haven’t looked at for six or more months might as well have been written by someone else
Do your (future) self a favour