Ask HN: What are some examples of good code?

321 points by amingilani ↗ HN
I keep reading on HN that I'm order to become a better developer I need to write code everyday, and more importantly read other people's code.

What are some examples of good code that you've seen?

Personally I'm interested in Ruby on Rails and I've recently started going through GitLab-CE[1] but what is your favor code?

[1]: https://gitlab.com/gitlab-org/gitlab-ce

167 comments

[ 3.5 ms ] story [ 209 ms ] thread
https://github.com/pallets/flask and https://github.com/kennethreitz/requests

both in python, but beautiful code, well structured and you would not need any docs, just read the code

Flask is absolutely beautiful; definitely agree with reading through it.
I don't want to take anything away from requests. It's an extremely useful library. However, I do a code walkthrough of various popular libraries with my students and I've never been satisfied with requests internals. The emphasis, I've felt, is more on clean, stable, public API rather than internal consistency and cleanliness. I don't pick it up these days for my classes. Instead, we usually go through the python standard library. Especially the code from Raymond Hettinger.
that sounds like a class I would love to take. any suggestions on what in the standard library to check out?
We usually do collections. Especially the implementation of the OrderedDict. It's a nice combination of compromises, careful coding and usefulness.

I used to do requests but like I said, moved away. I sometimes do pygments to discuss how different lexers etc. are "plugged" in and also to show how messy the command line parsing code there is.

This batch will probably be a little different since I'm reworking it completely. The syllabus for the first batch is here if you're interested in what we covered http://thelycaeum.in/blog/2014/06/10/mentoring_course_time_t...

I've read that Redis (https://github.com/antirez/redis) has well written code, in C.
I've also heard that Redis is incredibly well written. However, is it common for production C code to have a single file with 5,300+ lines?

https://github.com/antirez/redis/blob/4.0/src/cluster.c

> However, is it common for production C code to have a single file with 5,300+ lines?

It's definitely not uncommon, but yes, that file is on the long side. However, it is rather clean and well-organized.

It may be due to the poor module system (header files) of C where you end up making files slightly longer than ideal because splitting into two files means also adding some header files and thus adding to the maintenance burden.

When writing C code, I usually strive to put a whole "module" in a single file, so that no "private" interfaces have to be put in header files. Everything that needs to be exposed in order to test a module goes in the public interface.

It's a tradeoff and C being C doesn't help (and C++ wouldn't really help here IMO), but I see nothing wrong with the file you picked. It's a bit long but it looks well done to my C programmer eyes.

Right off the bat, I see multiple instances of str* functions where strn* functions should be used. I'm also wondering about this variable "server" which seems to have an unknown definition. There are lots of gotos, and many other deferred high-quality conventions, such as explicit parenthesis in comparisons, re-ordered fields in comparisons, chains of else ifs, where a switch would be preferable.

I also see lots of prototypes (all?!) defined at the top of the file instead of in a header file. They could be made static.

Also, the generic type "int" isn't a good idea because it can change, it helps avoid porting problems to specify the size (int32_t, uint64_t, etc.)

And the memcpy()'s should be changed to memcpy_s().

On the bright side, the comments are quite good and plentiful. Refreshing!

Upvoted the op. I'm learning Java so if anyone has some particularly good and idiomatic Java (Java 8 preferably) that'd be awesome.
I don't have an example to give, but i can offer this.

During the prototype phase, anything goes, speed is paramount, you just have to make it sort of work. Don't get hung up practicing pretty looking code during this phase.

You should always practice, but don't get hung up.

Prototype given the green light? Ready to dive into the build phase?

I'll say this, google for "coding style guide"

The challenge with style guides as an approach to "nice code" is they often focus on the code equivalent of writing a grammatically correct line of code -- things that can be often caught with a linter.

Where I see things fail a lot more is in the code equivalent of "How do I structure a paragraph" or "How do I organize this paper", which most things under the "style guide" umbrella don't cover.

I've always thought of the "how to write grammatically correct code" style guides as being relevant to writing high quality code in the context of a team that may onboard a new person or an internal transfer: they provide some baseline guarantee (insofar as the team honors them, of course) of the structure and formatting of the code. This reduces some amount of friction in a team. The benefits provided are largely context dependent (e.g. they'll benefit teams that expect to have a couple of senior engineers and many juniors on a relatively constant basis more than a team of experienced engineers in general), but I think having a style guide is important for any "large enough" engineering organization.

