Better Than Exceptions
Better Than Exceptions
Posted Jun 2, 2010 12:11 UTC (Wed) by farnz (subscriber, #17727)In reply to: Better Than Exceptions by ldo
Parent article: GCC begins move to C++
Actually, I much prefer avik's example to yours. As a competent C++ programmer who also knows C, I recognise the exception safety guarantee of auto_ptr, and I can guess that spinlock_t::guard provides the strong exception safety guarantee (an exception thrown that causes it to be destroyed returns it to the original state). I also know that the release method on auto_ptr returns the contained pointer and stops auto_ptr from deleting it.
As a result of this general C++ knowledge, and the assumption that the programmer hasn't been writing C in C++ (or otherwise behaved like a crack-addled monkey), I can see very quickly in avik's C++ example that nothing can leak. I can also see clearly what the code is meant to do.
In your version, I spend most of my mental energy figuring out what the code is meant to do - it looks like it's mostly error handling code, and the meat of the function is lost in swathes of boilerplate; as I'm normally concentrating on what the code should normally do, not what it does when there's an exceptional condition, I find the sheer quantity of effort I have to put in to read your code wasteful.
Of course, this is not to say that exceptions and C++ are perfect - whoever implements auto_ptr and spinlock_t::guard has to give me a suitable level of exception safety (no-throw or strong guarantees, possibly basic guarantee, to use the jargon). However, I personally find that it's not that hard to meet the needed guarantees for exception safety, and the resulting increase in code clarity is well worth the effort.
