
Read your Standard Libraries - bcjordan
http://blog.codingforinterviews.com/reading-code-standard-libraries/
======
unwind
This is great advice, since in general you would like to assume that the
standard library is implemented well, i.e. by people who know the target
language.

Unfortunately, for C this is pretty hard to do, at least for the GNU libc. I'm
not trying to critique the implementation, but every time I dive in (generally
to help someone on Stack Overflow) I'm confused and get to spend a lot of time
searching the code.

It's obviously optimized for a great deal of other parameters before ease of
reading, which I totally understand yet still am a bit sorry to see.

~~~
chubot
Yeah, a lot of good programmers have alternatives to libc, either for reasons
of history or taste.

I was just reading DJB's daemontools (less than 6K LOC, very tidy!), and he
doesn't use libc. Actually his code has been collected in libdjb, which seems
quite nice:

[http://www.fefe.de/djb/](http://www.fefe.de/djb/)

And also the Plan 9 / Go guys don't use ANSI libc as far as I remember.

Those are two examples of people who don't even use the ANSI libc interface.
But there are plenty of people who don't use GNU libc, and use uclibc or
various other source-compatible alternatives. I think Debian switched to
eglibc awhile ago.

~~~
burntsushi
Just FYI, I believe that by default, binaries compiled with the standard Go
toolchain _do_ link against your system's libc (which is probably GNU libc on
Linux). However, I believe that can be disabled by compiling the Go toolchain
with `CGO_ENABLED=0` set. But you might lose some functionality.[1]

[1] - [https://groups.google.com/forum/#!msg/golang-
nuts/H-NTwhQVp-...](https://groups.google.com/forum/#!msg/golang-
nuts/H-NTwhQVp-8/i5vhUB5dZ5QJ)

~~~
chubot
So I'm actually talking about what the Go compilers _themselves_ use for libc,
not Go programs. From what I can tell it is:

[http://code.google.com/p/go/source/browse/include/libc.h](http://code.google.com/p/go/source/browse/include/libc.h)

Which is definitely not ANSI libc. Go programmers won't care about this,
because it's an implementation detail, but we're talking about reading source
code.

~~~
burntsushi
Ah! Mea culpa. I completely misunderstood your initial comment then. You're
absolutely right.

------
arielweisberg
Excellent advice. Reading other people's (good) code is one of the best ways
to learn how real world problem solvers solve real world problems.

The Java standard library is interesting and easy to jump into if you work
with it every day. Guava is another good library to study.

I personally spend a lot of time looking at Riak, Cassandra, Hadoop, and
Postgres since developing databases is my day job. I also follow mailing lists
for both developers and users to understand the real world outcomes of their
design choices and use that to inform how I think rather then going into every
problem a blank slate.

------
pmr_
I mostly live in C++ land and the story is different there. The standard
library implementations are a place of magick, mystery, and obscure defect
reports. They provide plenty of insight what can go wrong if you are writing a
generic library and how difficult correctly programming with templates
actually is, but all the technicalities of the language make it hard to see
the algorithms. They are definitely worth reading, but only if you have
mastered a large amount of C++.

Not to mention the horrible naming conventions (leading double underscores
everywhere).

I often think about a C++ standard library written for educational purposes
with proper naming, focus on readability instead of optimization, and maybe
multiple possible implementations of certain specs.

~~~
maaku
No one would use it. It's a design flaw of C++ that performant, generic
libraries require arcane magic and hideously obfuscated code.

~~~
pmr_
Please read carefully: "for educational purposes". I'm fully aware that such a
library wouldn't be fit for production but it maybe would be useful for people
learning generic C++ programming. Currently you have to look at Boost or any
of the standard libraries and all you see are #ifdef and all the arcana and
have a hard time seeing all the useful techniques.

~~~
maaku
What I meant was: no one would use it, so it'll never be created.

------
avisk
I always make sure that I attach the source code of the 3rdParty libraries
used in my project to my IDE. The best time to read library source code is
while using them in your code, where we know the context. This also help in
writing better code. I feel that we should make reading source code of APIs we
use, integral part of the development process rather than reading the standard
library source code for the sake of reading it.

------
steveklabnik
This is generally great advice, but the Ruby standard library isn't exactly
the best to read if you want to learn. Don't get me wrong, it works, but the
code is generally very, very old. Nobody knew how to write good Ruby code back
then.

~~~
bcjordan
Ah, will certainly trust your assessment there. :)

In your opinion, what are some good starting points for interested Ruby
readers ?