I've also always insisted on including at least some loose guides to cover the "how do I structure a paragraph" cases. These generally are going to vary on a project by project basis, though, so they're going to be more vague as a rule.

Especially with Rails as rapid prototyping language this is kind of a bad advice IMO. Usually the prototype already will be the base of your application and we both know you will never get rid of all the dept you could build that way
My use of prototype was probably a poor choice. Not sure what is better replacement.
I see where you are coming from but this just does not fit the way a typical rails coder operates.
If you are into ruby/rails already start reading internels instead of docs whem using rails. Rails has a very nice ans clean code base and they do a lot to keep it that way
Without trying to start a flame war, while there is definitely good code in Rails, my personal opinion is that the architecture of Rails is not something you necessarily want to emulate. And this is really the problem when you are new, I think. Many people will love one thing while others will hate it. It's difficult to create an informed opinion.

My advice is actually not so much different than yours. Definitely checkout the Rails internals, but keep a reserved opinion. Then contrast it with other things. From a framework/library perspective look at Sinatra or other web frameworks that that a completely different philosophy. Try to understand the advantages and disadvantages to many approaches.

Again, I want to stress that my intent is not to be critical of Rails (heck it provides the bulk of my paycheck and I'm very grateful it exists!). My intent is really to say not to be so hung up about finding good code, but rather take a long term view of comparing different approaches and thinking through for yourself what you think is good. People who have a different view point than mine, shed light in corners that I won't look. That's always a good thing.

(comment deleted)
One of my favorite simple examples of what clean and elegant code can be is Peter Norvig's spellchecker: http://norvig.com/spell-correct.html

It's in Python and a single file but it comes with a wonderful description and shows how a complicated task can be broken down into a few small and powerful functions.

But small systems are easier to get nice. Once they are not small it is there where all the hell begins. Alas, it is a big systems where nice code helps most.
Definitely true! You also have the luxury of tweaking every line of code for a small system to get it perfect.

It's also the reason for trying to break down larger systems into smaller, elegant components.

Sorry, I don't have a link to some beautiful code.

I assume that as a developer you are interested in solving (business?) problems through the act of writing software?

It isn't much different than being a painter I guess. To be able to be a good painter (or to be considered a good painter) you first need to have a good grasp on how to use the brush and how to handle paint (e.g. oil paint), i.e. you need to learn the technique. The more versed you become with the technique the better you will become at painting, or, over time you will become better at painting what you intent to paint, to paint what's in your minds-eye because you don't have to think about the brush and paint anymore.

When it comes to software you first need to have a good grasp on programming. This means you will need to spend time practising the act of programming. Using two languages that are very different from each other might be good. E.g. learn an imperative and functional language. In your case this might e.g. Ruby and Lisp. Your programs will need to interact with other systems so you probably need to learn about operating systems, databases, queues, networking, etc. You probably don't have to be an expert in everything but being a good all-rounder will certainly be beneficial.

Over time you will see that it becomes easier to think in solutions of the bat rather than focusing on how you're going to solve a problem. This is basically what is being referred to as experience.

So, to be a good developer you need to put in the effort and you need to put in the time. There usually aren't any short cuts. I've been doing this professionally for over 20 years and I'm still learning every day.

"So you're an architect eh, and you want to study existing architecture to see how it was built, eh, to get better? Well you can't. Sorry, I don't have any buildings or architectural blueprints ready for you. That's not how you learn. You need to study architecture, not look at buildings and how they're built. There's no shortcuts here. I've been an architect for over 20 years and I've never looked at a building."
OP says "I keep reading on HN that I'm order to become a better developer I need to write code everyday, and more importantly read other people's code."

It hardly sounds like he is looking for a shortcut

UNIX v6 source code:

http://v6.cuzuco.com/v6.pdf

A tiny C-subset JIT:

https://news.ycombinator.com/item?id=8746054

It might even be controversial to suggest these are examples of "good code" today, because the majority of code I've seen lately seems to be overly verbose and complex. In contrast, these are extremely simple and concise for the amount of functionality they contain. I think this style has unfortunately disappeared over the decades of promoting lowest-common-denominator so-stupid-even-an-illiterate-could-read-it coding styles and enterprise OOP design-pattern bloat-ism, but when I think of "good code", I don't think of Enterprise Java; I think of code which, upon being told what it does, makes you think "wow, I never thought it would be so simple."

Not Ruby but for C code, I like Redis's code:

https://github.com/antirez/redis/tree/unstable/src

I don't personally use the product, but I find the source well written and always share it as an example of nicely done C code.

Here is some nice Erlang code I like -- network packet parsing:

https://github.com/msantos/pkt/tree/master/src

Notice how concise this is:

    codec(
       <<4:4, HL:4, ToS:8, Len:16,
       Id:16, 0:1, DF:1, MF:1, %% RFC791 states it's a MUST
       Off:13, TTL:8, P:8, Sum:16,
       SA1:8, SA2:8, SA3:8, SA4:8,
       DA1:8, DA2:8, DA3:8, DA4:8,
       Rest/binary>>
    ) when HL >= 5 -> ...
This is due to the beauty of binary pattern matching. You could kind of do it in C by casting to a struct but notice here it is also calculating header length (HL) as part of the match operation. So it can do a bit more than just casting a binary blob to a struct.

Another thing here is that it is also big endian by default so there is not need for htons(), htonl() and such functions sprinkled throughout the code.

Not disputing the code quality of either above project, but over valuing brevity / being concise is a common cause of problems. Specifically, people will often sacrifice readability and robustness for brevity.

At this point in my career I don't really value being concise at all. Which is not to say you shouldn't ever think about "Can I write less code here?" but the goal there should be writing less code to make what you're writing more robust, more stable, more readable and not just shorter.

If there's one thing I could tell every programmer early in their career (including a younger me) it would be; nobody's going to be impressed with how clever you were when they're frustrated trying to understand what the heck is going on. Including you in 6 months.

It's amazing that this is still highly debatable in our industry. Always write to make debugging easier. Always. There's so many people that worship cleverness over readability.
In all fairness, a stunningly clever insight can make things seem so obvious that you forget how hard it was to have that original insight.

This kind of brevity is extremely helpful when thinking about a problem. You "just" have to grok the mechanics thoroughly. This really helps in designing effective higher level abstractions.

Unequivocally admonishing brevity for ostensible readability kind of ignores the trade offs. I think there are other ways to bootstrap grokkability without having to sacrifice the conceptual clarity of nicely compact code.

As someone taking a lot of ML/AI courses ( a subject new to me) , these clever insights are popping up all around me lately, and I agree once you learn it, it's hard to imagine what it was like before.

I'm hitting a lot of 'aha' moments which has really been emotionally satisfying, it's been a long time since I had to push myself I guess.

There is a difference though when it is something custom and new (say if this code defined or parsed a new protocol) vs parsing an existing protocol. Notice https://tools.ietf.org/html/rfc791#section-3.1 section diagram. That code is basically an almost perfect translation of the RFC diagram, down to the bits involved. That is the beauty here: minimal impedance mismatch between the specification and code.
As a side, what are some good ways to start understanding the bigger projects? Blogs and documentation are helpful but mostly don't talk about the underlying implementation details.

Digging through the code line by line takes a long time and while one does learn a lot, I am sure there has to be a better way of doing this.

Any static analysis tools that help in this regard? Or any other tool/approach that you might recommend?

I am leaving my current team, in part for this very reason. Senior technical people in the organisation don't value readable, understandable code, favouring 150 character one-liners instead. It's proven to be a very effective way of increasing the time taken for anybody except the author to debug issues. In my view, this has increased the perceived value of those responsible.

I'm sure that wasn't their ultimate intention but there's certainly no incentive to change!

The Redis code doesn't look exceptionally good to me. I just looked through five files, but things like spacing and the use of braces seem to be pretty inconsistent. There are also a lot of magic numbers and quite a few variables have poor names.
Noticed those too, also I don't personally like camelCase in C however I still like the code because of how it is laid out and solves the problem. It has the right amount of comments (too many of those is also bad), some functions are large but overall has the a good balance there too.

Also for an example it is also the right sized project. It i not too small and not huge (like Linux kernel) and the overall structure can be understood in a few hours of browsing through.

I agree that things like spacing and inconsistent use of braces may cause code not to look good, but don't really feel it is a surefire indicator of the code not actually being good.
#include "lzfP.h"

# define FRST(p) (((p[0]) << 8) | p[1])

ref = hslot + LZF_HSLOT_BIAS; hslot = ip - LZF_HSLOT_BIAS;

I pity all C programmers who have to deem this beautiful

That looks clear and concise to me.

It's a part of a performance sensitive mission critical infrastructure. The parts you posted are some of the core data structures that make it fast by squeezing every bit and byte that reasonably can. There are no points awarded for being pretty.

A quick browse around Redis source code looks very clean and elegant to me. There are "hard" low level parts like the stuff you quoted (out of context) and then there are "easy" parts that implement the higher level functionality. It has a decent amount of comments.

Taking one of the "nasty" bits out of context is disingenuous. Every computer program has nasty bits and they're usually there for a reason.

> That looks clear and concise to me?

What's a FRST? The actual code appears to do a LE encoding of a u16, but I can't fathom why you'd call that "FRST". But maybe in context it makes more sense.

The rest, IDK. Can't tell what an "LZF hslot bias" is. Again, might make sense in context.

None of this code was meant to be understood without background knowledge of the algorithm internals - and some code just is that way. You don't go reading rbtree.c without first reading about Red-Black Trees in your algorithms book.

If I recall correctly, the file you grabbed these from had a lot of comments that thoroughly explain how it works. At least many other files in the redis repo do.

They could have named it "FIRST" or whatever FRST is short for. That would have made zero impact to the code quality but a longer name would make things clumsy. It's a file-local helper macro and not a part of any public interface or a crucial piece of the architecture.

Feel free to disagree, but picking two nasty lines out of context in an otherwise well designed and implemented code base has zero bits of information about the quality of the project.

A URL to the actual source helps, I think, to put it in context: https://github.com/antirez/redis/blob/d680eb6dbdf2d2030cb96e...

The parent poster didn't link, so I lacked the context to really evaluate it. Your persistence at waving this away as "making sense with background knowledge of the algorithm internals" drove me to find it.

> If I recall correctly, the file you grabbed these from had a lot of comments that thoroughly explain how it works

It's fairly light on comments. Not completely devoid of them, but the code can't stand in isolation. The algorithm itself, LZF, seems to be a variation of LZO, but not notable enough to make Wikipedia.

> They could have named it "FIRST" or whatever FRST is short for.

The fact that you're unsure of this fact defeats your next point, at least for me:

> That would have made zero impact to the code quality

Why? One extra letter immediately disambiguates it, and now we know that it at least means "first".

> a longer name would make things clumsy

If you look at the file it's used in, it has exactly two uses. The readability gained from the extra vowel in this case is well worth it, and would in no way make the code "clumsy". A four-byte (five if you count the commented out code) change so that the reader doesn't need to play guess-the-word? Worth it, in my opinion.

The documentation above it too,

> just believe me, it works ;)

