tags:

views:

805

answers:

13

To learning a language it is recommended to read some good code written in it. I want to recommend to friend some good C code to read. What projects are suitable online for reading good code?

Criteria:

  • Written in good style
  • Well documented (both externally and internally if possible)
  • Real-world, not tutorial-level or introductory book-level

I found SQLite code to be pretty nice. What other recommendations you have?

P.S: Preference given to code you've actually read and found good.

+4  A: 

What level are you talking about? I learnt a lot about how to structure a large C library from working with (which meant reading) OpenSSL. It wasn't exactly an enjoyable process, but it was certainly an education (it's very much "OO in C" if you get what I mean - function pointers to pluggable interfaces, etc).

andrew cooke
+6  A: 

What about Lua source code?

Nick D
what about it? you read it and it's good? what's good about it?
zaharpopov
it's very well engineered, as SQLite is. You can get some insights on how virtual machines and garbage collectors can be implemented.
Nick D
the lua vm is supposed to be a very nice implementation that's efficient and portable. there's a nice paper somewhere about it that should be easy enough to google.
andrew cooke
+3  A: 

You should probably check into the linux kernel.

Jonas
Take the code of the core subsystem of the kernel especially. (some drivers are not so nice sometime).There are also nice 'utils' like linked list / oo-programing in the kernel which is pretty nice.
246tNt
Did you read it and think it's good? I'm little wary of Linux kernel because I think it can't use the same primitives (malloc) like normal code, which makes it somewhat different. Is this right?
zaharpopov
Minix is far shorter and simpler.
jleedev
@zaharpopov: some primitives are different but still they're quite the same. But if the goal is to learn the libc, this is maybe not the right place to start. but in terms of design, you've got a lot of different programming paradigms, so that's pretty interesting.
LB
I'm certainly no expert and I've only skimmed through parts but everyone seem to recommend it so I kinda chimed on. Also a guick search on google on 'linux kernel source' gives a few links on tours to the kernel for a quick start and an overview. Although not personally familiar with minix jleedev does make a great point and it might be worth looking into. I might do it myself a bit later.
Jonas
+1  A: 

Getting to code for gcc or any other open-source C compiler would make for good/challenging/enlightening reading

David Oneill
last time I looked at the code of gcc I was a bit horrified. strange style - strange conventions.
zaharpopov
+2  A: 

The source code to git would be interesting. Not only is it well reviewed, you would be looking at the source for many small components, so the scope of it all would not be so overwhelming.

You might also want to look at the GNU project coding standards, just to see how they are a little different than most. For that, I recommend looking at the source of Bash.

Tim Post
A: 

Starting a personal project is the best way to learn a language. Coding style, from mine experience with co-workers is a very subjective issue, ie why companies have strict code-styling documents, which any developer has to adhere to when working for that company. C, is a lovely language to learn coding, i think it the best as, it is not lengthy. It is beautiful, you can do, i think everything, if you wish. The learning curve is not steep, yes but implementation curve is very steep. And I think that the way to learn programming. Get the basics straight, and try to implement it. Put some time and effort into thinking, designing and then coding. And, when you are stuck, come here to SO or go to bing/google/colleagues.

If your motive is learning, try to implement something is already implemented. Learn the design pattern/styles, coding patters/styles. And evolve as you spend more and more time.

To answer your question, give him some nice challenges. If he knows the basic of C, ask him to implement (depending on his interest, say if networking is his interest) say a concurrent TCP server. Its not too difficult if you know how to make a TCP server, and you can think of the fork() trick. Or, if his level is higher, why not a threaded work-queue model. It will teach him locking/thread-pooling. And he can have a look into plethora of code on net, discuss with you/friends the solutions in terms of design and style.

It will be more fruitful to have a motive in head, like a task, and then look for a solution, rather than just following some code, trying to figure its requirements, which might in the end does not interest him domain wise. Like if he likes image processing, TCP stack's implementation wont interest him.

That is my recommendation.

