views:

4070

answers:

28

Following the advice of Scott Hanselman, I'm looking for good source code to read. Here's what is currently on my list:

  1. Scott's posts
  2. CodeProject

How about you? What source code do you read? It doesn't have to be .NET related.


Update

Compiled replies:

It turns out reading bad code will also help you a lot.

+6  A: 

O'Reilly have a great book on this subject, Beautiful Code. While there will be some of it that doesn't interest you, there's plenty of good stuff in there. Tim Bray's section and the chunk on the SVN Delta Editor are particularly worth a look. Hope that helps.

GaryF
A: 

Codeplex

There are many greatful open source projects

darkdog
+10  A: 

I stumbled on SQLite once and think it's very nicely written. Clean and "disciplined" just like it should be. I might not agree with indentations and stuff like that, but the focus I think is in how clean things can be expressed.

epatel
+11  A: 

Donald Knuth's literate programs are great reading. See http://www-cs-faculty.stanford.edu/~uno/programs.html.

Kristopher Johnson
Since it can be hard to figure out how to use them: you need to run `cweave filename.w` to generate filename.tex, on which you can run (say) `pdftex filename.tex` to get filename.pdf. For the actual C code, you need to run `ctangle filename.w` to get filename.c, although it's not meant to be read directly.
ShreevatsaR
+6  A: 

I think Scott Hanselman mentioned this in one of his Weekly Source Code posts, but the Paint.NET source code has all sorts of interesting things. The source code is no longer provided, but an older version of it can be found here (6.9 MB).

Of particular interest are tabbed interfaces, MDI interfaces and of course, manipulating images.

lomaxx
+2  A: 

There's a pretty decent book that goes through the source code to SharpDevelop, the free Visual Studio-like IDE for .NET. The book's called "Dissecting a C# Application: Inside SharpDevelop". You can get a copy used for like $4.

It can be a really good start for getting into reading other people's code because you've got a guide who's there with you looking at it.

The other thing I'd recommend is installing Reflector and pointing it at every .NET DLL and EXE on your system for a peek. It can be really revealing of how an application or library works.

J Wynia
+4  A: 

It may be a bit daunting (and all in C no less), but the entire Linux codebase is easily browseable at the Linux Cross Reference.

mreggen
+21  A: 

While reading great code has obvious benefits, you may actually learn more by reading mediocre code. Honing your skills of analyzing and understanding horrible code will better prepare you for the real world of professional software development.

Grab some code from anywhere and read it. Keep reading it until you understand it well enough to make modifications or add features. What made it hard to understand? Does its structure make sense? How could it be made better? What bugs did you find? What questions would you ask of its authors? Apply these lessons to the code you write.

Kristopher Johnson
Very true regarding bad code. Especially in Open Source projects, where bad code will raise the entry barrier for potential contributors. (I once wanted to improve on a game with source available: every piece of logic was in a single, uncommented, 2000 line function. ._.;)
Bernard
+2  A: 

Actually, I think you can learn more by reading the worst code that you can find, assuming you know the language it is written in and only want to learn about style. Personally, I think the best way to learn how to do something right is to learn how to do it wrong...and then not do that.

Thomas Owens
+3  A: 

I'd second CodePlex and throw in other Open Source hosting locations like Google Code and SourceForge. What I usually do is drill down to a topic I'm interested in e.g. Wiki or Blog and then download a project to surf through.

One pitfall for me in the past was trying to digest well known projects; since they were so large it was often a bit overwhelming to really get into things. I'd recommend finding smaller projects first and working your way up to something as large as a Paint.NET.

David in Dakota
+4  A: 

I would suggest the Java JDK source (latest is available from SUN ) If you're developing Java code, it's really useful to make your IDE aware of this source so you can navigate into it and even step through debug it. But even if you're not, it's quite interesting and informative to look at the actual implementations of library code.

hromanko
Totally agree -- while quality is uneven (there are bad parts too), overall it is MUCH higher quality than many other suggestions. Specifically, I have found Spring source code pretty hideous in places.
StaxMan
+3  A: 

PostSharp does some pretty amazing things with .NET Reflection and is well worth checking out.

brendan
A: 