But why does it work? We'll never know, b/c that's bound up in the author's head.

This is perfectly fine and readable. No need to pity anyone, especially not C programmers. :)
It's not, and they are just saying it is, because its the hipster thing to do. Good code is simple and understandable. Redis code is neither.
What you show in Erlang example is just a syntax. The surrounding code or usage or approach may be bad/ugly. It has nothing to do with code quality.
SQLite/Fossil source tree is a piece of art, practically every single function is well commented, written in clean C, easy to read. DRH has done an astonishing work. I'm impressed by his work. In my company where we do embedded C, the programming style is based on him. https://github.com/symisc/
I took a quick look recently at id software's GitHub repos of their old blockbuster games (Wolfenstein, Doom, Quake) and gotta say it was the first-ever C codebase I encountered that I found eminently readable.. (whether it's "good C", I can't assess though)
I agree, the id code is very well organized and readable.
Doom original C sources what made me realise how simple and powerful C could be.
Your post inspired me to inspect element to see the front-end code on HN only to find out the layout is all tables...
The implementation of Project Oberon. It is so sensible that it can be hard to notice the magic.

http://www.projectoberon.com

That is a good read for many reason. Recommended to anyone interested in programming, operating systems and software architecture.
This stuff about "reading code" is basically bullshit and nobody does it. http://www.gigamonkeys.com/code-reading/

