Moving to Seattle
Startup Investing for Everyone
There's a Choice
Freeware Game

C++ and Automatic Garbage Collection

Programmers know about the mental state commonly referred to as “flow”. In this state, we are most productive, by a significant margin. This state is, of course, not limited to software development, and is well known in most creative pursuits.

For example, I’m currently writing this article in a program called Scrivener, an excellent writing application targeted at professional writers - the kind that write novels, screen plays, etc. It has a special mode, called “full screen composition mode”, which I’m using at the moment. In this mode, there is nothing on the screen except a blank white page. There are no formatting buttons, no word counts, no tasks bars, and certainly no talking paper clips. It is completely optimized for doing one thing, and one thing only - writing. Everything else is hidden away.

This Scrivener feature is there to help writers get into “flow” mode.

To enter this mode, you focus, completely, on the task in hand. And such a complete focus is not easy to establish. An incoming phone call, even if ignored, is more than enough to destroy it.

Focus is about saying no to the things which aren’t important right now.

This is not a new concept, nor was it discovered by Steve Jobs. Learning to focus has been a very important step in Buddhist meditation techniques for thousands of years.

Of course, Scrivener is not the only tool that helps people focus. Another excellent tool more familiar to programmers is vi. This editor is not famous for ease of use, but once the learning curve is overcome, the editor is excellent at getting out of your way and becoming invisible. People report that vi helps them enter this productive zone, and I can attest to this myself.

Some programming languages also help you enter the zone more easily. Remember, getting into flow is completely focusing on a task. This is why for different tasks, some languages are better suited than others, with regard to flow. It is all about what the language chooses to hide from you. For a high level task, Python is probably a better choice than C, because it will be easier to enter a productive mental mode in Python without worrying about remembering to call free().

A great many people claim that the single greatest benefit for programming productivity in recent decades is automatic garbage collection, as seen in managed languages, such as Java and C#. Let’s ignore the fact that McCarthy invented it for LISP way back in 1959. This is an enormous benefit because today programmers focus on writing the code they want, and they don’t have to think about managing memory. The language hides it away.

A great many people, myself included, claim that C++ is a bloated language. They also claim, myself excluded, that C++ is terrible because of the manual memory management. It certainly is a great burden to manage memory manually - it is generally difficult to do correctly and it also makes it harder to enter the precious mental state we all covet, because it’s one more thing to worry about. But it is simply not true for C++.

It is true that in C you have to manually manage memory. It is true that 10 years ago, most people managed memory manually in C++. However, today this is no longer true. Unfortunately, most Java/C# lovers, C++ haters, miss this fact.

More importantly, they miss out that the Java/C# promise is horrendously broken. Consider the following code snippet, taken from the book Release It by Michael T. Nygard, page 33:

This Java code has a bug in it. In this particular case, this bug caused thousands of people to wait for hours and hours for their flight and caused major financial damages to an airliner.

The bug is that java.sql.Statement.close() can throw an exception. If it does, then the close method of the connection is never called, and a resource is leaked.

In Java/C#, code in the “finally” block has to be managed C-style. But C doesn’t have exceptions, and exceptions complicate resource management by an order of magnitude. Exceptions are critical. Exceptions change everything. There are work arounds in Java and C#, but they are just that - work arounds. They aren’t pretty!

"autumn binscape" / Jes

The real problem here is that the garbage collector isn’t like a person walking around with a giant garbage can, cleaning up. A garbage collector, in Java/C# at least, is like a person walking around with a giant plastic recycle bin, cleaning up after scattered bottles. He ignores all those copies of yesterday’s newspapers and the empty tuna cans.

A Garbage Collector should collect all garbage, and garbage is defined as any no longer needed resource. And a resource is not just a piece of memory (with the book-keeping that comes with it). A resource is also a file handle, a DB connection, a no longer needed synchronization lock, etc.

C++ shines here, because C++ has a built in garbage collector that manages all such resources. Yes, you heard that right. C++ has a built in garbage collector. In modern C++, most programmers never, ever, have to write “delete”. The “delete” keyword went the way of the dodo. It went the way of the goto.

The same mechanism used for managing memory can be used for managing any kind of resource.

Focus is about saying no to the things which aren’t important right now

In Java, resource management is rarely not an important thing to think about.