views:

100

answers:

5

How does one study open-source libraries code, particularly standard libraries? The code base is often vast and hard to navigate. How to find some function or class definition?

Do I search through downloaded source files? Do I need cvs/svn for that? Maybe web-search? Should I just know the structure of the standard library? Is there any reference on it? Or do some IDEs have such features? Or some other tools? How to do it effectively without one? What are the best practices of doing this in any open-source libraries? Is there any convention of how are sources manipulated on Linux/Unix systems? What are the differences for specific programming languages?

Broad presentation of the subject is highly encouraged. I mark this 'community wiki' so everyone can rephrase and expand my awkward formulations!

Update: Probably didn't express the problem clear enough. What I want to, is to view just the source code of some specific library class or function. And the problem is mostly about work organization and usability - how do I navigate in the huge pile of sources to find the thing, maybe there are specific tools or approaches? It feels like there should've long existed some solution(s) for that.

A: 

Well, I think that it's insane to just site down and read a library's code. My approach is to search whenever I come across the need to implement something by myself and then study the way that it's implemented in those libraries.

And there's also allot of projects/libraries with excellent documentation, which I find more important to read than the code. In Unix based systems you often find valuable information in the man pages.

rogeriopvl
+1  A: 

One thing to note is that standard libraries are sometimes (often?) optimized more than is good for most production code.

Because they are widely used, they have to perform well over a wide variety of conditions, and may be full of clever tricks and special logic for corner cases.

Maybe they are not the best thing to study as a beginner.

Just a thought.

dmckee
A: 

Wow, that's a big question.

The short answer: it depends.

The long answer: Some libraries provide documentation while others don't. Standard libraries are usually pretty well documented, whether your chosen implementation of the library includes documentation or not. For instance you may have found an implementation of the c standard library without documentation but the c standard has been around long enough that there are hundreds of good reference books available. Documentation with hyperlinks is a very useful way to learn a new API. In any case the first place I would look is the library's main website

For less well known libraries lacking documentation I find two different approaches very helpful.

First is a doc generator. Nearly every language I know of has one. It basically parses an source tree and creates documentation (usually as html or xml) which can be used to learn a library. Some use specially formatted comments in the code to create more complete documentation. JavaDoc is one good example of this. Doc generators for many other languages borrow from JavaDoc.

Second an IDE with a class browser. These act as a sort of on the fly documentation. Some display just the library's interface. Other's include description comments from the library's source.

Both of these will require access to the libraries source (which will come in handy if you intend actually use a library).

Many of these tools and techniques work equally well for closed/proprietary libraries.

codeelegance
A: 

The standard Java libraries' source code is available. For a beginning Java programmer these can be a great read. Especially the Collections framework is a good place to start. Take for instance the implementation of ArrayList and learn how you can implement a resizeable array in Java. Most of the source has even useful comments.

The best parts to read are probably whose purpose you can understand immediately. Start with the easy pieces and try to follow all the steps that are hidden behind that single call you make from your own code.

Jeroen van Bergen
A: 

Something I do from time to time :

apt-get source foo

Then new C++ project (or whatever) in Eclipse and import.

=> Wow ! Browsable ! (use F3)

Gzorg