> Seibel: I’m still curious about this split between what people say and what they actually do. Everyone says, “People should read code” but few people seem to actually do it. I’d be surprised if I interviewed a novelist and asked them what the last novel they had read was, and they said, “Oh, I haven’t really read a novel since I was in grad school.” Writers actually read other writers but it doesn’t seem that programmers really do, even though we say we should.

> Abelson: Yeah. You’re right. But remember, a lot of times you crud up a program to make it finally work and do all of the things that you need it to do, so there’s a lot of extraneous stuff around there that isn’t the core idea.

> Seibel: So basically you’re saying that in the end, most code isn’t worth reading?

> Abelson: Or it’s built from an initial plan or some kind of pseudocode. A lot of the code in books, they have some very cleaned-up version that doesn’t do all the stuff it needs to make it work.

I don't read code just to learn in general, but I read it to find specific undocumented details or to figure out how someone solved a particular problem. Of course, you've got to read a ton of code if you're working in a large project.

It is a great way to find new ways of doing things, and to gain experience-based opinions, but I certainly wouldn't be doing it if I didn't have a very specific goal in mind.

Most novelists are bad. Most devs are bad. No one seems to know how to tell they are a bad one, but I think we have a good filter: do you voraciously read other people's work?

Code in books is dumb. The real code to read is other code you use or interact with. eg reading the jquery source or the Rails source or code reviewing a colleague.

