views:

1278

answers:

10

Reading code is one of the best ways to learn new idioms, tricks, and techniques.

Sadly it's very common to find badly written C++ code. Some use C++ as if it was C, others as if it was Java, some just shoot in their feet.

I believe gtkmm is a good example of C++ design, but a binding could not be the best code to read (you need to know the C library behind it).

Boost libraries (at least the one I read) tend to be less readable than I'd like.

Can you mention open source projects (or other projects which source is freely readable) that are good example of readable, modern, well-documented, and auto-contained, C++ code to learn from?

(I believe that one project per answer will be better, and I'd include the motivation that led you to selecting that one.)

EDIT: The question was not intended from a newbie point of view (even if good readings for newbies are welcome, I don't believe that it should make any difference). The reading issue with boost is primarly, as noted in comments, that it's filled with compiler dependent workarounds that make its prose less fluent.

EDIT2: It was not my intention to dismiss all the boost libraries. It's just that trying to read, as an example, the BGL I didn't found the prose I was searching for (even if you can eventually understand them). If you have better examples from boost just point to the specific library.

+1  A: 

QT is very well documented and has a nice and elegant design.

shoosh
I wouldn't cite Qt as great and modern C++. Qt is more in the C-with-classes category.
Didier Trosset
But Qt *is* a great example of intuitive, easy to use API design.
Barry Wark
@dtrosset Qt is far from being C-with classes, you do not have to push template meta-programming to limits in order to call it modern C++.
Artyom
Qt design is outdated and looks more like Java than C++. Besides, it is still not exception safe: http://doc.trolltech.com/4.6-snapshot/exceptionsafety.html which is unforgivable in year 2010.
Nemanja Trifunovic
+11  A: 

I see that you've already decided that Boost is "less readable than [you'd] like", but Boost is quite possibly the best example of exactly the kind of source code you're looking for. The unfortunate fact is that in spite of all of its positive characteristics, C++ itself is less readable than you'd like.

The more time you spend reading Boost code, the more C++ you'll understand and the more readable it all becomes.

At the op's suggestion, here are some libraries I think are pretty good:

  • smart_ptr - small, pretty self-contained, and idiomatic. Might be a good starting place.
  • pool - more complicated than smart_ptr, but still fairly portable and idiomatic.
  • filesystem - also sort of "medium" complexity, not terribly portable, but very useful and intuitive
  • iostreams - large, complicated, many patterns, lots of platform-specific things
  • asio - also large, complicated, and not portable.

There are many more to choose from.

One of the things that makes Boost good, and still readable, is that most of its libraries consistently factor out the platform/compiler-specific tweaks and leave the primary functionality as portable as possible. It's a really, really great thing for C++ devs to learn.

Note: when I say "portable" or "not portable" above, I'm not talking about the library as a whole, but rather the underlying implementation that a reader would be looking at. In other words, "not portable" means lots of implementation details that are particular to a platform. Boost libraries are amazingly portable in practice; I don't want to be misunderstood.

Ben Collins
Reading boost source quickly becomes a lesson in compiler and platform oddities, rather than good C++ idioms.
cpalmer
@cpalmer: actually, it's a good exercise in learning that idioms are not always implemented in their purely abstract forms, and that good design has to take into account platform differences. Call them oddities if you want, but the implementations of the various patterns and idioms in Boost are about the best there are in open-source C++.
Ben Collins
They are good yes, it's quite painful to read them though. I think the problem of compiler support is more an expert topic...
Matthieu M.
Perhaps you could improve your answer giving pointers to one or two boost libraries that are well-suited for reading.
baol
You still "see" and assume to much, but overall I like your answer, thank you.
baol
+10  A: 

You could have a look at:

Some projects NOT to learn from:

  • QT ("C-with-classes", not modern C++)
  • wxWidgets (Quite old, has its own string class).

Also I wouldn't recommend reading the Boost code if you are a C++ newbie, you will get buried in little details.

compie
I wouldn't cite Qt as great and modern C++. Qt is more in the C-with-classes category.
Didier Trosset
I wouldn't cite wxWidgets either. No naming convention and contributors doesn't seem to be bound to some kind of coding guidelines. Imho, the lack of rigor makes it a bad project to take lessons from.
ereOn
@ereOn: It also uses a poor string class in a desire to avoid `std::string` -- a mark of a codebase developed before `std::string` was considered reliable by many developers. +1
Billy ONeal
Did not know about Ogre3D, the website seems clean, I've yet to dig in the code :)
Matthieu M.
+1 for both dtrosset and ereOn. Qt is a typical 1990's Smalltalk-like design and wxWidgets is pretty much portable MFC.
Nemanja Trifunovic
I think you are not supposed to overturn the content of your answer :) but I appreciate you can change your mind.
baol
@dtrosset I like Qt, but it does require its own preprocessor, so it can't really be called a good example of C++ code.
rjh
@baol: I disagree. Nothing should stand in the way of improving an answer.
aib
+12  A: 