~~~
bradleyland
If you're speaking of code reading, I find looking at the source code of
Rubinius very interesting.

Ruby has so much utility wrapped up in places like Enumerator, that you don't
see a lot of examples of low-level data structure implementation in the wild.
You could look at MRI Ruby, you'd be looking at a lot of C, which tells you a
lot about how Ruby works, but doesn't show you much actual Ruby. That's where
Rubinius is really great. You get to see how a smart team would implement
Ruby... in Ruby.

[https://github.com/rubinius/rubinius](https://github.com/rubinius/rubinius)

~~~
netghost
I have to second this, if you use Ruby at all, Rubinius is quite fascinating
to read, even if you still use MRI on a day to day basis.

------
fit2rule
I'm kind of surprised this isn't "a thing". I mean, I'm surprised its 'a
thing'. Because, isn't this standard practice - I mean, isn't just a part of
the coder Creed that you read the code, anywhere and everywhere you can?

------
edwinnathaniel
This is where Java ecosystem shines the most:

1\. Easy access to standard libraries

2\. Most (not all, re: java.util.Date) of the standard libraries are well
thought-out and the documentation is top-notch

3\. Tools like Maven + Eclipse/IntelliJ can provide insight to Java libraries
source code easily (Maven has built-in capabilities to download the javadoc
_and_ the source code from the designated Maven repository if the author
publish them correctly).

Access libraries (3rd party or well-known ones) are a shortcut away.

PS: NuGet had this capability recently, which is great, but I rarely use it so
I don't know how easy and to what extend NuGet can replicate what Maven has.

------
emilv
This is extremely useful, not only for inspiration and learning, but also so
you actually know what is happening under the hood. This is great for
troubleshooting and for reasoning about your own code. This is one big pro for
open source in development!

I learned a lot by reading the source code for Python dicts (which also comes
with a lengthy motivation for why it was implemented that way) and any Haskell
library (Hackage links from the manual page directly to the source code for
the respective function, which makes it very easy to see what is happening).

~~~
sg47
Is the Python dict source code available in the link provided in the article?

~~~
bcjordan
Raw source:
[http://svn.python.org/view/%2acheckout%2a/python/tags/r266/O...](http://svn.python.org/view/%2acheckout%2a/python/tags/r266/Objects/dictobject.c?revision=84293&content-
type=text/plain)

Walkthrough of the implementation: [http://www.laurentluce.com/posts/python-
dictionary-implement...](http://www.laurentluce.com/posts/python-dictionary-
implementation/)

~~~
dalke
And a PyCon video: [http://pyvideo.org/video/276/the-mighty-
dictionary-55](http://pyvideo.org/video/276/the-mighty-dictionary-55) .

~~~
sg47
Thanks both of you!

------
bcjordan
In case my Wordpress-on-DreamHost-unlimited-plan blog goes down, here is a
gist copy:
[https://gist.github.com/bcjordan/8242593](https://gist.github.com/bcjordan/8242593)

~~~
bradleyland
Looks like you had the good sense to install WP Super Cache. I don't know how
much traffic you're seeing, but my blog has held up under some HN traffic
using the same setup (Dreamhost+WP+WP Super Cache).

~~~
bcjordan
Hah! The WP Super Cache "Activate" button was actually giving me 503 responses
the first few tries. Now it's nice and speedy.

------
dinosaurs
I think this is great advice and something I've been meaning to do for Ruby.

However, what in the case of JavaScript/Node.JS developers? What could/should
they read to help them understand their language/frameworks better?

I've been reading through the Express source code lately to understand the
module better. I'm wondering what other source code I could read to get a
better grasp on Node/JS.

Thoughts?

------
drblast
I always liked Common Lisp for this, since you could so easily inspect data
structures like hashes at run time.

I think CMUCL had a really interesting and unique hash implementation that
relied on two identically sized arrays, one for the keys and data, and another
for a "next" index that showed you where to go for a key collision.

------
mail2vks
Completely agree. Java collections and threads package well written code.
Infact I ask interview questions based on API implementation

------
yawboakye
I dig into Rails by accident. Most of the time I don't have internet and don't
remember exactly how a certain API is used. So I jump into the code with
`source_location` or `bundle open ...` and get what I want. I usually see
something new. That's how I discovered Dir[]

------
TazeTSchnitzel
PHP's Zend and /ext/standard are unfortunately unreadable and undocumented.

~~~
alextingle
You read them if you want to regress, as a programmer.