Vivek Sharma
10x, but this has no relation with the question. it's exactly why i ask for source that people personally recommend as good - code that's peer reviewed and written by great programmers.
zaharpopov
hmm, i just gave an advice on the basis, for someone learning C, giving him Kernel-Source code is like molestation. Let him grow as a developer, so that later he can value good code, if he does not code, and know what is good and bad, do you think he will appreciate the good code, when someone says it is good - take it for granted :). Programming is lovely, but when it happens :D. Let him grow.
Vivek Sharma
While you do tell people who want to be writers to do a lot of their own writing, you also tell them to do a lot of reading.
Kelly French
Not to mention that if reading source code were more common among programmers, they would be also incentivated to *write* more readable code. (At least something that one shouldn't be ashamed to show).
Remo.D
+3  A: 

I learned a lot about coding from studying NetHack. Its been around for over 25 years and has had lots of people work on it and keep it cleaned up. Wikipedia has plenty about it. You can find the source code at NetHack's site. It's a simple text UI but still has so much richness and has been ported to so many different platforms. It has real-world scenarios for conditional compilation, defining magic numbers properly, platform independence, and all while keeping the code organized and readable. While it didn't hurt that it appealed to my gamer side, I'd still recommend starting with NetHack before digging into a operating system kernel. The Linux kernel would be a good place for an advanced study, mainly because you'll run across too many op/sys details that might distract from learning the coding style.

Kelly French
+2  A: 

Have a listen to Episode 148 of Software Engineering Radio: Software Archaeology with Dave Thomas.

Here's the blurb:

Dave explains why reading source code is at least as important a skill as writing source code. He shares approaches for how to get to grips with unknown and undocumented source code even if it is non-trivial in size. He finishes with advice for how to get started reading code.

bendin
hmm I know it's important to read code. thanks! this is why I asked. but does he recommend any specific code to read? if so, please spare the MP3 listening and say what he recommends and why
zaharpopov
Dave Thomas suggested the core of the Ruby codebase as a good example of a well structured and consistent C codebase of approximately 15000 lines. Whilst I haven't read it myself, presumably Dave has! In the podcast, he also offers some sensible tips for approaching the process of reading and understanding a large codebase.
Dave Cluderay
+4  A: 

My recommendations (not necessarily in this order):

sed: learn to use sed, then study this. The code isn't too complicated, doesn't require a lot of domain-specific knowledge, and is a good intro to how real code is written.

linux kernel, as has been suggested. Focus on the overall architecture. Then, if you know the inner workings of some device or protocol, study that. The SLIP module would be a good start, since SLIP is trivial. Learn how the linker works.

git is another good suggestion that has been made.

glibc: You'll learn quite a few tricks, and have the benefit of learning the core C library as you learn the C language.

busybox: same reason, plus the source to a lot of standard UNIX and linux utilities.

libsparse: a C parser. You get the benefit of seeing how a C parser works (and thus, deep understanding of the syntax) without all of gcc's abstractions. You'll be motivated to study phi-functions and SSA, and learn a little of how modern compilers work.

Caveat: not all of these are necessarily heavily commented, but a lot of very good working code isn't. Especially code like the linux kernel, which changes often. Most of them have well-written, extensive documentation, though.

There are many more, of course, but if you can come to understand these moderately well you'll know what else to look for.

Tim Schaeffer
+2  A: 

In my honest opinion, any code in the /usr/include directory on any UNIX based/like systems [1] are the best pieces of code to read, for a learning programmer to C; that way, you can look at how system routines that you use for your own programs work.

The Linux Kernel [2] is an ugly blasphemous implementation of a kernel, a cleaner kernel (by miles) is any of the variants of the BSD [3] subsystem, reading the Linux Kernel sources so early, might lead you to infer that some of the practices made are right, whereas they aren't.

Other projects that you may like to look through, are compilers like GCC [4] (highly suggest that you run through older releases), or GNU's plethora of programs ( wget [5], gdb [6], etcetera [7] ).

You might consider __writing__ [8] programs too ;)

  1: http://en.wikipedia.org/wiki/Unix
  2: http://kernel.org/pub/linux/
  3: http://en.wikipedia.org/wiki/BSD
  4: http://ftp.gnu.org/gnu/gcc/
  5: http://ftp.gnu.org/gnu/wget/
  6: http://ftp.gnu.org/gnu/gdb
  7: http://www.gnu.org
  8: http://stackoverflow.com/search?q=Code+Golf
nihilis
I would personally ask you to look at wget. It is a useful utility backed by friendly and talented bunch of programmers.
ardsrk
+1  A: 

There aren't many programs that match OpenSSH as examples of good user-land C code. The code is clear, varied, and well commented and it has a seal of warranty from all the hackers in the world.

You can browse the OpenBSD version here and download the portable version from here.

jbcreix
A: 

SVN is a great example of well written and maintained C.

jeffamaphone
A: 

I have learned a lot of new techniques and C tricks reading Quake 3D source code from ID Software. Since many years it is not deprecated. Especially if you are planning to work on computer games, it can be very useful. You will learn a lot of useful stuff: e.g. how really big commercial software projects are organized and how to write multi-platform applications.

psihodelia