In my humble opinion I think dlib has some of the most readable code around. Modern style, RAII, no pointers, religious use of contract programming, etc.

But I maybe biased as I run this particular open source project :)

Davis King
The first paragraph of your homepage fail in its most basic task of informing me about what exactly this library does.
shoosh
Worked fine for me, the meat of it is a bit further down though.
Jamie Keeling
@shoosh: I think it's alright. It is a very general purpose library so it's hard to say *exactly* what it is about in a single sentence. Although, after looking at it a bit I agree that it can be improved. I just updated it so now it's at least a little better :)
Davis King
+3  A: 

Another well-written C++ project is the Monotone version control system.

jemfinch
But I could not read it online yet: http://viewmtn.angrygoats.net/branch/changes/net.venge.monotone
baol
Try http://mtn-view.1erlei.de/branch/changes/net.venge.monotone .
jemfinch
+6  A: 

I've played around some with Google Test (Google's C++ unit testing framework) and have been impressed with its clean design and clarity of code. It's also interesting to me how Google Test's adherence to the Google C++ Style Guide enhances its readability (even though I don't always agree with the Google C++ Style Guide).

I would expect other notable C++ projects from Google, such as Chromium (the codebase for Google Chrome) and Protocol Buffers, to be similarly high quality.

Josh Kelley
+1 Chromium's code is surprisingly clean and easy to follow for something as complex as a web browser.
ZoogieZork
-1 Burn these C++ google guide: In 4 words: no exceptions, no rtti http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Exceptions why just not to say no C++
Artyom
@Artyom: I did say that I don't always agree with their guide. :-) Actually, their arguments against RTTI are correct (using it directly really is a code smell), and the guide is pretty clear that they avoid exceptions only because of the difficulty in retrofitting exceptions into existing non-exception-aware code.
Josh Kelley
+1  A: 

http://www.virtualbox.org - mostly because it's a complete,complex and large application(s) - and not "just" a library.

I'd learned a lot from the source code of http://code.google.com/p/protobuf/ as well.

nos
+4  A: 

I'm surprised nobody mentioned Loki

baol
+2  A: 

I would say: if the code is beautiful, easy to read, modern, portable and uses latest goodies of C++, then probably you are reading textbook...

Unfortunately most of modern code is not so easy to read and far from being beautiful... Because real life is quite hard.

Examples of greatest modern software libraries that extremely useful and good designed Boost and C++.

  • Boost - includes lots of ifdefs for each broken compiler and to work well on many broken OS platforms.
  • Boost - some libraries push template meta-programming to limit in order to get as much flexibility as possible but readability suffers!
  • Qt - very well designed modern, very useful library (even some people think that if library does not push to limit all templates stuff it is not true C++) but... it requires to run on even very old and odd compilers... So you will find many oddities like not using dynamic_cast or other stuff. But it is great, well debugged high quality software.

Bottom line, programming is hard. Take a look on this code I had written. It is code of cache system with invalidation, triggers, LRU and timeout support, that can be shared between forked processes and normal threads... But it is damn hard to read and understand. But is very valuable piece of software to me that required lots of writing.

(And it is uses STL, smart pointer, allocators, RAII and many other goodies)

P.S.: I do not recommend to learn from my code ;-)

Artyom
On the contrary, many textbooks have abominably bad code.
Novelocrat
Your code is not bad, I liked reading it. My belief is that good code should also be well documented. In my opinion the hardest and most important programming skill is learning to write good documentation and choose the right names for variables and classes.
baol
A: 

I think ACE is a good example of a mature open-source multi-platform C++ library with a lot of available documentation. A lot of the documentation is in the form of academic papers, so you learn a lot about the underlying algorithms, design patterns, and tradeoffs.

However, note that it originated some time ago, and suffers from some ugliness in order to be compatible with all the C++ compilers it has ever supported. So it may not fit your desire for "modern" C++ code.

Kristopher Johnson