by KennyFromIT on 9/10/22, 6:57 PM with 261 comments
Please provide a link, if possible.
by davidst on 9/11/22, 3:20 AM
Tanj Bennett created our original 8087 floating point emulator. It was real mode, 16-bit code, that kept an 80-bit float in five 16-bit CPU registers as a working value during its internal math calculations. If you ever coded for the PC you will appreciate just how precious register space was on the 8086/8088.
It's been a few decades and my memory is fuzzy but I don't recall any critical places where the registers had to be saved and restored. He choose math algorithms and code paths specifically to keep all five live at all times. Tanj's code flowed with an economy of motion that brought to mind the artistic performance of a professional dance troupe. I did not have the math skill and could not have ever created it as he had. It brought me genuine pleasure just to read his code.
Eventually, it came time to port it to 32-bits (real and protected modes.) I wrote new exception handlers and replaced the instruction-decode logic with a nice table-driven scheme but did not touch the core math library. It still ran in 16-bits under the hood exactly as Tanj designed it.
Tanj, if you happen to see this, I have fond memories of your emulator. It was beautiful.
by dhosek on 9/10/22, 8:43 PM
Against all that, Knuth wrote code that could be ported to other systems (I first used TeX on an IBM mainframe running VM/CMS, then later on a VAX minicomputer running VMS, then on a PC with DOS, PC with OS/2, PC with Windows and now a Mac with OS X/MacOS. His presentation of code and internal documentation intermingled is something which I could see still being useful now, although sadly, literate programming seems all but dead now. I still go back to Knuth’s code to understand how to solve some problems on occasion even though it’s about 30 years since I last wrote a line of Pascal.
by vkazanov on 9/10/22, 7:43 PM
I also remember how Qt UI code and docs were a revelation after the nightmare of win32 and related frameworks.
Bellard's original Tinycc was very entertaining and took a while to digest.
Most old and well-maintained projects are worth learning from.
Apart from some local tips and tricks what is more important is understanding that there's nothing special about other people's code.
by juancn on 9/10/22, 7:43 PM
I mean, it has a lot of essential complexity but little accidental complexity.
That’s usually what I strive for when coding. Complexity is sometimes unavoidable, that’s fine, that’s why it’s essential. However, avoidable complexity should be… well… avoided.
by db48x on 9/10/22, 9:15 PM
https://github.com/mockingbirdnest/Principia/blob/master/ast... is a decent example of both:
// We compute the mean rate (slope) of the mean anomaly M(t), the mean
// argument of latitude u(t), and the longitude of the ascending node Ω(t).
…
Product<Angle, Square<Time>> ʃ_Mt_dt;
Product<Angle, Square<Time>> ʃ_ut_dt;
Product<Angle, Square<Time>> ʃ_Ωt_dt;
…
ʃ_Mt_dt += (Mt + previous_Mt) / 2 * dt;
ʃ_ut_dt += (ut + previous_ut) / 2 * dt;
ʃ_Ωt_dt += (Ωt + previous_Ωt) / 2 * dt;
…
anomalistic_period_ = 2 * π * Radian * Δt³ / (12 * ʃ_Mt_dt);
nodal_period_ = 2 * π * Radian * Δt³ / (12 * ʃ_ut_dt);
nodal_precession_ = 12 * ʃ_Ωt_dt / Δt³;
The advantages are concise notation plus compile–time guarantees that the units work out. I don’t care much for C# in general (or C++ for that matter), but I like the results here.Sounding out the Greek names occasionally used as release names is always fun too.
by throwawayacc2 on 9/10/22, 8:21 PM
I used to work for a large newspaper. Engineering wasn’t the focus of the business, it was only a means to an end, it was just something they needed to have a competitive website. As a result, the churn rate was very high. But more interesting there was also a very high return rate. Engineers would come and go and return and leave again all the time.
As a result, the code base was a path work of various engineers with various skill level, different directions by different heads of engineering, repurposed old projects, legacy code and last minute additions by urgent request from the editors, among others.
The part I was most familiar with was what I can only describe as a sort of next.js but unlike it, it wasn’t planned or designed but rather it sort of grew organically over the years.
The fascinating thing to me was precisely this phenomenon. Some projects have a clear design and purpose and are built so from the start. Sort of like a building or a mechanical clock.
Others just evolve over time, they change, mutate, evolve, incorporate other bits. More like a biological organism or perhaps nature taking back a derelict settlement.
At first as you can imagine it was difficult to wrap my head around it. But in time, I started to see the beauty in it. It had historical bits. There was code written in 2010 that ran in 2020. Others you could tell little habits of the writer. Not everything gets stamped out by the linter. There was this guy who wrote “class ClassName extends React[“Component”]” - I have no idea why but I would run into code written by him and immediately recognise, ah yes, that’s that guy.
It’s certainly not an example of a good code base, but to me was interesting being able to see the code as a living organism with a history and fingerprints of it’s creators rather than a well designed machine.
by pandler on 9/10/22, 10:27 PM
When I was still learning to code, I spent hours and hours and hours poking around the Django source code. In particular I was fascinated by the metaprogramming used in the Model and Query objects, and I ended up learning a ton about how Python works under the hood.
by jeroenhd on 9/10/22, 10:32 PM
Not only is the code itself structured much more pleasantly than I ever suspected possible in C++, huge parts of it were also recorded while they were being written (see https://www.youtube.com/c/AndreasKling) so you can see and hear the process that led to the final product.
Some of the code is quite gnarly, which is to be expected from a repo containing an entire operating system, containing everything from the kernel to a bespoke web browser.
However, as SerenityOS isn't trying to be a UNIX clone, its C++ oriented APIs are a nice breath of fresh air compared to the barebones C that Linux and friends use.
by zonetti on 9/10/22, 7:39 PM
by CraigJPerry on 9/10/22, 9:08 PM
Another one is the Postfix codebase by Wietse Venema. It was notable because it's basically had the square root of 0 vulnerabilities despite being written in C and being one of, if not the most popularly deployed MTAs in the world (so basically a constant target for hack attempts - contemporary products like Exchange were basically a laughing stock for vulns in that time period). Anyway, the architecture of that codebase is bordering on beautiful. It's a total goldmine.
Bonus - a popular code base that made my eyes bleed, nginx. I think it's basically been re-written today but the earliest versions of it were horrible to read. It was fast back then but it was like some sick twisted joke of how to write code. This is not to take away from whoever created it, they still created a monumental shockwave when they released nginx, it was far more performant that anything else.
by Nican on 9/10/22, 9:07 PM
https://github.com/ValveSoftware/source-sdk-2013
It is not that the quality of the code is high, but just that it is well organized, and everything seems like it was written by a beginner. That makes it wonderfully easy to read and follow the logic.
Since have played around with the Source Engine, I follow the KISS principle with coding with high priority. Rarely trying to be clever, or try to over-do abstractions.
by mattarm on 9/11/22, 2:01 AM
by johndoe0815 on 9/10/22, 7:10 PM
(see also the "Virtual Machines" book by Smith and Nair, ISBN-13 978-1558609105, if you are interested in this topic).
I can also recommend John Lions' "Commentary on the Sixth Edition Unix Operating System" - https://warsus.github.io/lions-/, Douglas Comer's Xinu (https://xinu.cs.purdue.edu) as well as Niklaus Wirth's "Compiler Construction" (https://people.inf.ethz.ch/wirth/CompilerConstruction/Compil...) and Project Oberon (http://www.projectoberon.com).
by inDigiNeous on 9/10/22, 7:34 PM
Can't provide source though on that one, as it's a propietary engine. Recently I've enjoyed reading the source code to Sokol, lot's of really good decisions there and I love the minimal C -style structure:
by prezjordan on 9/10/22, 8:03 PM
It feels like the logical end state of "clever" code, for better or for worse. Or, alternatively, what happens when a standard library is gigantic but each keyword is 1-2 characters.
by throwaway892238 on 9/10/22, 8:58 PM
Busybox and uClibc are great examples of efficient C code. I submitted a feature, and they basically rewrote it to be less crap, and that taught me a ton.
Hard to know what good Python code is. Everyone seems to use some framework that fundamentally changes how you write apps. "best source code" is probably just what's best for that specific kind of work.
by e12e on 9/10/22, 11:08 PM
by btilly on 9/10/22, 9:20 PM
by choletentent on 9/10/22, 8:02 PM
by michaelwww on 9/11/22, 12:12 AM
https://github.com/microsoft/TypeScript/blob/main/src/compil...
by scrame on 9/10/22, 11:18 PM
by ChrisMarshallNY on 9/11/22, 12:32 AM
They have done a lot of work on structure and documentation, since the docs are auto-generated.
I have taken a lot of inspiration from them.
The Adobe APIs[2] were also excellent, but I'm not sure they are open for browsing, anymore.
[0] https://github.com/apple/swift
by Cieplak on 9/10/22, 8:13 PM
by mav88 on 9/10/22, 10:33 PM
by eternityforest on 9/11/22, 8:35 AM
There are programs and programmers that impress me, and once in a while I'll come across an interesting algorithm, but I can't really think of any code that is anything more than just code.
It's either too simple to do anything I'm interested in, too full of ugly hacks like most large programs, or deals with math and algorithms way above my understanding, to the point where I probably haven't even tried to read it.
Some of most memorable bits or code for me are the examples for frameworks and libraries, but those are more impressive for the code that isn't there, rather than the code that is.
I love declarative work(Not so much pure functional, but things like CSS and CAD constraints where a solver figures out how to make your intent happen) but again, that's notable for the code that isn't there.
I also really like hardware workarounds. It's so cool to be able to have software that runs fine on bad hardware. It's not that common, but occasionally you can pull it off in a reliable way that really is just as effective as using better hardware. Seeing pure code replace hardware is kind of like a further development of replacing mechanical stuff with solid state chips.
I really just don't care about code itself very much. I don't want to write bad code, but I only care about good code in as much as it will actually benefit the project. Once it's good enough, features or architecture improvements or unit tests or something might be more important, and interesting or clever code might bring the project down.
If I'm reading your code, I'm probably either adding to it and complaining that it doesn't have an API hook already there, or fixing a bug.
If I'm using but not reading your code I'm probably happily assuming it's great, if it's a popular trusted thing.
by lioeters on 9/10/22, 10:16 PM
Hand-written compiler for WebAssembly Text format to binary - https://raw.githubusercontent.com/stagas/wat-compiler/main/s...
Gameboy emulator implemented in C, that also runs in the browser - https://github.com/binji/binjgb
QuickJS Javascript Engine - https://github.com/bellard/quickjs
The TypeScript language and compiler - https://github.com/microsoft/TypeScript
by srcreigh on 9/11/22, 12:59 AM
https://docs.racket-lang.org/generator/index.html
Can't find source code online. I browsed the code in emacs with racket-xp-mode go to definition after installing it via raco pkg install. Could probably use DrRacket instead of emacs/racket-xp-mode.
I had to read the source code to find out what the specific names of the proto creators, accessor, mutator functions would be.
by pamoroso on 9/10/22, 7:23 PM
by dchest on 9/10/22, 8:29 PM
by fisherjeff on 9/10/22, 10:24 PM
by jraph on 9/10/22, 9:13 PM
Quite simple and straightforward, and very modular. For a browser.
by jmmv on 9/11/22, 3:04 AM
The kernel abstractions that allow for portability make the code quite easy to read. Back in the day, I was able to write a file system from scratch by just reading the code of other file systems. The build system is also pretty amazing (allowing you to cross build the whole OS for any hardware target from many other OSes) and not too hard to follow. The source tree is structured in a way that makes a lot of sense (IMHO better than FreeBSD).
by mcculley on 9/10/22, 10:44 PM
by buescher on 9/10/22, 11:29 PM
The best - it compiled to the sane hex file.
by atilimcetin on 9/10/22, 11:43 PM
by halftrolling on 9/10/22, 8:27 PM
by carpenecopinum on 9/11/22, 7:46 AM
by eliseomartelli on 9/11/22, 10:00 AM
by sowbug on 9/11/22, 3:38 AM
Similar with Notch's Left4KDead, which implemented a fun zombie game for a Java small-code competition. A mirror of the original source is here (https://github.com/codingcampbell/Left-4k-Dead). I rewrote it in JavaScript as a Chrome App, in the process refactoring for readability (and sacrificing some of the code's beauty). https://github.com/sowbug/ChromeLeft4kDead
by orthoxerox on 9/10/22, 8:37 PM
by omn1 on 9/10/22, 8:04 PM
by ilrwbwrkhv on 9/11/22, 1:47 AM
by captdecibel on 9/11/22, 4:56 AM
by muskmusk on 9/11/22, 6:15 AM
A mistake i see many engineers commit is obsess over their code and how "pretty" it is. What they seem to forget is that code is only read when it isn't doing what it's supposed to do. Granted when you do have to read it there are good and bad examples, but the question you should ask yourself is not "how can I make this code easier to read?" But "how can I make sure noone has to read this code". The code that performs this job is not always pretty. If it is impossible to make the code bulletproof then the best bet is usually to just make it really simple. Good code is nonexistent and if it has to exist it is boring.
by smrtinsert on 9/10/22, 7:46 PM
by thefz on 9/10/22, 7:36 PM
by shp0ngle on 9/10/22, 9:48 PM
every time I am not fully sure what some function from golang std library does, I just click away to its source code and it’s quite clear
by drewg123 on 9/10/22, 8:36 PM
The BSD sources were sanely laid out, documented and had a beautiful coding style that made the code easy to read. Free- and NetBSD even had version control.
by elcritch on 9/11/22, 7:34 PM
The libVF.io iommu setup code is a good example and inspires me to think that system-glue code doesn't need to be gross or impenetrable: https://github.com/Arc-Compute/LibVF.IO/blob/master/src/libv...
Another one I've appreciated reading (and learned more about 2d graphics from) is Pixie, a 2d graphics library written in Nim. It's strongly affected how I write similar code.
Here's the implementation of a fair subset of SVG paths: https://github.com/treeform/pixie/blob/master/src/pixie/path...
And one last one for basic algorithms which I'd use to teach intro to data structures any day: https://github.com/nim-lang/Nim/blob/version-1-6/lib/pure/al...
Of course Knuth's original code is still some of the best classic code. K&R's original C book is a classic.
(edited formatting and tweak descriptions)
by zmmmmm on 9/11/22, 10:14 AM
[0] https://github.com/postgres/postgres/blob/master/src/backend...
by vedranm on 9/11/22, 6:27 AM
Example (config file, but the entire codebase is like that): https://imgur.com/UnIUZmZ
by jupp0r on 9/11/22, 3:30 AM
by danielmarkbruce on 9/11/22, 1:03 AM
by klabb3 on 9/10/22, 8:16 PM
by sibeliuss on 9/11/22, 6:18 AM
Blew my mind reading through it, honestly. Just perfect.
by jmconfuzeus on 9/10/22, 9:22 PM
I read it before learning Ruby but surprisingly, the code made sense even though I didn't know the language.
by reacharavindh on 9/10/22, 7:35 PM
by cammikebrown on 9/10/22, 7:45 PM
by chess_buster on 9/10/22, 8:32 PM
by agumonkey on 9/10/22, 10:17 PM
bsd code is clean
minikanren
some old lisp (le lisp or eulisp I forgot)
by caviv on 9/18/22, 7:39 PM
Yes, you are right. It is empty. The best code there is - is the code you did not had to write. It have no bugs, no latency. I am not being sarcastic here. I truly think that less code is better.
by mccorrinall on 9/10/22, 8:02 PM
by tmtvl on 9/11/22, 9:19 AM
by elromulous on 9/10/22, 9:41 PM
by fiatjaf on 9/10/22, 10:41 PM
by mrcrowl on 9/12/22, 6:01 PM
Beautiful c code. As a 30+ year programmer, one new thing really stuck with me. He wrote all comments as sentences: first letter capital, finish with a “.”.
by seogasparini on 9/16/22, 3:34 PM
by mrcartmeneses on 9/11/22, 11:12 AM
It’s a very simple code-base in many ways. At the time it was 10k lines of mostly short pure functions.
The code examples were also really nice and explained a lot about how to work with SVG.
I owe some Thanks to Mike Bostok and his fellow developers.
by bfung on 9/10/22, 9:04 PM
Then trying and looking at https://github.com/geohot/tinygrad which can implement SD, it’s really well written and ideas well organized, concise, and it works on multiple platforms well.
by philippekahn on 9/18/22, 11:56 AM
That compiler served as the blueprint for Borland tools.
philippe@fullpower.com
by userbinator on 9/10/22, 11:45 PM
C4 and its follow-up C4x86:
by Teknoman117 on 9/10/22, 9:15 PM
I always found much of the FFmpeg API very unintuitive and much of what I now know about it I learned from reading through mpv. Hardware accelerated encode/decode, etc.
by ranger_danger on 9/10/22, 9:34 PM
by skywal_l on 9/11/22, 9:33 AM
It's a good way to document old hardware with emulation code.
by r3trohack3r on 9/10/22, 8:37 PM
by sonnhy on 9/12/22, 12:05 PM
by dataflow on 9/10/22, 8:41 PM
by xkriva11 on 9/11/22, 10:26 AM
by felixschl on 9/11/22, 12:24 AM
by aestetix on 9/10/22, 8:10 PM
by Lukas1994 on 9/12/22, 9:40 PM
by okr on 9/11/22, 3:48 PM
by 10g1k on 9/11/22, 11:34 PM
by andreskytt on 9/11/22, 6:13 AM
by MeyerK on 9/10/22, 9:28 PM
by m33k44 on 9/11/22, 12:55 PM
Wt[0] source code is also good.
by valdect on 9/10/22, 9:02 PM
by sgtnoodle on 9/11/22, 2:05 AM
by roeles on 9/11/22, 11:39 AM
by monster_group on 9/10/22, 8:20 PM
by decoy98 on 9/10/22, 7:50 PM
by hestefisk on 9/11/22, 5:44 AM
by Inviz on 9/10/22, 7:45 PM
by ramesh31 on 9/10/22, 11:19 PM
by revskill on 9/10/22, 7:49 PM
by adhoc32 on 9/10/22, 11:15 PM
by techsin101 on 9/10/22, 11:21 PM
by baggiponte on 9/10/22, 11:23 PM
by sakesun on 9/11/22, 11:08 AM
by rootw0rm on 9/11/22, 5:14 AM
by Razengan on 9/11/22, 1:34 AM
by badinsie on 9/10/22, 9:00 PM
by b20000 on 9/13/22, 4:43 AM
by rabuse on 9/11/22, 12:33 AM