r/programming • u/chrisdew • Sep 26 '09
Ask Proggit: What are the most elegantly coded C/C++ open source projects?
I've recently been reading (parts of) the source for sqlite3 and found it to be a revelation in good programming practise.
What other C/C++ open source projects (of any size) would you recommend that I look at, in order to get an idea of current good practise?
144
Upvotes
34
u/MyrddinE Sep 26 '09
I hear that the Quake codebase released by id is some beautiful code. I am not a C programmer though, so that's hearsay.
31
u/[deleted] Sep 26 '09
OpenSSH. To see something that is clear art read the whole source code and get enlightened.
19
u/[deleted] Sep 26 '09
the Haiku kernel
13
u/panic Sep 26 '09
WebKit isn't bad for a framework of its size and complexity.
12
u/[deleted] Sep 26 '09
The qmail source I've read was very clear and straightforward.
12
u/krelian Sep 26 '09 edited Sep 26 '09
I believe llvm is considered quite good.
10
u/KayEss Sep 26 '09
I always thought that Jeroen's libpqxx was very nice. libpqxx is the official C++ interface for Postgres.
9
u/srekelwork Sep 26 '09
Ogre (the graphics engine) is very decently designed and written C++ library. Only ugly thing I know of in it is the use of Singletons here and there.
30
u/[deleted] Sep 26 '09 edited Sep 26 '09
Read the source for the Lua interpreter.
16
u/[deleted] Sep 26 '09
ZFS
41
u/[deleted] Sep 26 '09 edited Sep 26 '09
Do read:
Apache portable library
Minix 3
But whatever you do, don't read boost. Unless you are a sad masochist.
I tried reading boost.lambda code, and almost ripped out my eyes.
7
u/[deleted] Sep 26 '09
Qt by Trolltech/Nokia
7
u/ElectricRebel Sep 26 '09
LLVM. They don't use the full power of C++ though, just what they need to make the compiler have a sane internal design (unlike GCC) but still run with reasonably high performance.
44
u/xach Sep 26 '09
procmail.c. Even the header comment says "Seems to be perfect."
6
u/gogglygogol Sep 26 '09
Regarding C: nginx looks impressive. You can learn a lot about optimization from reading the code.
6
u/malefic_puppy Sep 26 '09
Qt.
(L)
11
u/zerothehero Sep 26 '09
V8 for C++ http://code.google.com/p/v8/wiki/Source?tm=4
33
u/[deleted] Sep 26 '09
Qt is really beautiful. Especially comparing to other GUI libraries. And it has good documentation too.
5
u/jroller Sep 26 '09
You might want to compare against PostgreSQL. There's a lot more for features, and I've found it very clear as to how they work.
5
u/Arelius Sep 26 '09
LLVM is really well designed, the API, along with the code are both top notch!
5
u/[deleted] Sep 26 '09
The best C++ framework I've ever read is, IMHO, LLVM.
5
u/[deleted] Sep 26 '09
For C anything out of Bell Labs is a good bet. It takes a while to get used to the 1127 coding style, but after you do, you'll never think about C the same way again. A lot of it, including the original Unix, Plan 9 and Inferno are on google code search. Start here: http://www.google.com/codesearch/p?hl=en&sa=N&cd=1&ct=rc#miRTe8ZyR0o/Archive/PDP-11/Trees/V6/usr/sys/ken/slp.c&q=file:/usr/sys/ken/slp.c%20%22You%20are%20not%20expected%20to%20understand%20this.%22&l=325
8
u/AlLnAtuRalX Sep 26 '09
The original Doom.
4
u/uep Sep 26 '09
I only briefly looked through them a couple years ago, but I remember thinking the clang and llvm sources looked pretty good.
4
u/[deleted] Sep 26 '09 edited Sep 26 '09
Apache. Linux kernel. memcached. gdb.
4
u/grosskur Sep 27 '09
qmail and other DJB software.
7
u/gekidoslair Sep 26 '09
look at any of john carmack's game engine code - it's like reading a good book, clean, concise and beautiful ;}
61
u/qlqropi Sep 26 '09
for fuck's sake people
C and C++ are two different languages
what are the most elegantly coded Fortran/Ruby open source projects?
6
u/[deleted] Sep 26 '09
libev
3
u/Imagist Sep 26 '09
Apparently the Ruby source code is well-written enough that all the English Ruby documentation was written just by looking at the source.
3
u/[deleted] Sep 26 '09
http://www.freeswitch.org
3
u/StinkiePhish Sep 26 '09 edited Sep 26 '09
poker-eval. "One glance at the libpoker-eval source files and I instantly unlearned 62 months worth of best-practices programming techniques." Actually, stay away from poker-eval if you like your sanity. edit: to give credit where credit is due, the quote is from codingthewheel.com
3
u/dagbrown Sep 26 '09
Postfix. It's beautifully-clear code, you can easily see what everything's doing and how it's doing it, and changing it involves nearly no stress at all.
3
u/madhadron Sep 28 '09
I learned a lot of style from the OpenBSD code back in the day. I recall the user utilities being particularly nice. Given the obsessiveness of that community, I have no reason to think it has gotten anything but nicer since then.
The thing about C is that "current good practice" is different from "K&R's good practice" only in some formatting and layout issues, and some library calls that have been replaced over the years by versions with fewer insecurities. The language is a codification of how Thompson liked to write assembly on a PDP-9. Thompson was a very fine programmer, and so the codification is worth knowing the same way it is worth knowing the writing style of Jane Austen.
I dispute whether there is such a thing as good programming practice in C++, though I will stipulate the existence of more defensive practice in C++.
12
u/geocar Sep 26 '09
This changed the way I think about coding.
At first, I thought it was mere masochism; some kind of unlambda or intercal barf, but I ran into it again later after reading an interview by its author. Apparently he can read that.
Not just decode it; not figure it out with effort, but read in the same sense that you're reading what I'm typing now.
I decided that was something I wanted to be able to do, so I spent some time trying to read it. Several hours for several days I just looked at it, and one day it just clicked.
My coding style changed radically at that point; my development time shortened significantly, and while I don't usually write code that looks like that, I have an increased clarity of thought that I definitely associate with being able to read that.
10
u/iamnot Sep 26 '09
Qt
4
u/rixed Sep 26 '09
The version of the "wc" tool that's written using literate programming.
5
u/[deleted] Sep 26 '09
Take a look at Tokyo Cabinet.
2
u/drbrain Sep 26 '09
dwatch
2
u/gagle Sep 26 '09
The SGADE without a doubt.
2
u/jambarama Sep 26 '09
I always thought xmms2 was among the best coded projects out there. Clean, efficient, and virtually without bugs (so probably not much to work on).
2
u/[deleted] Sep 27 '09
Read "Effective C++" from Scott Meyers.
2
u/chneukirchen Sep 27 '09
Dive into the Plan 9 source tree.
3
u/elihusmails Sep 26 '09
vsftpd - secure and clean C code.
3
u/djork Sep 26 '09
SQLite
2
u/[deleted] Sep 26 '09
Apache server is a great example of well thought out and organized code.
1
u/beagle3 Sep 26 '09
FLTK. It's a light GUI toolkit done right. I think a static link with all the bells and whistles is still less than 200k, it's fast, easy to use and extend, and well coded. You want to look at the latest 1.x branch -- fltk2 was(is?) a rewrite that never quite got there.
1
u/pjdeets2 Sep 27 '09
Other people have mentioned both of these, but I have heard that LLVM and WebKit are well-coded. I can't say I have spent more than a little time looking at their source though.
1
u/[deleted] Sep 27 '09
NetBSD. Written in C. In particular, the architecture for drivers.
1
u/msx Sep 27 '09
sqlite has one of the best quality source out there
1
u/segoe Sep 28 '09
The lua VM
Tim Budd's Leda (as in the book multiparadigm programming in leda)
TCL/TK source code (for version < 7)
Susie (a little smalltalk fork on sourceforge)
0
u/robbansworld Sep 26 '09
rtorrent
0
u/kpierre Sep 27 '09 edited Sep 27 '09
First Prototype J interpreter -- Arthur Whitney is the inventor of this amazing style. Once you familiarize yourself with dictionary, everything is diamond clear. It is amazing how much distracting symbols common programming styles have.
If you would prefer 'big' opensource project, look at A+
-8
u/astrosmash Sep 26 '09
Really? I just has a quick look at the SQLite source code:
Hungarian notation. Bleh. That single variable name right there embodies so many things that I hate. It's just awful.