Which codebases are the most elegant ones written in your favorite language that new comers to the language can learn something from?
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.
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.
Personally, in a pinch I prefer the actual to the elegant.
For an example of "modern Java" I would point at something like this (which I wrote, sorry about the hubris):
Checked exceptions get a bad rep, but I've frequently found taking advantage of their hierarchical nature can be beneficial to at least making them less bad to catch. When I work with languages that have no exception support at all, I sometimes even miss them.
Comment for a method says "null safe" - why not use an annotation?
A package called "util" is a code smell for me.
UrlUtils only provides two methods for URL encoding so why not name it appropriately?
Now, your assertion about scaling to more engineers may be true but I’d argue tooling is the main reason for this because Java certainly doesn’t enforce any particular style of programming.
On a more serious note, most Java code on Github is now written by Copilot.
It makes sense in large applications with experienced developers, but it sucks to learn.
Even with the relative magic of things like init functions that execute on import, there's so much less magic that you have to consider.
IMO it's the easiest to jump into for this reason. Yes, the code itself might be excessive, but reading Go is as simple as can be.
What gets me about go is 2 things.
Its touted as being so easy that a junior dev can pick it up easily; it's targeted towards average developers. Fine, this is a virtue; when Java said the same thing, it's a horrifying blight.
The other is that people seem to mistake motion for action. "The code flies from my fingers" is one way I've heard it said. And, sure, you have the same error catching boilerplate (which again, in go is a virtue but other languages seems to be considered a sin) 1000x, as if the clicky-clicky of the keyboard is some measure of productivity.
It's got a big "blub paradox" thing going, to my eyes. I don't find it elegant at all, or maybe I mean not very expressive.
Taken ad absurdum, I have a language that only adds 1 to a variable. I can understand that language in milliseconds. But I can't do much with it. Maybe go hits a good balance, I dunno, but I found it quite tedious.
BASIC -> VB -> Java -> Python for some time -> Go
How you feel about Go is how I feel about JavaScript
Want to make a function or variable or field public? Just capitalize it. That’s it.
Error handling? Check if the error isn’t nil and handle it, otherwise move on. No need for silly things like try except.
And there are no magic functions because everything is explicit.
Go is just simple and pleasant to use.
At my employer we use a subset of C++ and we use Go. We have a process by which when you write code others have to review it and you need to have a review by someone who is an "expert" [0] in the language [1]. You become an "expert" by being ordained by a shadowy committee who reviews code you submit to the code repository. They look for knowledge of the core language, how this language is used within the company, the external and internal libraries, performance, testing, etc. There are many factors and if you demonstrate all of them you get marked as being this type of "expert."
Before I joined this company I had written code which was launched into production in the following languages: Java, PHP, C, C++, assembly (arm + x86), Python, JavaScript (browser + node), and a few more. I would consider myself about average in all of these. I did not write any golang.
After I joined my current job I obtained this "expert" bit for both C++ and Go. It took ~1.5 years to get the C++. It took ~3-5 months for Go.
You can actually see the first golang code I wrote (this was at home when I was experimenting with the language before convincing some team members we should rewrite some of our infra in go): https://github.com/gravypod/gitfs
It has all sorts of mistakes but I didn't read any guides. I just used https://cs.opensource.google to search for examples of things that I thought I would need to do (ex: "how do I do a switch statement").
For C++ I had worked on ~2 projects that used it. My first few PRs were very rough and I had a lot of performance issues that crept into my code. If I didn't already have a lot of C experience I would have also had a bunch of pointer/lifetime stuff the reviewers would have found (I know this from reviewing new team members first lines of C or C++).
I know that to some C++ represents a literal worst case but most people coming from C++ to Go will think it's amazing because it removes all of the common footguns.
> Things like awkward error handling
Yes, it is very clunky and annoying but it's very simple. It's just a normal if statement and doesn't use any special language rules. I think this would still be better if there was dedicated syntax for it (something like throw/try/catch which works the same way but gives it a different syntax) but honestly I don't think it's as bad as it's made out to be. It's basically a "less bad" errno and that worked go-... dece-... ehm fine for many years.
> dealing with null
I've never really had to think about this too much. There are some times it is important but I rarely return nullable things without errors and it hasn't bitten me yet. My code is not provably correct but for the things I'm working on I don't need that guarantee. If I did I'd probably switch to Rust or something with better type safety.
> capitalization for public/private
Yea, this sucks but it doesn't really get in my way much. I don't like it but it isn't actively hurting my usage.
> magic functions
Like `String()`? I don't know if that's the worst think in the world. Python, C++, and Java have things like this.
> and imports
It's not been too bad for me. What I think is unfortunate is that the package of something is unconnected to where it is in most go usages which is annoying but it makes things more terse. This does actually hamper my productivity.
> I don't think it's good or easy to use at all, but people rave about it.
It's pretty easy to use because all of the libraries I've seen share common interfaces for behavior. This makes things feel a lot more cohesive. fuse-go and billy was very easy to use in gitfs because of this.
[0] - This is not an expert as in "knows everything" but more like "has been seen to consistently write efficient, simple to understand, idiomatic code." It basically means that when someone else from this subset of SWEs reviews your code they often do not have many comments. Again, I want to stress, this is not expert as in "knows everything" just as in "good enough".
[1] - https://abseil.io/resources/swe-book/html/ch03.html#what_is_...
Very satisfying to be able to say "nope, it's fine, here's the source".
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):-
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.
Courtesy bitsavers.org (http://www.bitsavers.org/pdf/dec/pdp11/rt11/v5.6_Aug91/AA-PD...)R - sf package is a clean example of functional OOP
Python - pytudes (Peter Norvig’s notebooks)
Haskell - Elm compiler. I could mostly understand what’s going on even though I barely know any Haskell.
Ruby - Sequel is really nice.
Rust - Ripgrep
Pretty much any F# codebase is super readable too.
Funny because I detest it because of how difficult it makes it to dig into and customise spatial data and visualisations at a low level like I am used to with the spatial packages it sort of supercedes
1. https://www.cs.cmu.edu/~quake/triangle.html
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.
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.
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).
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.
```rb
# hashes
{ foo: 'bar' }.to_json
# numbers
28.to_json
# anything really
['hello', 42, { lorem: "ipsum" }].to_json
```You... don't. Python has a unified DB api (DBAPI 2.0) which drivers usually support. They often also provide a lower-level API specific to the DB.
I ended up going deep on the D programming language for my "new language". As a statically typed language it's not as expressive as the Python/Ruby/Perl contingent, but it's a lot more expressive than most static languages, and its C-like syntax "clicked" with me in a way Ruby never did.
It also can approach C-like run-time performance and memory footprint at times, which I appreciate. As much as I like developing in Python and Perl, I frequently wish they had better run-time performance and a smaller memory footprint. D gives me that, at the cost of a little expressiveness.
I usually end up always wrapping the json_decode in an eval to catch the error and handle it in an easier to understand fashion.
But I agree though, its nice how json and perl data objects pretty much map to each other. It's great.
> 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.
Sounds kinda backwards to me. I thought the fact that throwing exceptions (or "making a fuss," if you will) is better than silently producing incorrect (or "kinda lossy," if you will) results wasn't controversial anymore in 2023.
Go has this too, very handy:
https://github.com/solvespace/solvespace/blob/master/src/srf...
There is a lot more in other files - triangulation, booleans, creation - but the core math functions are there in very readable form.
https://news.ycombinator.com/item?id=33413124
https://news.ycombinator.com/item?id=32384769
https://news.ycombinator.com/item?id=25355171
https://news.ycombinator.com/item?id=15663980
https://news.ycombinator.com/item?id=18037613
https://news.ycombinator.com/item?id=14836013
https://news.ycombinator.com/item?id=34205294
https://news.ycombinator.com/item?id=13624926 (2017; 98 points; 67 comments)
I feel sure I'm missing a lot. This subject seems to come up every few months or so.
Pretty much anything else from Edi Weitz is also great.
Mezzano is quite elegant in my opinion, especially for an operating system. For example, this is the USB mass storage driver: https://github.com/froggey/Mezzano/blob/master/drivers/usb/m...
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.
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.
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.
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.S. It's now easy to check a repo file by replace .com with .dev: https://github.dev/desktop/desktop
Quick demo:
https://www.youtube.com/watch?v=i0CwhEDAXB0
Repo:
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. https://sourceforge.net/p/ftgl/code/HEAD/tree/trunk/
Perl
The Mojolicious / Mojo toolkit. Great minimalist API and great documentation and clean code all around. https://metacpan.org/release/SRI/Mojolicious-9.33/view/lib/M...
https://www.jsoftware.com/ioj/iojATW.htm
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.
I guess the Doom 3 source code is an elegant codebase. Surprisingly, they used C++ features quite sparsely.
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.
And then, the opposite is Monix (https://monix.io/) 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.
As someone who has written many small apps in PHP, I might dive into it and finally see what that Laravel thing is all about.
I also really appreciate FakerPHP for how easy it is to generate realistic fake values.
C
* Lua https://www.lua.org/source/
* Redis https://github.com/redis/redis (mentioned twice)
* Ruby https://github.com/ruby/ruby
* SQLite https://sqlite.org/src/dir?ci=trunk
C++
* Botan https://github.com/randombit/botan
* ClickHouse https://github.com/ClickHouse/ClickHouse
Go
* Go's standard library https://cs.opensource.google/go/go
* HashiCorp repos, particular those by Mitchell https://github.com/hashicorp
F#
* jet.com repos https://github.com/jet?language=f%23
Haskell
* Elm compiler https://github.com/elm/compiler
Lisp
* CL-PPCRE https://github.com/edicl/cl-ppcre/
* Mezzano USB driver https://github.com/froggey/Mezzano/blob/master/drivers/usb/m...
PHP
* Carbon https://github.com/briannesbitt/Carbon
* Laravel https://github.com/laravel/laravel
Python
* pytudes (notebooks) https://github.com/norvig/pytudes
R
* sf (ex of functional OOP) https://github.com/r-spatial/sf
Ruby
* Sequel https://github.com/jeremyevans/sequel/
* Sidekiq https://github.com/sidekiq/sidekiq
Rust
* ripgrep https://github.com/BurntSushi/ripgrep
Scala
* fs2 (ex of good type safety) https://github.com/typelevel/fs2
* monix (ex of ugly code with great performance) https://github.com/monix/monix
TypeScript
* GitHub Desktop (ex of SPA) https://github.com/desktop/desktop
---
thanks to: cejast, bb86754, wewxjfq, antonyt, bit, diego_moita, agjmills, freedomben, Contortion, valenterry, DethNinja, mberning, seneca
Golang Stdlib -> Golang
redis -> C
anything by armin ronacher -> Python
Oddly, even though he wrote one of the two most popular frameworks for Python, Wikipedia still claims his entry doesn't meet notability requirements.
https://github.com/bitwarden/server
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.
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.
Everything is nicely documented and pretty easy to read.
It's a great companion if you want to learn more about how languages are implemented.
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.
2. The TeX sources. Knuths literate programming seems like such a missed opportunity.
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.
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.
https://github.com/hanabi1224/Programming-Language-Benchmark...
CONST TRUE = -1
CONST FALSE = NOT TRUE
https://web.archive.org/web/19990202113028/http://hem.passag...C - Redis
Full DDD, it was a refactor during COVID-19 for my ecommerce.
It powers https://belgianbrewed.com
Copy paste from: https://news.ycombinator.com/item?id=35257225
> 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 :) :
- https://snipboard.io/D03VWg.jpg - General overview of the architecture. Small fyi: Connectors => Autogenerated nugets to call the api's
- https://snipboard.io/9M24hB.jpg - Sample of Modules + Presentation layer
- https://snipboard.io/ybp6EH.jpg - Example of Specifications related to catalog ( = products )
- https://snipboard.io/lE9vcK.jpg - 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.
- https://snipboard.io/7rVBpk.jpg - . In general, i have 2 List methods ( one for Paged queries and one not for Paged queries)
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.
The enduser frontend supports a hierarchy of themes ( so it's insanely flexible to create variations of pages for other clients).
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 :)
Edit: my specification implementation has been open-sourced here: https://github.com/NicoJuicy/Specification-Pattern-CSharp
Someone was curious to see it.
Now lets see Paul Allen's screenshots of the directory structure of his codebase.
Ok, i get it.
Just a shame that while i gave a pretty clear technical description on the "why i think so".
Nobody of the downvoters actually cared to give a actual technical counter argument or even did a test with a technical question... Instead just a reply with a partial movie script...
Ugh.
- "Oh, really? Who wrote it?"
"I did."
..."Lessons in humility"