Almost always one does this while answering a question about this source. Trying to understand what a library call is doing or tracing a bug.

You learn a lot this way including coding tricks and style points.

> This stuff about "reading code" is basically bullshit and nobody does it.

Some guy says in an interview it's bullshit, so it is?

You never read other people's code? Not when the documentation's lacking? Not when the documentation's fine, for a framework say, but you need to see an example of how it's actually used in the context of a real application?

The implication that I get from the quote is that people don't read code for the sake of reading code.

And that matches my experience: When I read code, it's precisely because the documentation is lacking, or because I want to match up an error message produced by the code with the logic preceding the error message's origin.

Or, quite often, you read it because you want to change it - to extend it or to fix a bug.
I think the best way to both read code and gain experience is to set a goal of at least weekly or monthly contributions to a couple of open codebases that benefit your life.
But if you look at the article you'll see what I think is a salient point -- looking at code and figuring out how it works often looks more like an "investigation" (like, set some breakpoints and step through it, for instance). It's nothing like sitting in front of a fireplace with a novel.
Choose your own adventure books are still reading though.

I think that there's also a much more similarity with analyzing a novel than reading one for pleasure. Reading code has a much greater mental tax than pleasure reading, it requires more focus to actually put together what's going on, and that's similar to analyzing a novel for a report, where you might read the same passage 4-5 times, taking notes. Or even better yet, analyzing a poem, which are often short (and often obey 80 char line width rules!) and analyzing them can require a great amount of focus and thought to decide why a poet used this word in this way, and what meaning they intended to convey and such. That's much more similar to reading code than reading a novel, and I expect also less common.

