Safe C++

Safe C++: How to Avoid Common Mistakes by Vladimir Kushnir
$19.77 at Amazon ($12.39 Kindle)
Published by O’Reilly Media

C++ used to be my favorite language; although I haven’t used it in years, I’m starting to get into several related languages (C# and Objective C) and I was curious about this new book from O’Reilly. In my day job, I spend a lot of time testing code and tracking down bugs, so the promise of strategies to avoid bugs in your code and identify errors is intriguing, even if not all of the errors apply to the languages I’m currently using.

The premise of the book, covered in the first section, is simple: we want to code in such a way that the compiler will catch as many bugs as possible; it’s always easier to fix something when you can see exactly what code won’t compile than when you’re testing the program later and trying to figure out why something isn’t working correctly. If the bugs don’t keep the program for compiling, our second choice would be that when we hit one in debug mode, we get the information needed to immediately locate the error (and hopefully a decent indication of what happened).

Section two of the book wasn’t exactly what I was expecting. When I decided to read the book, I was expecting coding style tips to make bugs less likely (and we do get some of these, although I found the author’s naming conventions – such as putting an underscore after a private variable rather than before it, and not sticking with standard Pascal or camel case – to be a bit annoying), but the main thrust is using different code (provided with the book) in place of built-in code structures to avoid and/or catch errors. Chapter 4, for example, covers Index Out of Bounds errors, and avoids them using smart pointers (which function similarly to regular pointers, but have built-in error checking). The book makes heavy use of special assert statements, which are set up to make sure you know exactly where in the code an error occurs – something you don’t always get from the compiler!

The specific errors covered in the book include the above mentioned index out of bounds error, errors in pointer arithmetic, invalid pointers, references, and iterators, avoiding uninitialized variables, avoiding memory leaks, dereferencing null pointers, errors in copy constructions and assignment, problems with throwing exceptions from constructors, problems with comparison operators, and errors caused by C library functions.

Reading this on my Kindle, I was thrown when the book unexpectedly ended about 70% of the way in; the remainder is all the code that’s referenced throughout the book. It’s available both as a download and in the book for easy reference, which is nice. I haven’t actually tried running any of it, but it all looks straightforward enough.

Overall, I thought the book was interesting and raised some good points, although I don’t actually plan on using any of the provided code (again, partially because I’m no longer working in C++). It wasn’t exactly what I was expecting and I can’t say that having read it will make me a better programmer, but it did raise some points that I hadn’t considered before and was a reasonably quick read. For that, I give it four out of five stars; just realize that it’s primarily about using the author’s C++ library rather than generic things you can do to improve the safety of your code.

Disclosure: I received a free review copy of this book.



This entry was posted by William on Saturday, August 11th, 2012 at 10:29 pm and is filed under Technical . You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Reply