Apr. 29th, 2010

haggholm: (Default)

I haven’t really worked in C++ for a number of years (and never professionally), but when I felt an urge to read some quality programming/software engineering books and went to the local Chapters, I came home not only with Code Complete (2nd ed.), but also Scott Meyers’s Effective C++ (3rd ed.).

There are several reasons why I’m very much enjoying this book: The fact that it’s accessible and well-written, the feeling of going back to my programming roots, and so forth. But I also enjoy reading it because it has a lot to say about resource management—things that I’ve only heard discussed in the context of C++, but certainly relevant outside of that context.¹

Well, of course good C++ books talk a lot about resource managent. After all, C++ offers all the wonderful ways of leaking memory that C has on the menu, with the addition of gotchas like delete vs. delete[] and the addition of an exception system to bypass your careful resource deallocation. If you can’t write C++ code that does a very good job of resource management—and this takes both knowledge and discipline—you can’t write good C++ code.

But, you may be thinking, this is irrelevant and uninteresting to me: I use modern languages with garbage collection, so all this complicated stuff about exception-safe resource deallocation is only another reason not to learn or use C++ at all. An understandable thought, if so, but one I disagree with. What people can easily forget is that memory management is only one form of resource allocation, and a program can leak other resources in much the same way as it can leak memory: Database and socket connections or file handles left open, GUI resources left unreleased to the OS, and so on. Some of these are in fact a lot worse than mere memory leaks, since a file your program leaves locked may stay locked even after your program terminates (freeing up even leaked memory).

In other words: Because of the omnipresent danger of memory leaks, C++ forces you to be aware of proper resource management—but these lessons are relevant to all manner of other resources. Learning not to leak memory in the face of virtual and non-virtual base class destructors, or in the face of multiple return paths and unexpected function termination via exceptions, may be tedious and seem like busywork, but will instil a solid grasp of all the myriad gotchas of general resource management.

Of course, if you’re working in a web environment you may be relying on the fact that $YOUR_DYNAMIC_LANGUAGE_INTERPRETER will free up all its resources upon termination, i.e. upon completion of each request handled. However, this is not only potentially wrong (locked files, &c.), but should make you feel a little bit dirty and ashamed anyway: That’s no way to program! It’s not hygeinic. And heaven help you if you acquire those bad habits and are ever forced to write a long-running program.


¹ Everything I said in this preceding paragraph applies equally well to Herb Sutter’s Exceptional C++ and More Exceptional C++, only Meyers’s book seems to deal with somewhat less mindboggling material. I should explain this remark by saying that when I refer to all three of these books as accessible, I mean that the material is approached in the most accessible fashion I can imagine. The material itself, especially in Sutter’s books, is occasionally rather mindboggling, and the octopus-from-a-dog joke comes to mind.

Profile

haggholm: (Default)
Petter Häggholm

July 2025

S M T W T F S
  12 345
6789101112
13141516171819
20212223242526
2728293031  

Most Popular Tags