I wouldn't call Abelson just "some guy" but you obviously didn't click through and read what the article says.
Hal Ableson..."some guy". C'mon now. True, he's a person with an opinion but let's give him a bit more respect than some random javascript douche.
(comment deleted)
I mean that's all definitely true but if you consider that you're trying to pick up new motifs, different ideas, and understand how people organize the thought and concept behind their code, reading another person's code is invaluable. I think the tricky part comes with looking for quality code. One should go in with an open eye looking to satisfy what you're trying to solve, and read enough to learn how to do that oneself. I don't think casual reading of code will ever be as entertaining as a novel, but if you want to get better by solving specific problems it is a great way to go.
I'm managing a side project with four devs and I read lots of code. Since It's all performance-oriented algorithmic code, reading is the only way to know what's going on. And none of the code is crudded up. It's actually pretty nice.
Looking at the code of a project you're working on or managing isn't really the same as just sitting down and reading code for the sake of it.
The thing is, reading code is hard. Even well written code is hard to read and grok, the other kind can take days to absorb.

But it is very effective as a way of spotting certain kinds of bugs, while at the same time gaining a much deeper understanding of a codebase.

Reading code in a non trivial way is a very special skill that not many developers have, but that is very worthwhile to attain.

Whenever I come to a new codebase, I start by grabbing a pretty random bug and reading the code in that area. just flicking through, forcing myself to ask questions about it and finding out the answers.

actively engaging with code like that is an amazingly effective way of absorbing a codebase.

Personally I find it easier to read code than to read an English explanation of the code. Obviously comments do help with reading code but in general it's the code that really explains things for me rather than the human description.

I suspect this might be a side effect of my dyslexia, but it's something which often works to my advantage.

I disagree. Especially when switching from one framework or language to another, it's a great shortcut for picking up on idioms.
Maybe not too many people read code just to read code, but you absolutely have to read code when working on a project that's not your own, and that experience is very valuable.
I read others code in code reviews, and as necessity to get the job done. I very occasionally read others code say in GitHub, but not for any desire to get better, but out of curiosity. But when I do that I get bored quickly!
The single most important change I've made in how I learn new tools/frameworks/whatnot is Github code searches -- I spend far more time there than on StackOverflow. Maybe this is more important in Scala land, where there are far fewer books and talks and blog posts to work from, and the tool stack is evolving a lot more, but I've found answering many questions of structure / "How do people incorporate this concept into a larger application" to live largely in code.

The answer is never exact, though. Something will have 10 different solutions, and being able to look at them and discuss the tradeoffs that went into the decision is a definite skill.

I wonder what Fabien Sanglard would think of this.

To me, the Quake 1/2 source code has been an invaluable source of insight, as much for small algorithmic stuff (like collision/sliding code) than for its architecture as a whole (like game/engine separation, the edict_t system) ...

Have you never asked yourself "how did they do this" ?

You'll end up reading code if you work on code. Find something to work on. (I don't know what you should work on.)

Just like you can't really learn math just by reading the book, you'll "learn good code" much more effectively if you're working on it and you have to make decisions and judgments to move forward.

> You'll end up reading code if you work on code.

This.

Also, you'll have a more goal-directed path through the code, which is usually more motivating than understanding code for the sake of understanding.

Of course, there's always the pitfall that people will start making changes willy-nilly, until the code does roughly what they want, without really understanding anything.