Here's some interesting links, most of them related to C# and ASP.NET:

iulianchira
+1  A: 

If you want to find out what NOT to do in your code: The Daily WTF

Jon Limjap
+4  A: 

I recently discovered that the source for Residual is a darn good read. It's a project to make Grim Fandango playable on modern systems, and the developers have a lot of good practices going on.

Josh Matthews
normally C++ using templates makes my eyes bleed. and i work with physicists who are horrible coders. this source is understandable, fairly neat, makes me hate C++ less!
DarenW
Residual's parent project, ScummVM (http://www.scummvm.org) follows the same conventions, and I can also assure you it's an interesting, well written read.
Vicent Marti
+5  A: 

Some of the smallest and most elegant C code I have read has been from Plan 9.

ccxvii
+2  A: 

I found that the source code of the Spring Framework (Java) is the most beautiful and elegant I have seen. I would recommend you take a look at it even if you are not using Spring (although I can think of no reason not to ;) )

Shimi Bandiel
+1  A: 

I recommend virtual tree for its flexible and high performance design. I learned a lot from the code. It is written in Delphi.

+1  A: 

Plan 9, and Tcl

Mark Bessey
+2  A: 

I'm actually reading some parts of wget's source code

To be specific I'm interested in the parser wich is holded in html_parse.c, It uses the Boyer-Moore algorithm and memory pools. One of the interesting things I found is that the memory pools are stored at compile time at first time, and only stored in heap if neccesary while in runtime.

Lagenar
+3  A: 

"Programming Pearls" by Jon Bentley and "Elements of Programming Style" by Kernighan and Plauger.

I second the mention of the Tcl source, and would like to add the BSD 4.4 source.

mpez0
+4  A: 

I absolutely love Minix code. I worked on it for about two years. Its just a 4000 line kernel with 15 years of proper design to back it up.

Sridhar Iyer
+2  A: 

Diomidis Spinellis: Code Reading: The Open Source Perspective (Addison Wesley, 2003. ISBN 0-201-79940-5) provides guidance in reading samples of code. The web site http://www.spinellis.gr/codereading/ gives an overview of the book's contents and links to related material.

Older (1976) but still valuable material is in Lion's Commentary on UNIX 6th edition, more information at http://en.wikipedia.org/wiki/Lions%27_Commentary_on_UNIX_6th_Edition,_with_Source_Code

mas
A: 

GitHub is for Public Open Source and Private Business Code

joe
+3  A: 

From Trevor Blackwell's web site:

Spend time reading other people's well-written programs. Sadly, a lot of Linux and Gnome open source is poorly written. Good code bases that I know of include:

  • The kernel and utilities for FreeBSD.

  • Bittorrent, an elegant Python program with both serious algorithms and good UI.

  • Squeak, which is written in itself. It includes the complete source to its powerful GUI system, and is the only GUI toolkit I know of that is clean, readable, and hackable.
  • Python, both the interpreter (in C) and libraries (in Python.)
  • LCC, the lightweight C compiler along with the excellent book documenting it: A Retargetable C Compiler: Design and Implementation by David R. Hanson. It's worth reading even if you have no interest in writing compilers.

And take a look at MINIX, darcs, xmonad and this article.

barism
A: 

I originally learned to program reading the source of qmail. It's idiosyncratic, but probably the best single exposition and execution of a program for Unix. It is a well-designed program(s), and it's proven it's practical worth.

Even since my conversion to functional programming, I still think qmail is work of art.

Lang Martin
A: 

The best source code I ever read was the Fortran code of Adventure (Colossal Cave) - if only for the comments ;-)

http://www.ifarchive.org/if-archive/games/source/advent-original.tar.gz

Mawg
+1  A: 

If you want to learn how to write beautiful code, I'd advice you to read Robert Martin's Clean Code. It helped me a lot while writing my open source projects, such as this one (browse the code here). When reading back code I've written, I always ask myself:

  • Is there a better name for this method?
  • Are the statements in this function in the same level of abstraction?
  • Can I get this method (even) more readable?
  • Can I get the method smaller (I'll hardly write methods bigger than 10 lines of code)?
  • Can I give the variables better names?
Steven