Your favorite language can be anything - Lisp, Python, C, Haskell, etc.<p>Which codebases are the most elegant ones written in your favorite language that new comers to the language can learn something from?
I'd nominate Java itself.<p>In the decades Java sources have been available, any of the kazillion junior programmers could debug-step into java.* and see exactly what's happening -- progressive disclosure at its finest for building expertise.<p>Most other languages have a hard boundary, so the roots of language are a matter of conceptual documentation and experience. Java also offers that fly-over knowledge, but when problems arise, programmers love knowing exactly and seeing directly.<p>Personally, in a pinch I prefer the actual to the elegant.
Go and it’s standard library. ‘Elegant’ is pretty subjective, but it’s a treasure trove of learning how things work under the hood. I’ve learned loads about networking, compression, encryption and more by browsing through it, because the code is super easy to read and understand.
RT-11 on the PDP-11 (all in PDP-11 assembler). Back in the early 80's you could rebuild it in several different ways, depending on whether you wanted it "single job" (as it was back then) or not.<p>As an example, here's the PDP-11 assembler to convert a binary number to decimal ASCII (no idea who wrote it, but they certainly made every word count):-<p><pre><code> CNV10: MOV R0,-(SP) ;Subroutine to convert Binary # in R0
1$: CLR R0 ;to Decimal ASCII by repetitive
INC R0 ;subtraction. The remainder for each
2$: SUB #10.,@SP ;radix is made into ASCII and pushed
BGE 1$ ;on the stack, then the routine calls
ADD #72,@SP ;itself. The code at 2$ pops the ASCII
DEC R0 ;digits off the stack and into the out-
BEQ 2$ ;put buffer, eventually returning to
CALL CNV10 ;the calling program. This is a VERY
MOVB (SP)+,(R1)+ ;useful routine, is short and is
RETURN ;memory efficient.
</code></pre>
Courtesy bitsavers.org (<a href="http://www.bitsavers.org/pdf/dec/pdp11/rt11/v5.6_Aug91/AA-PD6LA-TC_RT-11_System_Macro_Library_Manual_Aug91.pdf" rel="nofollow noreferrer">http://www.bitsavers.org/pdf/dec/pdp11/rt11/v5.6_Aug91/AA-PD...</a>)
So many, I like reading how other people write code.<p>R - sf package is a clean example of functional OOP<p>Python - pytudes (Peter Norvig’s notebooks)<p>Haskell - Elm compiler. I could mostly understand what’s going on even though I barely know any Haskell.<p>Ruby - Sequel is really nice.<p>Rust - Ripgrep<p>Pretty much any F# codebase is super readable too.
Shewchuk’s Triangle code for Delaunay mesh generation is some of the most elegant C-code I’ve ever seen [1]. It even won the SIAM Wilkinson Prize for numerical software at the time it was written.
It’s written in a literate style, and builds up from the most fundamental arithmetic operations and data structures, all the way to one of the most efficient mesh generators of its kind.
He has a few other codebases written in a similar style [2, 3]. It’s very different from what you see in the wild, but it’s been great for understanding what’s happening under the hood for complex algorithms like mesh generation.<p>1. <a href="https://www.cs.cmu.edu/~quake/triangle.html" rel="nofollow noreferrer">https://www.cs.cmu.edu/~quake/triangle.html</a><p>2. <a href="https://github.com/ctlee/Stellar">https://github.com/ctlee/Stellar</a><p>3. <a href="https://www.cs.cmu.edu/~quake/robust.html" rel="nofollow noreferrer">https://www.cs.cmu.edu/~quake/robust.html</a>
Working with Perl, two things spoiled me for other languages: JSON and DBI/DBD.<p>In Perl, everything serializes to JSON without fuss. It can be "lossy"; objects without an explicit JSON serialization method and coderefs can't be deserialized, but serializations at least have placeholders for them.<p>Compare to Python's json module, which doesn't try very hard to serialize things and throws exceptions every time it runs across something new. It's very frustrating to use.<p>Perl's DBI provides a universal API for all databases, with implementation-specific details in an underlying DBD module (which both provides glue between the DBI abstraction and programmer access to features specific to different database systems).<p>Compare to Python, where you need to import a different module and use a different API for every different kind of database. As I increasingly use Python for a living, I frequently wish they'd follow Perl's example with this.
C++ this file covers all the math for working with NURBS curves and surfaces:<p><a href="https://github.com/solvespace/solvespace/blob/master/src/srf/ratpoly.cpp">https://github.com/solvespace/solvespace/blob/master/src/srf...</a><p>There is a lot more in other files - triangulation, booleans, creation - but the core math functions are there in very readable form.
Some past threads for dang to macroexpand:<p><a href="https://news.ycombinator.com/item?id=33413124">https://news.ycombinator.com/item?id=33413124</a><p><a href="https://news.ycombinator.com/item?id=32384769">https://news.ycombinator.com/item?id=32384769</a><p><a href="https://news.ycombinator.com/item?id=25355171">https://news.ycombinator.com/item?id=25355171</a><p><a href="https://news.ycombinator.com/item?id=15663980">https://news.ycombinator.com/item?id=15663980</a><p><a href="https://news.ycombinator.com/item?id=18037613">https://news.ycombinator.com/item?id=18037613</a><p><a href="https://news.ycombinator.com/item?id=14836013">https://news.ycombinator.com/item?id=14836013</a><p><a href="https://news.ycombinator.com/item?id=34205294">https://news.ycombinator.com/item?id=34205294</a><p><a href="https://news.ycombinator.com/item?id=13624926">https://news.ycombinator.com/item?id=13624926</a><p><a href="https://news.ycombinator.com/item?id=16960300">https://news.ycombinator.com/item?id=16960300</a>
For Lisp I would recommend Edi Weitz's CL-PPCRE: <a href="https://edicl.github.io/cl-ppcre/" rel="nofollow noreferrer">https://edicl.github.io/cl-ppcre/</a><p>Pretty much anything else from Edi Weitz is also great.<p>Mezzano is quite elegant in my opinion, especially for an operating system. For example, this is the USB mass storage driver: <a href="https://github.com/froggey/Mezzano/blob/master/drivers/usb/mass-storage.lisp">https://github.com/froggey/Mezzano/blob/master/drivers/usb/m...</a>
Back when I was starting out, it was nice that Backbone had such clean, well-commented code: <a href="https://github.com/jashkenas/backbone/blob/master/backbone.js">https://github.com/jashkenas/backbone/blob/master/backbone.j...</a>
I really like Pandoc codebase [0]. It is a document converter written in Haskell.<p>Reading it’s source code a decade ago was a turning point for me. Prior to that, I always felt an insurmountable gap between my toy codebases and real projects. All those open source software written in C++ etc. looked so unapproachable that I felt like I could not write production ready software.<p>Pandoc however, was written in a language I didn’t know and did something very complicated very thoroughly, yet remained accessible. It was very nicely laid out and I could easily follow how it constructs it’s internal representation of documents and converts between them. I think this made me catch the functional programming bug for the next decade that let me build way bigger things than I had any right to, without getting crushed underneath all the complexity.<p>Putting together something in Java or even contributing to OOP Python codebases was still like an exercise in frustration, no matter how much better I thought I’m getting at programming I would feel stupid trying to wrap my head around those abstractions and hierarchies. Somehow FP just clicked for me and made me see how I could start from a simple library call and little by little build the complete program.<p>Today I am comfortable with all kinds of paradigms and levels of abstraction, but I definitely owe a lot to Pandoc for showing me I was smart enough to understand and modify real world software I did not build myself.<p>[0] <a href="https://github.com/jgm/pandoc">https://github.com/jgm/pandoc</a>
At the beginning of the year I was rewriting a SPA and looking for ideas on how to structure a web app. One project I looked at was Github Desktop and I think it has very clean code for an app.<p><a href="https://github.com/desktop/desktop">https://github.com/desktop/desktop</a>
I've really enjoyed reading (and playing with) kons-9's codebase in Common Lisp. It lets you do exploratory 3D modeling via the program's GUI or the repl. The source is very neatly structured and readable. I don't see many CL projects that do 3D rendering, so it's nice to see!<p>Quick demo:<p><a href="https://www.youtube.com/watch?v=i0CwhEDAXB0">https://www.youtube.com/watch?v=i0CwhEDAXB0</a><p>Repo:<p><a href="https://github.com/kaveh808/kons-9">https://github.com/kaveh808/kons-9</a>
For C, I've really enjoyed reading the Ruby source code. There are good and bad spots, but overall especially if you know Ruby the language, the source is both entertaining and enlightening.
I’m surprised that nobody has posted about Elixir yet. I nominate the excellently written Phoenix library. Not only is the code well organized and easy to find, the documentation is expansive and right next to the code.<p><a href="https://github.com/phoenixframework/phoenix">https://github.com/phoenixframework/phoenix</a>
I don't know if elegant is the word, but I often refer to the Hashicorp repos for Go, and specifically admire Mitchell's own. Very clean, well written code most of the time.
C++<p>An old one, but the FTGL library (renders truetype fonts in old-school OpenGL in half a dozen different ways: texture-per-letter, texture-per-word, 2D polygons, 3D polygons, etc) is the best example of C++ inheritance I've ever run across. The code formatting isn't my favorite, and comments are sparse, but the hierarchy of objects subclassed for all the different rendering modes is just about perfect. <a href="https://sourceforge.net/p/ftgl/code/HEAD/tree/trunk/" rel="nofollow noreferrer">https://sourceforge.net/p/ftgl/code/HEAD/tree/trunk/</a><p>Perl<p>The Mojolicious / Mojo toolkit. Great minimalist API and great documentation and clean code all around. <a href="https://metacpan.org/release/SRI/Mojolicious-9.33/view/lib/Mojolicious.pm" rel="nofollow noreferrer">https://metacpan.org/release/SRI/Mojolicious-9.33/view/lib/M...</a>
arthur whitney's famous first J interpreter.<p><a href="https://www.jsoftware.com/ioj/iojATW.htm" rel="nofollow noreferrer">https://www.jsoftware.com/ioj/iojATW.htm</a><p>The first version of KDB the timeseries database was written by arthur in C to bootstrap the K interpreter that was used to write KDB. It was 26 files, named a.c to z.c, none larger than a single page of C written with notepad.exe.
C++ isn't my favorite programming language anymore.<p>I guess the Doom 3 source code is an elegant codebase.
Surprisingly, they used C++ features quite sparsely.<p>I think that’s nice because in programming it’s about getting the job done and not about using all sorts of features for other reasons.
fs2 (reactive streaming, <a href="https://github.com/typelevel/fs2">https://github.com/typelevel/fs2</a>) written in Scala. It shows how nicely things can compose in a typesafe way if the language supports it.<p>And then, the opposite is Monix (<a href="https://monix.io/" rel="nofollow noreferrer">https://monix.io/</a>) also written in Scala. It's also about reactive streaming and the API is great, but the internal code is ugly because it sacrifices readability/composability for performance.
[C#] Bitwarden server repository is probably one of the cleanest (non-novel) solution architectures I have seen so far. I always point people to it as learning material for structuring the code. It is not the most minimalistic but I feel like it strikes very good balance and does not follow blindly all the """fancy""" OOP patterns people should never use anyway.<p><a href="https://github.com/bitwarden/server">https://github.com/bitwarden/server</a><p>Otherwise, I can see why people are burned, average Java/C# codebases look abysmal and written without understanding of (not) using heaps of mediator/factory/adapter/provider classes.
The various flavors of Forth all qualify. They are great examples of how to bootstrap towards a powerful abstraction, starting from a relatively simple context. The 80% of the 80/20 rule here comes from "writing a Forth" being an engaging learning experience.<p>For anyone who would like to do that, I recommend looking up one of the old standards(FIG-FORTH, FORTH-79, FORTH-83) and implementing the words in them with whatever tools and languages you like. You will hit a point where your early assumptions about the implementation are wrong. Keep going, get it to the point where you are capable of using the metaprogramming words.
<a href="https://github.com/lua/lua">https://github.com/lua/lua</a><p>Everything is nicely documented and pretty easy to read.<p>It's a great companion if you want to learn more about how languages are implemented.
There were 3 different libraries that impressed me:<p>1. The SunOS headers. Couldn't see the code but the number of header files was small enough in the 4.x Sun libraries that you could read them all. Mostly the documentation was terse but information dense.<p>2. The TeX sources. Knuths literate programming seems like such a missed opportunity.<p>3. ParcPlace VisualWorks. Due to the nature of Smalltalk you could read all the source code, learn object patterns directly from the people who invented it. A great learning experience when transitioning from C and so much better than the cfront based C++ hack.
What I find to be elegant in a codebase is where the chosen abstractions or seams that separate subdomains/concerns is well chosen that each is handled mostly in one place without a lot of ceremony or machinery. I would call these 'well factored' for factors which are domain-specific.<p>Each language has different ways that may lend itself as part of this shaping, but I like being able to think of this as something to look for when reading and strive for when writing, irrespective of the language at-hand.
Ruby - Devise. A beautifully functional gem that also makes customization of its magic really easy. Also ActiveRecord, as it's just a delight to read.
Peter Norvig's Advent of Code solutions in Python. e.g., for 2022 - <a href="https://colab.research.google.com/github/norvig/pytudes/blob/main/ipynb/Advent-2022.ipynb" rel="nofollow noreferrer">https://colab.research.google.com/github/norvig/pytudes/blob...</a>
I found Zig implementation of json parsing is interesting. The code is free from hidden control flow !.<p><a href="https://github.com/hanabi1224/Programming-Language-Benchmarks/blob/main/bench/algorithm/json-serde/1.zig">https://github.com/hanabi1224/Programming-Language-Benchmark...</a>
Back when I first was learning about GraphQL in 2016, I remember thinking the reference implementation in JavaScript was extremely clear, easy to read, and well organized. It had a "wow, I wish all the code I read was this good" feel to it for me.
The TurboVision library that came with Turbo Pascal was awesome. Borland really had their shit together back in the day. Then they got greedy and overpriced their product, and killed the community.
I think FreeBSD code in C is very well organized:<p><a href="https://github.com/freebsd/freebsd-src">https://github.com/freebsd/freebsd-src</a>
C++: ClickHouse. It is very readable and easy to navigate. Call stacks that you pick up in logs are very descriptive, and you can easily tell what went wrong.
Not sure if it's still the case but about 6 years ago Facebook's folly C++ library was something I'd point to for my junior engineers to get a sense of "good" C++ <a href="https://github.com/facebook/folly">https://github.com/facebook/folly</a>
My own ( c# ). It's not opensource yet, monetizing it first.<p>Full DDD, it was a refactor during COVID-19 for my ecommerce.<p>It powers <a href="https://belgianbrewed.com" rel="nofollow noreferrer">https://belgianbrewed.com</a><p>Copy paste from: <a href="https://news.ycombinator.com/item?id=35257225">https://news.ycombinator.com/item?id=35257225</a><p>> But I believe the project is much cleaner and frankly better to understand than all other projects i've encountered for this size. I'm using DDD, so DDD knowledge is a requirement to navigate this in a breeze :) :<p>- <a href="https://snipboard.io/D03VWg.jpg" rel="nofollow noreferrer">https://snipboard.io/D03VWg.jpg</a> - General overview of the architecture. Small fyi: Connectors => Autogenerated nugets to call the api's<p>- <a href="https://snipboard.io/9M24hB.jpg" rel="nofollow noreferrer">https://snipboard.io/9M24hB.jpg</a> - Sample of Modules + Presentation layer<p>- <a href="https://snipboard.io/ybp6EH.jpg" rel="nofollow noreferrer">https://snipboard.io/ybp6EH.jpg</a> - Example of Specifications related to catalog ( = products )<p>- <a href="https://snipboard.io/lE9vcK.jpg" rel="nofollow noreferrer">https://snipboard.io/lE9vcK.jpg</a> - How specifications are translated to the infrastructure ( here I'm using EF, so I'm using Expressions a lot), but plain old SQL is also supported. A query is basically a list of AND/OR Specifications where a hierarchy is possible. This would translate to "(QUERY 1 ) AND ((QUERY 2) AND (QUERY 3))" in the Infrastructure layer.<p>- <a href="https://snipboard.io/7rVBpk.jpg" rel="nofollow noreferrer">https://snipboard.io/7rVBpk.jpg</a> - . In general, i have 2 List methods ( one for Paged queries and one not for Paged queries)<p>Additional fyi: Is V2, so has some legacy code. Uses my own STS. Has 2 gateways ( the ShopGateway that is used to develop new sites and the BackendGateway for the Backend). Enduser frontend is in MVC for SEO purpose, Customer backend is in Angular ( SPA). The basket is a NoSql implementation on top of SQL server.<p>The enduser frontend supports a hierarchy of themes ( so it's insanely flexible to create variations of pages for other clients).<p>There are more projects involved outside of this solution, eg. nuget repo's usable accross solutions (JWT, Specifications, ...) and "plugins" for a standalone project that is installed for end-users for syncing local data. So it's +101 projects :)<p>Edit: my specification implementation has been open-sourced here: <a href="https://github.com/NicoJuicy/Specification-Pattern-CSharp">https://github.com/NicoJuicy/Specification-Pattern-CSharp</a><p>Someone was curious to see it.