HACKER Q&A
📣 andrewstuart

What's a small example of some of the best code ever written?


I want to be able to hold some smallish number of lines of code and say "it is generally agreed that this code is amongst the best examples of programming that there is".

Does anyone have any suggestions for such examples? Any language is fine. As long as there's some way to say "look, here is where people say this is great code".


  👤 ozten Accepted Answer ✓
Alan Kay has famously cited the half page of code on page 13 of the Lisp 1.5 manual as the "Maxwell's Equations of Software"

https://www.gnu.org/software/mes/manual/html_node/LISP-as-Ma...


👤 mmillin
One of my favorites is Peter Norvig's spelling corrector in less than 30 lines of python

https://norvig.com/spell-correct.html


👤 belltaco
"Best" is relative, but here's an example of "clever" code that actually shipped in a very popular game, it's just 8 lines.

https://betterexplained.com/articles/understanding-quakes-fa...

Also a lot of 4k demo scene have some great looking videos rendered purely by a small amount of code.

https://en.wikipedia.org/wiki/Demoscene

https://www.youtube.com/watch?v=W58r7oycUrA


👤 abrudz
From http://archive.vector.org.uk/art10500390

In the year 2033 Earth was discovered by Survey Fleet MCXII of the Galactic Empire. The Emperor ordered Earth to send a representative to the court, with strict instructions to “bring something beautiful”.

Emperor: What beautiful things does Earth have?

Earth Representative: Your excellency, our mathematician Euler proved in our year 1737 that

  +/(1+⍳∞)*-s ←→ ×/÷1-(⍭⍳∞)*-s
Emperor: What is the ⍭ symbol?

Earth Rep.: ⍭i is the i-th prime.

Emperor: And what is ∞? Does it do anything useful?

Earth Rep.: It denotes infinity, your excellency.

Emperor: (ponders equation for a minute or two) Respect!

Emperor: Neat notation you have there. Tell me more.

Earth Rep.: Your excellency, it’s called APL. It was invented by the Canadian Kenneth E. Iverson…


👤 hyperman1
I'd say: Hello world. And not as a joke, as someone below dud.

The C 'Hello, world!' program documented the style of a whole programming language in 5 lines of code. Every computer engineer who sees it can now recognize C code. Even non -programmers understand vaguely what's going on.

Just about every programming language today starts with the same example. That's how wildly succesfull it became as demo.


👤 seanhunter
I remember being truly shocked when I first read the source code to Dan Bernstein's qmail and djbdns. I had been programming in C forever at that point, but it was so different to anything I had seen before it was a real paradigm shift. Extremely idiosyncratic, but rich with ideas for ways to make software more reliable and secure. For example, because old-school C strings are the source of so many bugs, he created his own string library (stralloc[1]) which was incredibly straightforward to understand and use and avoided all the many many footguns that C strings are prone to.

[1] https://cr.yp.to/lib/stralloc.html


👤 badrabbit
I am not a programmer by profession but some of the data structure code in Linux I really liked and thought it was the best implementation I have seeen: https://github.com/torvalds/linux/blob/master/include/linux/...

👤 suprjami
musl libc - https://musl.libc.org/

This isn't just a small implementation of the C standard library, it's an absolute joy to read and usually every easy to understand. It takes real talent to write code which is both this legible and efficient.

Often when I'm trying to understand what glibc is doing I'll read the musl version of a function and that will give me an indication of what the glibc source is trying to communicate.


👤 MikeOfAu
The InterViews library, written by John Vlissides, was for writing GUIs in C++ under X11. It came out in the early 90s. (Yes, I'm an ancient programmer)

He was one of the Gang of Four who wrote Design Patterns, but he wrote this library years before the book came out. It always seemed to me that he invented all the main patterns in the Interviews library before they were subsequently (nicely!) documented in the book (which went on to become a smash hit).

I've since realised that I much prefer functional programming (Clojure!) over OO, BUT, oh my goodness, what a truly groundbreaking and beautiful bit of work it was for its time. I read every line of code in that library, and I became a much better programmer for it.


👤 hoofhearted
I believe this is an early proof of Stripe.

You could charge a credit card with just 7 lines of code.

https://github.com/benweissmann/dev-payments


👤 bitwidget
print("Hello World") /s

But I've heard good things about the Fast Inverse Square Root Calculation algorithm in the Doom 3 source code. https://www.youtube.com/watch?v=p8u_k2LIZyo


👤 jfghi
I like sqlite although technically there’s quite a bit of code there. But overall it seems pretty straightforward to determine what does what and why.

👤 akkartik
I kinda dislike the premise of your question, but https://www.amazon.com/Beautiful-Code-Leading-Programmers-Pr... has some good candidates. Chapter 1 for example is a regular expression matcher that appeared on HN recently: https://news.ycombinator.com/item?id=32589311

👤 nuc1e0n
This is probably not a popular opinion, it is now wholly unnecessary and was often abused but I would say jQuery's source code. It took a language previously considered as a toy with different implementions that were inconsistant and unwieldy and made something immensely powerful and consistent with it. It brought lisp style functional programming to many who had never seen it before. Within a matter of months everyone working on the web was using it.

👤 sslayer
The best code is code that is never written.

👤 verdverm
We have a small bash script to retry commands if they fail, mainly used in CI. It may not fit the typical version of "best" but it saves uncountable numbers of hours and frustration when builds fail because 3rd party services return flakey errors.

👤 bmitc
Honestly, I've never seen a codebase that has blown me away (including my own). The GLFW project is very well documented for users of the API, and the code looks pretty clean. GLFW has impressed me because it makes something very complex just work and the documentation is really top notch. When I've peaked inside the F# and Elixir codebases (the codebases of the language itself), things look pretty good.

👤 gaze
It's a simple one but freebsd's is short, well designed, and really useful.

👤 accountinhn
For me the EICAR Antimalware test file, simple 68bytes, does something elegant. https://www.eicar.org/download-anti-malware-testfile/

👤 ncmncm
Anything by Justine (justine.lol) will be a serious contender for the title.


👤 theflyingelvis
10 print “hello world”