15 ms·
Ask HN: What code samples should programmers read?
What are examples of code that ever programmer should have read and analysed because they show a particularly deep insight into the problem, a creative attempt at a solution or otherwise outstanding proficiency?
Goal is to learn something from excellent pieces of code, much like it is practised in art by studying Old Masters, pieces of literature or musical compositions.
PS: I admit to exaggeration in the headline / first paragraph ("should have") btw ;)
- db48x 9y agoA metacircular interpreter for Scheme. You can even watch a lecture where this is presented (https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-001-structure-and-interpretation-of-computer-programs-spring-2005/video-lectures/7a-metacircular-evaluator-part-1; https://ocw.mit.edu/courses/electrical-engineering-and-compu... see also part 2).
- _Microft 9y agoThanks! By the way, there's a trailing semi-colon on the link that needs to be removed for it to work.
- majewsky 9y agoFixed link: https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-001-structure-and-interpretation-of-computer-programs-spring-2005/video-lectures/7a-metacircular-evaluator-part-1 https://ocw.mit.edu/courses/electrical-engineering-and-compu...
- db48x 9y agoThat's actually just a bug in the autoformatter; the url was simply embedded in a sentence with a semicolon in it.
- kybernetikos 9y agoThe book Beautiful Code has some examples along these lines with interesting commentary too.
- mpfundstein 9y agoffmpeg source code :-) its beautiful C code and you will learn how to build a maintainable, modular system with just the tools that C gives you.
- ktta 9y agoDoesn't ffmpeg have a reputation for bad code merge practices? I've heard libav[1], a fork of ffmpeg has much better focus on code quality. Note that ffmpeg frequently merges any new code from libav. So ffmpeg is almost a superset of the both. For anyone wondering why this is, here is a good explanation.[2] [1]:https://www.libav.org/ https://www.libav.org/ [2]:http://blog.pkh.me/p/13-the-ffmpeg-libav-situation.html http://blog.pkh.me/p/13-the-ffmpeg-libav-situation.html
- shezi 9y agoFollowing Handmade Hero is always a good idea. http://handmadehero.org/ http://handmadehero.org/
- malydok 9y agoWow, this is impressive. The dedication required is immense, over 380 episodes recorded already.
- Tyr42 9y agoYeah, there's a lot of them. I think the best parts so far (I'm at 300ish) was the dll hot reloading, the software pixel renderer, with SSE instructions, and some of the stuff around how the debug machinery works.
- elorm 9y agoCheck out 500 Lines or less https://github.com/aosabook/500lines https://github.com/aosabook/500lines I just can't recommend it enough. All the projects are open source, so you can review the source code and still be walked through the code by book. You'd learn why the programmers made certain trade offs and how the applications became better of for it
- mrleinad 9y agoIs it finished? I see lots of folders without a README, which leads me to think that it was abandoned.
- pakitan 9y agoHere is a direct link for those like me who are wondering where can they actually read the book: http://aosabook.org/en/index.html http://aosabook.org/en/index.html
- andai 9y ago500 lines or fewer ;)
- deleted 9y ago[deleted]
- waqf 9y ago500 lines or fewer lines; but 500 lines or less code.
- deleted 9y ago[deleted]
- songshu 9y agoYes! I often find myself making this point -- i.e. that the supposed problem indicates that the reader has made an incorrect assumption about an imaginary elision. Cf. "5 items or less [stuff]", "$5 or less [money]".
- daliwali 9y agoHere's an overlooked one: your dependencies. Every programmer should read the (presumably open source) code they depend on, but almost nobody does it. Some look at documentation, but not everyone even does that. What you may find is that some of the code you depend on is garbage, and may even motivate improvements. This may be less feasible for giant messes such as most front-end tooling and frameworks that exist today.
- pc86 9y agoGreat advice, especially if the improvements are made to the open source package(s) so that everyone can benefit. Proprietary forks/upgrades to open source packages has a much narrower impact.
- aczerepinski 9y agoI read my dependency code partially as a way to learn the language. Several times in the past year I've been assigned to a project writing a language I don't know, and the major dependencies have often been a great example of well written, idiomatic code in that language.
- ATsch 9y agoI can really recommend this. I've made a habit of just casually browsing through the source of dependencies and just opening up anything that sounds interesting.
- seanwilson 9y ago> Every programmer should read the (presumably open source) code they depend on, but almost nobody does it. Has this ever been beneficial? I generally just check if a library is popular and maintained and that's always been enough for me. If the code really is horrendous nobody is going to be using it and if you always read the hundred of thousands of lines of code you depend on you'd never get anything done.
- gwbas1c 9y agoHave you ever used a library and thought, "ugh, it should be written THIS way." When you review a dependency, you answer why it should not be written THIS way. (Or why it's junk and shouldn't be used.)
- jonsen 9y agoIEEE CODE OF CONDUCT [PDF]: http://www.ieee.org/about/ieee_code_of_conduct.pdf http://www.ieee.org/about/ieee_code_of_conduct.pdf ACM Code of Ethics and Professional Conduct: https://www.acm.org/about-acm/acm-code-of-ethics-and-professional-conduct https://www.acm.org/about-acm/acm-code-of-ethics-and-profess...
- fenomas 9y agoI'm not a C person, but I've often heard that the Quake source code is good, practical-not-necessarily-elegant code that's worth emulating. https://github.com/id-Software/Quake https://github.com/id-Software/Quake
- H4CK3RM4N 9y agoWasn't Quake the one where they did a really good approximation of he inverse square root because they needed it?
- dmurray 9y agoThat was Quake III https://stackoverflow.com/questions/1349542/john-carmacks-unusual-fast-inverse-square-root-quake-iii https://stackoverflow.com/questions/1349542/john-carmacks-un...
- kierenj 9y agoThe Quake source was a huge source of learning for me when I was getting started. Hacking OpenGL, making a red/blue stereoscopic mod, and discovering pools of particles quickly brought to life and rendered via a linked-list pool.. great fun and so valuable to stumble upon.
- kierenj 9y agoThe Quake source was a huge source of learning for me when I was getting started. Hacking OpenGL, making a red/blue stereoscopic mod, and discovering pools of particles quickly brought to life and rendered via a linked-list pool.. great fun and so valuable to stumble upon.
- _Microft 9y agoIt's impressive how the software renderer of Quake II manages to achieve the results as they have them with just a 256 color palette and zero repainting of pixels (i.e. every pixel is correctly painted from the beginning). http://fabiensanglard.net/quake2/quake2_software_renderer.php http://fabiensanglard.net/quake2/quake2_software_renderer.ph...
- 9y ago
- petercooper 9y agoThis doesn't entirely match the criteria in your question but I think dipping into the standard library for your language (if it has one) is a good idea. It's not only useful to see what you're using but it can also show you how language experts use the language (which can be useful stylistically) and be an archaelogical exercise in the history of the language and its priorities (particularly true with Ruby's stdlib, I found).
- real-hacker 9y agoAgree. I sometimes read Python standard lib for fun.
- EliRivers 9y agoFast inverse square root as seen in Quake (as mentioned in other comments) https://en.wikipedia.org/wiki/Fast_inverse_square_root#Overview_of_the_code https://en.wikipedia.org/wiki/Fast_inverse_square_root#Overv...
- hendry 9y agoplan9 sources or http://git.suckless.org/sbase/ http://git.suckless.org/sbase/
- pyroinferno 9y agoWhy you're at it: https://bitbucket.org/inferno-os/inferno-os/ https://bitbucket.org/inferno-os/inferno-os/
- happy-go-lucky 9y ago> Why you're at it: Not to sound pedantic, but did you mean "While you're at it:"?
- bjourne 9y agoTabs in C source code. Ew!
- franzwong 9y agoYou should read the code of a open source project that you often use. You might not have the "ah ha" moment when reading something unfamiliar.
- mtreis86 9y agoI am learning lisp and was recommended to read "Paradigms of AI Programming" as the examples are all given in lisp. The later book is "Artificial Intelligence A Modern Approach" which is written with pseudocode examples. The site includes other languages than lisp; python, java, js, scala, and c#. PAIP lisp code: http://www.norvig.com/paip/README.html http://www.norvig.com/paip/README.html AIAMA code: https://github.com/aimacode https://github.com/aimacode
- steverit 9y agohttp://norvig.com/spell-correct.html http://norvig.com/spell-correct.html
- golergka 9y agoFabien Sanglard wrote great reviews of various codebases, including git, Doom 3 and others. http://fabiensanglard.net/ http://fabiensanglard.net/
- notburnt 9y agoI remember having a few aha moments when reading through the Redis code.
- simon_acca 9y agoAll of the norvig Jupyter notebooks: http://norvig.com/ipython/README.html http://norvig.com/ipython/README.html
- mafribe 9y agoFor those who understand networking: the Mirage TCP/IP networking stack [1] in pure OCaml is a must. It's an object of extreme beauty, and possibly the most eloquent argument for types, types inference and algebraic data types I can think of. The TCP state machine is mostly specified at type level [2], preventing numerous potential bugs in one fell swoop. Reading this code is probably most enlightening if you have already written networking protocols. NB: this has nothing to do with OCaml, other comparable languages with ADTs (Scala, Rust, Haskell, F#) would be similarly suitable. [1] https://github.com/mirage/mirage-tcpip https://github.com/mirage/mirage-tcpip [2] https://github.com/mirage/mirage-tcpip/blob/master/lib/tcp/state.ml https://github.com/mirage/mirage-tcpip/blob/master/lib/tcp/s...
- deleted 9y ago[deleted]
- SomeStupidPoint 9y agoThanks! I've been looking for something beyond basic tutorial apps to get a real feel for OCaml.
- pthreads 9y agoThank you. I haven't written networking protocols, yet. But hopefully I can still benefit from these.
- int08h 9y agoOnly looked at link [2], but wow. That state machine is brilliant. Don't know OCaml but am learning Rust and I see what you mean about the universality of how types make this possible. Thanks for pointing this out.
- emodendroket 9y agoI don't think there is such a thing. Learning about common algorithms is a good idea but programs aren't novels. People pay a lot of lip service to reading code but practically nobody does it. http://www.gigamonkeys.com/code-reading/ http://www.gigamonkeys.com/code-reading/
- dmartinez 9y agoWow, this was a great read. I wholeheartedly agree that very few people read source at a deep level, but your link explains why. We should read source code sometimes (maybe) to deepen an understanding of how someone might think about the problem differently. In the link, Donald Knuth says he enjoyed the challenge of exploring a piece of software that had no documentation in order to figure it out. In light of this, my answer to "what code should people read" is that you should read (really, decode) the source code of libraries competing to solve the same problem. This would lead to the greatest yield in terms of uncovering core shared concepts and core unique concepts. You could compare Vue and React to understand how two different parties thought about componentizing the front-end. Or you could compare Django and Flask to see how a batteries-included MVC framework compares to a lighter alternative.
- travmatt 9y agoI disagree in part, I read lots of code. Typically if I don't know how to solve a problem / use a library, I'll go onto github and search out projects either by their dependencies or structures I know must be present in the code I want. Then I'll pick out 20-30 projects and compare and contrast. Funnily enough, I read most all of the source code to Etsy's 'Artsy' iPhone app because I wanted to get a sense of how a professional shop structured their iPhone code. As for the understanding the code, the author is very correct in that it's tough to implicitly understand what's happening in the code. But to that point I use Reveal to peek inside iPhone apps, or node-nightly + chrome devtools to walk through js code. Since my goal isn't to understand the whole of a program, rather parts I'm interested in, it's worked out quite well. I've always wanted to read code for very secure applications (like Signal or SecureDrop), but I fear I don't understand crypto/infosec enough to understand the context the code was written in.
- pyroinferno 9y agoI am surprised no one said Linus Torvald's double pointer problem
- pjc50 9y agoI don't think there's an "every", because not every programmer can read every language and there isn't even a common union of languages that you can universally expect. In that regard Donald Knuth's work in the fictional assembler MIX for TAOCP is worth reading - it's at one remove from any real system. Knuth's TeX source is also quite unique.
- rijoja 9y agosox is kinda nice. Read a lot of it ages ago but I would like to revisit it know with my marginally improved math skills.
- mmphosis 9y agohttp://www.linuxfromscratch.org/lfs/view/stable/ http://www.linuxfromscratch.org/lfs/view/stable/
- nxc18 9y agoReally, no one has said Linux yet? This is very surprising, given how often it is put on a pedestal for its excellent design... Realistically this is because it is in fact a big messy pile of 'at least it works'. Still, it is worth studying as an example of a work that was architected to support open source contribution from thousands of developers.
- qb45 9y agoSo which parts did you study and actually understand? I have contributed to device drivers a few times, for example. And I wouldn't really recommend this part of Linux for learning. Maybe they work but code readability is often neglected and it's not unusual to see whole functions without a single line of comment or files/modules without even a short explanation of what they are trying to achieve. Maybe the core is better.
- jdelator 9y agoI liked reading the kernel code when I was an undergrad. At the very least it taught proper coding habits. (mainly to be consistent across my work. e.g. don't name a variable n_object in function and then nameObject in another) The book that helped understand the code was pretty good as well.
- donatj 9y agoGolang itself. It's soooo readable and contains so much wisdom.
- jacquesm 9y agoHashlife and anything else implemented by Norvig, he's one of the best programmers that I've had the pleasure of reading code from. Plenty of tricks to be learned there, as well as fantastic structure.
- abecedarius 9y agoAgreed, his code shows craft at every level. Is a hashlife by him on the web? The first page of google results only turned up an older comment by you.
- jacquesm 9y agoIt used to be, let me see if I can dig it up. Grr, nope :( That sucks. Ok, so try this instead: http://norvig.com/sudoku.html http://norvig.com/sudoku.html Not quite as satisfying but it gives you the general idea, his whole approach is elegant and super direct.
- abecedarius 9y agoOh well, thanks! I'll read the Sudoku solver after I get around to writing my own -- it helps to keep you from just nodding along.
- jacquesm 9y agoI sent an email to see if I'm either mis-remembering or it got taken down. Ok, received answer from Peter Norvig, I must have mis-remembered who wrote what I read, but in the interest of completeness here is a life implementation by Peter Norvig (but not hashlife): https://github.com/norvig/pytudes/blob/master/Life.ipynb https://github.com/norvig/pytudes/blob/master/Life.ipynb
- abecedarius 9y agoNice. I saw one working like that in Clojure and rewrote it in my own Lisp: https://github.com/darius/squeam/blob/master/eg/bag-life.scm https://github.com/darius/squeam/blob/master/eg/bag-life.scm (the bag is like Python's Counter).
- Walkman 9y agoThe Architecture of Open Source applications: http://aosabook.org/en/index.html http://aosabook.org/en/index.html These are very detailed, very well written articles from well-acknowledged developers about their OSS project.
- Clubber 9y agoA half joking response (only half), I would say your own code you wrote a year ago.
- ChicagoBoy11 9y agoEvery time I've done that I can never quite figure out if the guy who wrote it is a tremendous genius or a complete fool.
- m-j-fox 9y agoGhaa my eyes. It burns.
- collyw 9y agoFor me that has motivated the biggest improvement in my own coding - writing and maintaining a system for a few years. If I go back to code I have written a few months ago and it doesn't immediately make sense, then i have no one else to blame but myself (I usually refactor it when this happens).
- QuotesDante 9y agoThe D3 source code. Since this is made of around 30 modules, one might start with [d3-quadtree](https://github.com/d3/d3-quadtree https://github.com/d3/d3-quadtree).
- oddthink 9y agoWhenever I look at the Tcl source code, I'm impressed by how clean it is: https://github.com/tcltk/tcl https://github.com/tcltk/tcl
- taw55 9y agoLugaru Gametick. A succesful indie game written by a highschool student. https://hg.icculus.org/icculus/lugaru/file/97b303e79826/Source/GameTick.cpp https://hg.icculus.org/icculus/lugaru/file/97b303e79826/Sour...
- cercatrova 9y agoHave you seen Overgrowth, it's a spiritual successor by the same guys. They have very interesting development videos on YouTube. https://www.youtube.com/user/WolfireGames/videos https://www.youtube.com/user/WolfireGames/videos
- kenoyer130 9y agohttps://www.amazon.com/Framework-Standard-Annotated-Reference-paperback/dp/0768682088 https://www.amazon.com/Framework-Standard-Annotated-Referenc... Even if you hate c#/java an amazing book on how to write solid clean code.
- kowdermeister 9y agoRead something that matters to you, some library or snippet that you use a lot or you depend on it. Context matters a lot in getting motivated to comprehend things. Otherwise you will just browse some random code with little to no connection and lack of understanding how the authors got there. One source code I look into from time to time is Three.js: https://github.com/mrdoob/three.js/ https://github.com/mrdoob/three.js/ to discover more details over the documentation.
- evanwolf 9y agoI love this. Writers ask the same question. Teaching literature and composition starts with reading the works of greats who game before, learning what made them great, and incorporating their skills into your own tool box.
- likelynew 9y agosmallpt: Global Illumination in 99 lines of C++: http://www.kevinbeason.com/smallpt/ http://www.kevinbeason.com/smallpt/
- zachwill 9y agoOne of the pieces of code I read through that helped me the most: Beautiful Soup. If you're a Python developer, I wholeheartedly recommend you read through it -- although I do think Leonard has removed some of his more disdainful comments from BS2. When I originally read it, I loved how you could really feel how much he hated all the XML/HTML parsing gotchas. I think it's the only time I've laughed out loud reading through code because of humorous comments and TODO notes to self.
- sigjuice 9y agoAs a Linux user, I find it very useful to set up the source and debug symbol repositories for the distribution that I am running. This way I have a large body of code easily accessible which matches what I am actually running. I usually grep for error/log messages, or examine core dumps or hung/misbehaving programs using gdb.
- EternalData 9y agoThanks for bringing this thread of thought to light! Sometimes I look through Stack Overflow not only for solutions to particular problems, but particularly elegant solutions in a proactive manner. I often just browse through profiles of people with tons of karma.
- Animats 9y agoFang.[1] Fang is a utility program for UNIVAC 1108 computers, written in 1972. UNIVAC's EXEC 8 had threads and async I/O for user programs, decades before UNIX. The machines were shared-memory multiprocessors. FANG uses those capabilities to parallelize copying jobs. The UNIVAC mainframes had plenty of I/O parallelism and many I/O devices, so this was a significant performance win. See especially "schprocs". Those are the classic primitives from Dijkstra: P, V, and bounded buffers. That technology predates Go by 40 years. Here's Dijkstra's P function: . . . DIJKSTRA P FUNCTION . . . LA,U A0,<QUEUE> . LMJ X11,P . <RETURN> X5 DESTROYED . P* TS QHEAD,A0 LOCK THE QUEUE LX X5,QN,A0 LOAD QUEUE COUNT (note: load) ANX,U X5,1 BACK UP THE COUNT (note: Add Negative, i.e. subtract) SX X5,QN,A0 REPLACE THE COUNT IN THE QUEUE (note: store) TN X5 DO WE NEED TO DEACTIVATE HIM ? (note: Test Negative) J PDONE NO. SKIP DEACTIVATION (note: Jump, i.e. branch) ON TSQ=0 (note: this is an assembly-time ifdef) LX X5,QHL,A0 LOAD BACK LINK OF QUEUE SX X5,QHL,X4 PUT INTO BACK LINK OF ACTIVITY SX X4,QFL,X5 CHAIN ACTIVITY TO LAST ACTIVITY SA A0,QFL,X4 CHAIN HEAD TO NEW ACTIVITY SX X4,QHL,A0 MAKE THE NEW ACTIVITY LAST ON QUEUE CTS QHEAD,A0 RELEASE PROTECTION ON QUEUE HEAD SCHDACT* DACT$ . DEACTIVATE PROCESS (note: system call) OFF ON TSQ (note: for later version of OS with alt wait fn) C$TSQ QHEAD,A0 WAIT FOR C$TSA (note: system call) OFF J 0,X11 RETURN AFTER ACTIVATION . PDONE CTS QHEAD,A0 UNLOCK THE QUEUE (note: not a system call, just a macro. Stores 0.) J 0,X11 RETURN (Notes: Instruction format is OPERATOR REG,OFFSET,INDEXREG The "TS" instruction is "Test and Set". That's atomic. If the flag is already set, an interrupt occurs and the OS does a thread switch. CTS just clears the flag. Later versions of the OS support C$TSQ and C$TSA, where the OS queues waiting test and set operations. X4 is the "switch list", the local data for the thread.) [1] https://www.fourmilab.ch/documents/univac/fang/ https://www.fourmilab.ch/documents/univac/fang/
- kruhft 9y agoRead the source code to Emacs. It has everything a growing programmer needs: portability, compilers and interpreters, user interface(s), language design, build systems... http://www.gnu.org/software/emacs http://www.gnu.org/software/emacs
- navyad 9y agofind popular github repositories for your tech and explore them for fun !!