9 ms·
Ask HN: What source code is worth studying?
I realized recently that it's been a long time since I looked at anyone else's code outside of the context of debugging or working on it for hire. In your opinion, what are some examples of particularly well-designed or implemented software projects worth looking at to broaden one's own horizons as a programmer?
- avinashv 17y agoSQLite's source listing is great. I learned a lot about good C practices and documentation reading it.
- davidw 17y agoIf you like that style of C code, Tcl's is similar (and it's not a coincidence, as Dr. Hipp is part of the Tcl core team).
- avinashv 17y agoThanks for the tip, I didn't know that. I haven't ever used Tcl besides some tinkering a few years ago, whereas SQLite is used incredibly often, but I'll read any C that's well written.
- mahmud 17y agoYou will enjoy "C Interfaces and Implementations", along with the full, literate source-code for LCC. This is also good, Standard Function Library: http://legacy.imatix.com/html/sfl/ http://legacy.imatix.com/html/sfl/
- ori_b 17y agoWhere can one find the full literate source code for LCC? I haven't been able to find it anwyhere, and god knows I searched.
- mattyb 17y agoI'm guessing here: http://www.cs.princeton.edu/software/lcc/ http://www.cs.princeton.edu/software/lcc/
- ori_b 17y agoThere's no literate source linked to on that page. Just plain, normal, barely-commented C.
- plinkplonk 17y ago"There's no literate source linked to on that page. Just plain, normal, barely-commented C." The book by David Hanson is the literate program.
- ori_b 17y agoOk, so the literate source, in a form that I can actually play with, isn't acutally available. It doesn't even seem to be maintained in literate form in SVN. That's what I was trying to determine.
- jeremyw 17y agoEspecially its deep test architecture.
- clutchski 17y agoan intro: http://www.sqlite.org/testing.html http://www.sqlite.org/testing.html
- arthurk 17y agoIf you're interested in literate programming: TeX
- jeduan 17y agoAnyone willing to recommend a Java project worth studying?
- projectileboy 17y agoI highly recommend both Paul Graham's source code for Arc (http://ycombinator.com/arc/arc3.tar http://ycombinator.com/arc/arc3.tar), as well as the source code for Quake and Quake II (http://www.idsoftware.com/business/techdownloads/ http://www.idsoftware.com/business/techdownloads/).
- lpolovets 17y agoThis is half answer/half tangent, but I like http://www.google.com/codesearch http://www.google.com/codesearch a lot for browsing and reading code. A fun thing to do is to look at how others implement something you want to implement, or use some library you want to use. You end up finding a lot of different ways of doing the same thing, and if you find something that you want to explore more, it's easy to browse around.
- scott_s 17y agoThe Linux kernel: http://miller.cs.wm.edu/ http://miller.cs.wm.edu/
- dpifke 17y agoEqually or more valuable than trying and sit down to read the kernel source is to follow the patches and discussions thereof on the linux-kernel mailing list: http://lkml.org/ http://lkml.org/ I've found that doing so a) improves my knowledge of what's going on under the hood tremendously, and b) has taught me a fair bit about how to do code reviews and make difficult design decisions. Studying the kernel itself line-by-line would take ages, but reading a few threads a week depending on what piques my interest is totally manageable.
- scott_s 17y agoI don't treat is a novel, but a reference. I'll often wonder how certain things are implemented, and I'll poke around for the data structures and algorithms until I gain some understanding of how it works.
- jsyedidia 17y agoI highly recommend the Stanford GraphBase, written by Donald Knuth. It's C code written by Knuth using the literate programming tool CWEB. If you don't know about CWEB, it's not hard to learn how to use, and probably is already installed on your system if you have TeX. The Stanford GraphBase is available for free download on Knuth's web-site, and also comes bound in a nice paperback book, that was reprinted in 2009. Knuth also makes available many other programs written using CWEB on his web-site, but I would start with the GraphBase.
- shabda 17y agoDjango's code is well written, and very well documented.
- axod 17y agoI think you tend to learn more studying badly written badly documented code. Certainly there's a lot of knowledge to be gained being able to decipher spaghetti code with no documentation. I guess it depends on your aims though.
- known 17y agohttp://httpd.apache.org http://httpd.apache.org and http://jboss.org http://jboss.org and http://lxr.linux.no http://lxr.linux.no
- kyenneti 17y agoAny recommendations for php? I wrote a php/mysql web application for SaaS customers. I am looking at ways to improve performance/cache/error handling or simply write a better code.
- eru 17y agoPerhaps MediaWiki?
- philjr 17y agoPersonally, I wouldn't recommend it. Unfortunately I can't give a better alternative, but I've done some hacking on MediaWiki since version 1.7 and while it's some of the better examples of PHP out there, it still leaves lots to be desired in different places.
- eru 17y agoThanks. My post was just a guess.
- jeduan 17y agoI learnt a lot by using frameworks as symfony. The initial learning curve is steep, but then you can understand architecture better. The jump to other languages such as python and java was really easy after that.
- bnmrrs 17y agoThe symfony framework and Doctrine ORM are both well written projects.
- jparise 17y agoThere's a lot of excellent PHP code in the Horde Project's repository: http://cvs.horde.org/ http://cvs.horde.org/
- gmoothart 17y agoThere is no such thing as excellent PHP code.
- mmc 17y agoFor systems programming, I've learned a lot from reading the source for open solaris: http://src.opensolaris.org/source/ http://src.opensolaris.org/source/ And the LLVM compiler is far more understandable than gcc sources: http://llvm.org/viewvc/llvm-project/llvm/ http://llvm.org/viewvc/llvm-project/llvm/
- yan 17y agoI agree with this, and along the same thought, OpenBSD (or any BSDs really, but OpenBSD tends to favor simpler implementations) source is very clear and concise.
- davidw 17y agoMinix, not being as 'real world' as the BSD's or Linux, is even more readable, if you're just interested in a quick glance at how an OS works.
- judofyr 17y agoFrom Minix' site: MINIX 1 and 2 were intended as teaching tools; MINIX 3 adds the new goal of being usable as a serious system on resource-limited and embedded computers and for applications requiring high reliability. Is Minix 3 still readable, or do you recommend Minix 2?
- davidw 17y agoGood question. My experience was with Minix 2: I needed a floppy driver to weld on to eCos, and Minix's was by far the easiest to deal with. I haven't looked at Minix 3.
- mhansen 17y agoMinix3 has comments galore, but seems plagued by one/two letter variable names like 'c' and 'ip'
- mahmud 17y agoYour compiler's source code. That should shake your confidence in the world.
- pgbovine 17y agofor an easier time, your favorite language interpreter's source code ... interpreters are easier to comprehend than compilers, since you can 'follow along' with the flow of execution in the main interpreter loop just like you're executing a program ... e.g., creating stack frames, allocating heap memory, assigning variables, etc.
- stcredzero 17y agoSqueak Smalltalk. For Java, if you can get hold of it, the old Acme Webserver. That was very clean code.
- omouse 17y agoSqueak is pretty sweet. I was looking at the Kernel category and the Parser classes. Pretty clean stuff.
- btilly 17y agoThis really depends on the language. Ruby's code is (or at least was) quite nice. It is pretty much OO written in plain C. By contrast Perl makes very heavy use of macros for portability, to an extent that may induce brain lock in many people, and makes your debugger very hard to follow. This is not to say that its use of macros is a bad thing in the end, but it is a definite shock.
- scrame 17y agoYeah, after hacking with perl for a while, i got interested in how it worked under the hood, and had some trouble following. Check out the regex compilation module: http://cpansearch.perl.org/src/GBARR/perl5.005_03/regcomp.c http://cpansearch.perl.org/src/GBARR/perl5.005_03/regcomp.c And execution: http://cpansearch.perl.org/src/GBARR/perl5.005_03/regexec.c http://cpansearch.perl.org/src/GBARR/perl5.005_03/regexec.c
- nixme 17y agoI haven't perused it myself, but every time I see this question posed, someone always mentions Lua: http://www.lua.org/ftp/ http://www.lua.org/ftp/ Similar previous discussions: http://news.ycombinator.com/item?id=225577 http://news.ycombinator.com/item?id=225577 http://www.reddit.com/r/programming/comments/26dyh/ask_reddit_whats_the_most_beautiful_piece_of http://www.reddit.com/r/programming/comments/26dyh/ask_reddi...
- amichail 17y agoTeXmacs http://www.texmacs.org/tmweb/download/sources.en.html http://www.texmacs.org/tmweb/download/sources.en.html
- wallflower 17y ago"Code reading requires its own set of skills, and the ability to determine which technique to use when is crucial. In this indispensable book, Diomidis Spinellis uses more than 600 real-world examples to show you how to identify good (and bad) code: how to read it, what to look for, and how to use this knowledge to improve your own code." http://www.spinellis.gr/codereading/ http://www.spinellis.gr/codereading/
- mbubb 17y agoThis is a good read and uses lots of examples from one of the BSDs (NetBSD as I rememeber).
- misterbwong 17y agoAny recommendations for good C# code? Much of the web is open source oriented so I don't hear too much about great C# code.
- shrughes 17y agoThere are some code examples on Eric Lippert's blog that are made of good C# code.
- statictype 17y agoMono's c# compiler is written in c#. SharpDevelop is another good choice.
- alexkay 17y agohttp://banshee-project.org/ http://banshee-project.org/ (disclaimer: I'm one of developers)
- dtf 17y agoOne project I found fascinating reading a while back when I was working on graphics, is AGG. It's a vector graphics toolkit, a bit like Cairo, Quartz 2D or Java 2D. It uses a particular style of C++ - a mixture of template programming together with regular polymorphism - to build up a collection of rendering components. The components are almost Unix-like - you plug them together to build your customized rendering pipeline, and the C++ compiler's template logic takes care of making everything hardwired (or dynamic, if you choose to use the polymorphic components): http://www.antigrain.com/ The end result is a toolkit which is at a lower level than Quartz 2D et al, but could easily be used to build such an API. It can also be adapted for embedded, or high precision uses.
- tmsh 17y agohttp://www.haiku-os.org/documents/dev/painter_and_how_agg_works http://www.haiku-os.org/documents/dev/painter_and_how_agg_wo... http://bit.ly/131cxL http://bit.ly/131cxL (.doc file) those help. if you can figure out the source code without the documentation at the agg site + those files, more power to you. but basically you are nuts.
- dtf 17y agoThanks for the links, I've never seen those before. I read the agg code without the docs, but it would have been difficult without the example programs. The docs definitely help when looking at some of the algorithms though. I remember spending a good day or two staring at the anti-aliasing code, together with the freetype rasterizer (on which it was based), trying to figure out what the hell was going on. Then in just clicked.
- revetkn 17y agoIf you're an iPhone developer, Joe Hewitt's three20 is the best open-source codebase I know of: http://github.com/joehewitt/three20 http://github.com/joehewitt/three20
- boucher 17y agoThree20 is really useful. It's also quite powerful. But I wouldn't recommend it to someone trying to learn/study idiomatic Cocoa. Joe tends to do things his own way, which works out fine, but does tend to depart from most people's Cocoa/Touch code.
- staticshock 17y agoI hear good things about the webkit source code
- subbu 17y agoAny recommendations for Ruby/Rails projects?
- ujeezy 17y agoI liked Jamis Buck's (aka the creator of Capistrano) Bucketwise: http://github.com/jamis/bucketwise http://github.com/jamis/bucketwise
- delano 17y agoRyan Tomayko recommends Unicorn as a reference for Unix programming (primarily, signals and sockets): http://tomayko.com/writings/unicorn-is-unix http://tomayko.com/writings/unicorn-is-unix I found Sinatra to be an interesting read (and specifically how it parses the app configuration into methods): http://github.com/sinatra/sinatra http://github.com/sinatra/sinatra
- dasil003 17y agoI'm a big fan of ThoughtBot's stuff (http://www.thoughtbot.com/projects http://www.thoughtbot.com/projects). Mostly I use Shoulda and FactoryGirl, but I've been consistently impressed with their ideas and code quality. For idiomatic Ruby and Rails stuff I have probably learned more from Rick Olson's stuff (http://github.com/technoweenie/ http://github.com/technoweenie/) than any other single source. Sometimes I think he tends to be too clever for his own good, but the code is good to read for that reason even if simpler things are better in production.
- al3x 17y agoRead the source to Marcel Molina's AWS::S3.
- subbu 17y agoAh! Somebody published a timely list of high quality rails apps http://jetpackweb.com/blog/2009/10/14/high-quality-ruby-on-rails-example-applications/ http://jetpackweb.com/blog/2009/10/14/high-quality-ruby-on-r.... Discussion here http://news.ycombinator.com/item?id=882071 http://news.ycombinator.com/item?id=882071
- sgoraya 17y agoQuake source code: www.idsoftware.com/business/techdownloads/ From a graphics and game engine perspective, it was very informative to go through pieces of of the source - I was mainly interested in the client/server and collision detection areas of the code.
- thismat 17y agoI'm still pretty wet behind the ears, but looking through Quake source (and quake 2), was quite the experience. I though the code was beautiful, as well as impressed with how DRY everything seemed to be....oh, and variable naming, small things like that, really impressed me.
- aw3c2 17y agoCheck out this recent analysis of parts of the source: http://www.fabiensanglard.net/quakeSource/ http://www.fabiensanglard.net/quakeSource/
- yesimahuman 17y agoI second this. It's cool that the ideas behind some of the more simple things in the Quake game (such as the console, bindings, commands, etc.), are still being used as the back bone of other products (see: Valve). They are implemented very elegantly in the Q1 source.
- keefe 17y agoFor Java, all of Apache's stuff is good - particularly Jakarta. http://jakarta.apache.org/ http://jakarta.apache.org/ This covers a wide range of topics and is designed to have a public API. http://code.google.com/p/google-collections/ http://code.google.com/p/google-collections/ and http://code.google.com/p/guava-libraries/ http://code.google.com/p/guava-libraries/ and http://code.google.com/p/google-guice/ http://code.google.com/p/google-guice/ are interesting in that they come out of Google, beyond their own individual merits.
- ori_b 17y agoPlan 9's source (http://plan9.bell-labs.com/sources/plan9/sys/src/ http://plan9.bell-labs.com/sources/plan9/sys/src/) The Lua VM's source (http://www.lua.org/source/5.1/ http://www.lua.org/source/5.1/)
- geocar 17y agoQmail: http://cr.yp.to/qmail.html http://cr.yp.to/qmail.html It feels well organized almost throughout, and it's a joy to experience the clarity of thought that went into it. In fact, most of djb's code has a similar feeling to it. Arthur Whitney's code: http://www.nsl.com/papers/origins.htm http://www.nsl.com/papers/origins.htm Being able to read this (and not merely decode it, but read it, in a manner similar to how you would read a book) will broaden your horizons like nothing else I've seen.
- slackenerny 17y agoMore of crazy Whitney code available at http://aplusdev.org http://aplusdev.org
- statictype 17y agoI learned about sockets and networking by looking at the source for wget. That's also where I saw calloc.c - A portable implementation of calloc. I distinctly remember it being one of the cleverest hacks I've seen.
- subbu 17y agoAny good event-driven JavaScript (jQuery/Prototype is also fine) projects out there?
- grayrest 17y agoEvent driven JS tends to go spaghetti really quickly unless you're using a good events framework. The yui3 and sproutcore frameworks are pretty close to opposite ends of the spectrum in terms of philosophy, but both have very well defined processes for dealing with events. The two frameworks you mention are fine for adding "ajax" to a more or less working server based app but they aren't so hot for building a complete app. FWIW, MochiKit's Signal framework is also nice (though the codebase is now fallow) and Dojo's system has a sort of nifty aop feel, but dojo is kind of hairy in that there are a lot more chefs in the kitchen there.
- justinlilly 17y agoCheck out sammy.js. Its basically a web framework on top of jquery with a plugin architecture, etc. Seems pretty neat.
- c00p3r 17y agoopenssh
- DanielStraight 17y agoOne way I've found to start reading code (something that I do not find easy at all) is to read code you use. Lately, I've been reading pyparsing and the Python markdown module. As an added bonus, I also discovered features in those libraries that I never knew existed.
- rev_null 17y agomemcached It's short and clearly written. And it demonstrates some neat things, like memory management.
- sriramk 17y agoI'm going to get downvoted for picking a non-open source option but... The NT kernel is the most beautiful piece of code I've seen. Dave Cutler and team wrote some very, very elegant code that anyone (even if you're not a kernel hacker) can understand. If I need 'code inspiration', I spend some time looking through their code. Now, the NT kernel itself doesn't have source out there but for folks who are students, you should be able to get your hands on the Windows Researh Kernel (http://www.microsoft.com/resources/sharedsource/windowsacademic/researchkernelkit.mspx http://www.microsoft.com/resources/sharedsource/windowsacade...). This has all the good stuff (and is much easier to build too)
- anatoly 17y agoI agree that it's very beautiful code. I wouldn't go as far as "the most beautiful I've seen", personally, but it's really, really good. Also, when you come to it, you're typically shell-shocked from reading lots of crappy Win32 code, and it's just such a nice change.
- slackenerny 17y agoThe NT kernel is the most beautiful piece of code I've seen Modulo Plan9 kernel. It's also lot smaller a read (ca. 50 KLoC and that's it). NT kernel itself doesn't have source out but for folks who are students And folks who are hackers should be able to look in certain places to find the leaked version ;>
- dtf 17y agoAll the more heartbreaking that it was forced to don the hideous mantel of the Win32 API (itself further scarred from numerous assaults such as C2 compliance!)
- huhtenberg 17y agoI spent several years writing drivers for NT and I don't know which part of the NT kernel you were looking at. The parts I saw were an utter mess lacking any sort of elegance whatsoever. If there were two ways to implement something, one simple and the other one contrived, you can bet your MCSD certificate NT will be using the latter. The overall impression was that it was hacked together in the worst sense of the word. Like there were several teams developing it that did not communicate with each other. Pieces of the kernel are forced together rather than fit snugly by design. I cannot stress enough how mind-boggingly repulsive it was to work with it. And that's not even touching NDIS, the networking module, which can be used for scaring some serious shit out of young programmers. If you put BSD or Linux code side by side with NT, look me in the eye and tell me that NT kernel is "the most beautiful piece of code you've seen", you will need to see a head doctor.
- biohacker42 17y agoDVD John's DeDRMS written in C#. It was beautiful, but I can't seem to find the source code right now. If anyone can please share. --EDIT--- Found it: http://web.archive.org/web/20050315135351/http://nanocrew.net/software/DeDRMS/DeDRMS-0.1/DeDRMS.cs http://web.archive.org/web/20050315135351/http://nanocrew.ne...
- plinkplonk 17y agoOne way to find code worth reading is to select inspiring developers and look at their code. Most of the developers in "Coders At Work" for example, have Open Source code we can look at learn from. The best code I've seen: - Common Lisp - "Paradigms of Artificial Intelligence Programming" by Peter Norvig and "On Lisp" by Paul Graham C - "C Interfaces and Implementations" and "LCC- a compiler for ANSI C" both by David Hanson. I also found the code for the Player/Stage robotic sim framework surprisingly readable. I liked the Scala Actors library code as well. If any HNers know any great codebases in Haskell or Erlang, please post here.
- mbowcock 17y agoFor haskell I would recommend Xmonad. The code is short, lots of interesting functionality including a well written plug-in system. http://xmonad.org/ http://xmonad.org/
- caffeine 17y agoXmonad is nice code indeed. For an introduction to a piece of it, check out Simon Peyton Jones's talk at OSCON 2007 - he uses Xmonad code as an introduction to Haskell, which gives you a taste of how the program is constructed.
- davepeck 17y agoThe FreeType 2 font rasterizer is beautiful code. Geometric computation meets performance optimization meets just-well-written-and-commented C. http://git.savannah.gnu.org/cgit/freetype/freetype2.git/tree/src/raster/ftraster.c http://git.savannah.gnu.org/cgit/freetype/freetype2.git/tree...
- josephruscio 17y agoDiomidis Spinellis has written two books (Code Reading and Code Quality) on this subject that take you through annotated examples of industrial strength systems code (mostly from BSD IIRC): http://www.spinellis.gr/codereading/ http://www.spinellis.gr/codereading/
- alexitosrv 17y agoAnyone can point to a good javascript code?
- tudorg 17y agoI find the sources of ExtJS nice to read. They have an OOP approach.
- gord 17y agoI'd really like to see some functional-style JS code. [ background : I've used OOP a lot, and thought it was the ultimate, until I discovered lisp... ]
- gord 17y agoHmm.. How about the ADsafe code - http://www.adsafe.org/adsafe.js http://www.adsafe.org/adsafe.js by Douglas Crockford, author of 'JavaScript : the good parts' - http://www.crockford.com/ http://www.crockford.com/
- bd 17y agoI learned a lot from Qt: http://qt.nokia.com/ http://qt.nokia.com/ And it's not just their code, everything they do is exemplary. It's one extremely well run software project. See for example their documentation: http://doc.trolltech.com/ http://doc.trolltech.com/ http://doc.trolltech.com/qq/qq13-apis.html http://doc.trolltech.com/qq/qq13-apis.html
- newsdog 17y agoI've been led to understand that the OpenBSD source code is of amazing quality.
- djnym 17y agoI've heard this book was good http://www.amazon.com/Beautiful-Code-Leading-Programmers-Practice/dp/0596510047/ref=sr_1_1?ie=UTF8&s=books&qid=1255463914&sr=8-1 http://www.amazon.com/Beautiful-Code-Leading-Programmers-Pra.... It asks 30 different developers for pieces of beautiful code. I read an excerpt or two back when it was released and put it on my to read list. Unfortunately haven't gotten to it yet, but still plan too.
- kokofoo 17y agoThe Lions book. http://www.amazon.com/Lions-Commentary-Unix-John/dp/1573980137 http://www.amazon.com/Lions-Commentary-Unix-John/dp/15739801...
- natemartin 17y agoAny recommendations for good objective-C code?
- spokey 17y agoWard's Wiki (The Portland Pattern Repository) has a short list of "Programs to Read" at http://c2.com/cgi/wiki?ProgramsToRead http://c2.com/cgi/wiki?ProgramsToRead.
- tolmasky 17y agoI don't know if this is good advice today, but back in the day I learned a lot both from STL and the Boost libraries, specifically Spirit, which I still think is an amazing feat: http://spirit.sourceforge.net/ http://spirit.sourceforge.net/
- mdakin 17y agoThat of a metacircular evaluator such as the one described in SICP.
- deleted 17y ago[deleted]
- wallflower 17y agoFlex: FlexLib (mostly by Doug McCune) http://code.google.com/p/flexlib/ http://code.google.com/p/flexlib/
- thesnark 17y agoLAPACK http://www.netlib.org/lapack/ http://www.netlib.org/lapack/
- colbyolson 17y agoCould anyone recommend something for Python? If something is out there for ruby, there's got to be at least one thing for python.
- tmsh 17y agoHow about Ned Batchelder's implementation of Frank Liang's hyphenation algorithm: http://nedbatchelder.com/code/modules/hyphenate.html http://nedbatchelder.com/code/modules/hyphenate.html or AI: http://aima.cs.berkeley.edu/python/readme.html http://aima.cs.berkeley.edu/python/readme.html
- revorad 17y agoR internals - http://cran.r-project.org/doc/manuals/R-lang.html http://cran.r-project.org/doc/manuals/R-lang.html
- frosty 17y agoi will say unless you plan to make small changes, you wont get much out of studying the source code. fixing small bugs in an open source project will expose you to lot of portions of source code and also give you some rep in case you want to become a contributor later