Fog Creek Software
Discussion Board

Well formed C++ code?

Having spent years using Perl and various other scripting languages, I am now learning C++. To further my education, I am looking for code to read. Sadly, Sturgeon's Law applies to source code as well, and there is a lot of really bad code out there.

Can anyone point out C++ code that is well formed, or at least, won't teach me any bad habits? ;-) I am most interested in looking at code that conforms to the ANSI/ISO standard.

Wayne Earl
Monday, November 26, 2001

Hmm... For good examples, that's a tough one. In my rather limited experiance, I've noticed that code can be very well factored, and easy to understand once you see the "big picture", but often it's difficult to figure out what's going on from reading the code itself.

The other problem I usually get when trying to read code is API issues: it's hard to understand code in any language written for an API that you are unfamiliar with. For example, there are many simple X11 window managers written in C and C++, but the API function calls are prevalant all over the place, and I have to look up each and every one, and every class & structure. It's not easy, in any language.

My suggestion is to read tutorials: their code is designed to make it easy to learn from; the above examples (i.e. real world code and applications) are greatly more rewarding to work with, however, are designed merely to WORK, to accomplish a task. The code is not usually designed to be self-explanatory at the first glance. Certainly, after study, and it ought not be obfuscated, but most of the code organization I see requires quite a bit of study before it's understood in any nontrivial application.

Keep in mind, about bad habits: If it's bad in Java, it's bad in C++. If it's bad in VB, perl, python, pascal, lisp, scheme, bash, or tcl, it's bad in C++. Some languages like java try to design in good habits, but that doesn't prevent null pointers. Good code is good code, I think. I find it interesting that most "good code" strategy guides (or whatever you want to call them) focus more on meta-style things, like doing things in logical manners, and less on "++x is bad" specifics. Meaningful variable/function names are valid in ALL languages, for example.

My advice: get a book. Well, for any language, always get a book. I happen to own a copy of the C++ for dummies book, that I keep as a quick reference for an example of simple C++ syntax I may have forgotten. Any "learning" book should have plenty of examples, I think that's the easiest way to pick it up.

And don't discount experiance: I think I've eliminated many bad habits of my own simply from working on things on my own. I don't write overly convoluted code, because I learned the hard way that that is bad. The best way to get into a language and it's style, i think, is to pick a project and write it in that language.

Mike Swieton
Monday, November 26, 2001

As a resource I'd recommend the C++ in action web book at

Ged Byrne
Tuesday, November 27, 2001

You may find usefull also the articles in "C/C++ Users Journal -- Experts Forum" that is partly on-line at

I personally like also Herb Sutter's "Guru of the week" at, the core of his books (you will find the books reference there).

In my opinion, C++ is so much flexible, that it is difficult to find _typical_, well written code.  I mean, the "well written code" may be very different in some cases. 

I would recommend to read the following books...

Booch: Object Oriented Design with Applications
[Rather old (1991) but very well written, in my opinion. You will understand why OO Programming appeared, how it is related to OO Design.  Btw, the "Five attributes of the complex systems" (pp. 10-11) should be carved to stone, in my opinion.]

Gamma, Helm, Johnson, Vlissides: Design Patterns: Elements of Reusable Object-Oriented Software
[Combination of theory and praxis in the form of reusable design building blocks.]

Stroustrup: The C++ Programming Language
[The bible (i.e. possibly not readable from the first to the last page)]

Petr Prikryl
Tuesday, November 27, 2001

Another book to take a look at is Thinking In C++ (2nd Ed) by Bruce Eckel.

It's available for download from the author's web site as well as in printed format.


Michael Josephson
Tuesday, November 27, 2001

What you want is called a "coding standard".  Every programming orgainization should have one.

There are lot of different c++ coding standards around, just do a google search for "c++ coding standards". 

Here is a good place to start:

Jim Howard
Thursday, November 29, 2001

Word of caution its
int main (void)

not void main (void)...

Jon H.
Saturday, December 1, 2001

If good Win32 source code is what you're looking for, I highly recommend Sam Blackburn's WFC class library (do a google search on "sam blackburn wfc" and you'll find it).

Great stuff and wraps a lot of Win32 NT functionality that MSFT in their infinite wisdom never bothered with... MFC seems to wrap UI and not engine stuff.

Tuesday, December 4, 2001

If you have any comments about I'd like to hear them.

It's my first attempt at a Web page, perhaps my third attempt at wrapping the Windows API.

Christopher Wells
Thursday, December 13, 2001

Some more source of inspiration:

There are umpteen million books on C++. Most of them are bad, and you can spend up to $1000 just on the good books. The difficulty lies in fishing out the good ones from the pile of bad ones. The Association of C and C++ Users publishes book reviews on line a , so you might want to start there.

Regarding Sutter's Guru of the Week series: it's on hiatus as
he's working on too many other columns at once. You can still
follow his "conversations" at .

I keep a list of C++ books I've personally found to be helpful on my website at -- most of these I've read in full at least once. Some of them (eg modern C++ design ) have companion source code, which will be another source of good habits.

If you are just beginning with C++, I recommend Lippman, _The C++ Primer, 2nd edition_. That edition is out of print, but it is much more readable (400 pages) than the 3rd edition (1200 pages). In its place, I hear that Koenig and Moo, _Accelerated C++_ ( ) is the best "beginner"'s C++ book to come out in recent years, but I haven't sat down and read it yet.

If you're looking for a high-quality Free Software project in C++, your best bet is probably KDE ( ). KDE and QT ( ) tend to stick to pretty modern compilers, and their programmers are pretty disciplined.

Remember that MSVC isn't particularly standards compliant (at least version 6 isn't), so you'll have to use either gcc or the intel compiler if you're on windows, or use another platform.

Where's keith ...

Nick Beaudrot
Monday, December 17, 2001

good i to started now but i like to be goood programmer in c++

Wednesday, August 18, 2004

Read "Code Reading  - An Open Source Perspective" by Diomidis Spinellis

I  was once handed a monsterous project of porting
a multimedia player on to an embedded platform. The source was written in C++ {in an age where Templates were not in fashion and pre processor macros did the job of templates which just consumed me.}. The project was a fiasco.

I was hard on myself and to this day feel that I was being very short sighted about code reading.  I could not have managed to understand all 9000 files and still successfully compile the code to run on the board. I couldnt just focus on the job.

I consider myself a better C than C++ programmer simply because
I can read C better than C++
I can debug C better than C++
I am always overwhelmed with new design patterns and jargon.

For a person with "marginally above average" intelligence {I'm more of a software coding enthusiast than a gifted genius}, I think reading a lot of C++ code, lots of C++ coding/debugging and astute OOD concepts are critical.

Anyone have anything else to comment?

vijay rajan
Thursday, August 26, 2004

*  Recent Topics

*  Fog Creek Home