But this will vanish as soon as you put higher quality standards to your own work. (e.g. if you want to ensure that it does the correct thing in all edge cases, or if you produce something larger than a quick hack)

> e.g. if you want to ensure that it does the correct thing in all edge cases, or if you produce something larger than a quick hack)

... if you submit your code to the project's owner. :)

But you'll notice this is nothing like the idea of "reading code" for the sake of it.
You've never thought "gee this is really cool software, I wonder how they implemented xyz..."?
While he might make a few usable points, this article is drenched in non-recognized bias. He takes his (as I see it, heavily bottom-up leaning -> 'in order to grok it I have to essentially rewrite it.') cognitive style and applies it as universal.
Except that what led him to this point was interviewing multiple world-renowned programmers for his book "Coders at Work" and finding essentially none of them read code regularly either.
Still making it only anecdotal - and even more obviously biased (I'd hazard a guess that the interviewees are quite far from the norm in how they learn...)
OK, sure, the data set is small. Do you know anybody who actually does it? Have you seen them doing it?
Yep. I do it, for example. A few of my colleagues do it. Others work more like described in the article.

(But I know that I myself are somewhat of an outlier, since I'm a rather extreme top-down learner.)

Spot on. Most of the time I can barely stand reading my own code, so how about I take a break and read some barely intelligible code written by someone else, using a different coding style and a different mindset? Wreck my brain trying to figure out what on Earth the other person was thinking when they wrote that abomination. You know, just for the kicks.
Wrecking your brain trying to figure out what another person was thinking is a pretty fascinating way to appreciate the incredible complexity of human beings and how other minds work differently than your own.
When you do it as a day job for prolonged periods of time (where the other people put little to no effort into writing code that doesn't resemble raw encryption), the fascination wears out real fast.
(comment deleted)
Check out the WASTE text API, its written in C and its very nicely done.
At the top of https://github.com/darius/code-canon I collected a list of other places people have answered this quesion. (The rest of the page is mostly a list of books with worthwhile code, because I'd already written it and because it's harder to think of code outside of books that I can recommend as easily.)
I really like Unicorn[1]. It's a very well-architected webserver with a nicely structured codebase. Reading the code has taught me about some neat tricks, e.g. self-pipe trick, and a lot about `fork`.

Additionally, I've on occasion consulted the Linux kernel and PostgreSQL repos. Would recommend, although I'm definitely either lying or very ignorant if I said I was familiar with them.

1: https://github.com/defunkt/unicorn

PostgeSQL has some very easy to read and well documented source code. Great place to look if you need anything related to db data structures.
A few weeks ago, I spent some time in reading O'Reilly's Beautiful Code (http://shop.oreilly.com/product/9780596510046.do). The book is essentially a collection of essays from various programmers describing what they think of as beautiful code.

I particularly liked Brian Kernighan's description and implementation of a regex matcher, and Travis Oliphant's discourse about multidimensional iterators in NumPy.

Worth a read.

One of the first tech talks I remember was Bryan Cantrill at Google, and I vaguely remembered that he mentioned at the beginning of that talk that he had contributed to a book called Beautiful Code, so I double-checked and it seems to be the same book.

Looking back up the talk, it points to his review of the book here: http://dtrace.org/blogs/bmc/2007/07/28/on-the-beauty-in-beau...

> More specifically: read [two specific chapters with very different attitudes on what makes code beautiful]. It seems unlikely to me that one person will come away saying that both are beautiful to them. (And I’m not talking new-agey “beautiful to someone” kind of beautiful — I’m talking the ”I want to write code like that” kind of beautiful.) This is not meant to be a value judgement on either of these chapters — just the observation that their definitions of beauty are (in my opinion, anyway) so wildly divergent as to be nearly mutually exclusive. And that’s why the title is perfect: both of these chapters are beautiful to their authors, and we can come away saying ”Hey, if it’s beautiful to you, then great.”

I've been told by several people that they look to Flask as well written, well designed code that everyone can learn from.