7 ms·
Ask HN: What code do you frequently read?
Be it for beauty,or for reference, elegance, whatever... what is a piece of code that you find yourself returning to frequently
- dalke 12y agoI can't think of any code which I read and reread. For that matter, there are few books that I reread frequently. The closest is for a software library which I use only occasionally, where it takes a few non-intuitive lines to get things set up correctly. Someone developed an easier API for it, which is closer to the way I think about the problem. When I need to X, I consult that software to see how to implement it.
- halayli 12y agoUsually you don't just go read source code for the heck of it. You read the source code either to contribute a feature or fix a bug. There's no point in reading the src otherwise because you won't find a good enough reason to try to fully understand it.
- marbemac 12y agoI beg to differ. For example, reading the Go source is extremely useful both to learn idiomatic Go, and for reference.
- fapjacks 12y agoI've been reading the Docker source lately and I couldn't agree more.
- halayli 12y agoskimming through the source is one thing, but reading with the intent to thoroughly understand it to a level that you can comfortably modify it is something else.
- wtf_is_up 12y agoIt's the only language I've used so far where I feel comfortable going straight to the github source and figuring out how a library works instead of searching stackoverflow. In fact, since I've been writing a lot of code in Go, I've noticed my SO usage has dropped considerably.
- amirouche 12y agoWithout having a specific goal it's difficult to read code. "Learning how it works" is not precise enough. It's not useless you learn stuff but you have nothing obvious to show yourself as an achievement. Rewritring or "deconstructing" the code is helpful to understand it.
- wiredfool 12y agoI've been reading the code for Pound lately, trying to get a handle on where I'm getting some latency events. I'm really wishing I had something like dtrace for OpenBSD, since all I have is ktrace. You can get timing info out of it, but to get what I really want, I'm going to have to add some utrace calls at the appropriate points.
- vram22 12y agoIt can be useful to read source code other than for reasons you state: https://www.google.co.in/search?q=code+reading+the+open+source+perspective https://www.google.co.in/search?q=code+reading+the+open+sour...
- mholt 12y agoWhen I feel burnt out, I find it refreshing to take a break and look at something I've already accomplished. Sometimes that means source code I've written. Looking through my code reminds that I can do hard things. I can solve hard problems, even elegantly. Other times it reminds me how painful a project was to finish or I chuckle at how BAD of a coder I was back then compared to now. It helps me feel like I'm making progress despite the relentless stack of issues, problems, and assignments. And when I do feel motivated to be a better coder, I try to explain somebody else's source code to somebody else. It helps me be a better reader and writer.
- mischief6 12y agohttps://code.google.com/p/plan9front/ https://code.google.com/p/plan9front/
- ahi 12y agoIf I am returning to a piece of code frequently it's not because the code is good, but because the documentation is bad.
- joshschreuder 12y agoOr because the code is bad and you keep getting issues because of it. I think some of the best code I use is that which I don't look at frequently, because it means it's behaving well on its own / in concert with other components, and is abstracted enough that it doesn't require changes.
- krapp 12y agoMy goal is to complete a game in C++ using SDL2 this year, so i've been reading a ton of that on Github just to see how other people do it.
- luizpv9 12y agoThis is actually one of my goals too. If I can recommend, the godot engine[https://github.com/okamstudio/godot https://github.com/okamstudio/godot] is my main source of inspiration and learning.
- DoggettCK 12y agoIf you're not already following it, check out Handmade Hero (https://handmadehero.org/ https://handmadehero.org/). Great, free video series from Casey Muratori that guides you through building a game from scratch.
- krapp 12y agoI am following it, slowly but surely. Unfortunately, he's up to day 75 and i'm still on day 11.
- amirouche 12y agoThe code I happen to use at work. This might sound silly, sometime I read code just to understand one specific behavior, say one method and come back to it later because I need to know something else.
- vhost- 12y agoI pretty much read the code for every dependency I use. It helps me understand how it works and what the docs are fibbing about. If I can't read the code because it's proprietary, I always strace it to see what it's doing. For me, it makes it easier to reason about the behavior of my program. Currently my favorite code to read includes: key/value databases written in C and other "lower level" languages, and the golang standard library packages.
- sillysaurus3 12y agoWould you mind going into detail about how you use strace in that context? Or about how you learned to do that? That's a very handy skill which I need to learn.
- seanp2k2 12y agoThis should whet your appetite and give you a bit of direction on what to dig deeper into: http://althing.cs.dartmouth.edu/local/reverse-talk.pdf http://althing.cs.dartmouth.edu/local/reverse-talk.pdf
- vhost- 12y agoStart with htop. You can get that package from apt in a debian-based distro: `sudo apt-get install htop strace`. When you launch htop, you will see a list of all your running procs. If you select a proc with the arrow keys, you can hit "s" to start a strace and then hit f4 to follow the strace as it dishes output. This won't work for all procs, you will need root to strace certain procs. This is a good place to start as it's visual and easy to follow. Later you can start traces from the shell with proc IDs and even get system call info from GDB.
- d0m 12y agoLearning Android on the side so I read simple projects to see how activities or fragments are implemented, etc.
- d0m 12y agoLearning Android on the side so I read simple projects to see how activities or fragments are implemented, etc.
- togasystems 12y agoBroken code.
- matthewarkin 12y agoThe Stripe bindings (primarily Ruby, Node and PHP). The API docs don't show how to do anything so being able to see what is expected and the format from the function header is very helpful when helping out in IRC. Reading through the pull requests is also helpful in learning / speculating on some new features (this is pretty rare though)
- bliti 12y agoI re-visit my old code. Due to how I'm constantly thinking about how much better it could have been. Not that I doubt my abilities. Its just that I wish I knew back then what I know now. Not only my coding skills, but about the problem I was trying to solve.
- IgorPartola 12y agoDjango and Django Rest Framework. DRF docs are pretty bad (though as the saying goes, better than none at all), so I end up reading the code to figure out how to do what I need. I read Django code when things go wrong or to implement e.g. an auth backend, or see how come get_by_native_key() doesn't always need to take a native key as a parameter.
- shawnps 12y agoGolang stdlib is an excellent reference for how to write idiomatic Go code https://github.com/golang/go/tree/master/src https://github.com/golang/go/tree/master/src
- fortytw2 12y agoIt's also easily among the most self-documenting code I've ever read
- donatj 12y agoIt's so beautifully understandable.
- frakkingcylons 12y agoMy experience with learning Go so far has involved: * Do the tour (tour.golang.org) * Read 'How to Write Go Code' * Try to write a simple, concurrent program (I went with a web crawler) * Read 'Effective Go', stdlib code And now I'm trying to make the concurrent web crawler distributed! It's a lot of fun so far.
- arvinsim 12y agoAs someone who never learned to code in C, should I be ashamed that I find it difficult to read code with pointers and references?
- deleted 12y ago[deleted]
- djb_hackernews 12y agoI write a lot of code and have an interest in new languages. I've looked in to Go quite a bit (especially since I spend a lot of time working with Docker) and I guess I might be missing something. Taking a random method here: https://github.com/golang/go/blob/master/src/strings/reader.go#L31 https://github.com/golang/go/blob/master/src/strings/reader.... Some questions that come to me when looking at that code: - In general, why is everything abbreviated? Storage isn't an issue and IDEs solve the typing problem. It feels really old school. Plus you lose a lot of meaning throughout the code (r.prevRune is actually an index, so why not reader.previousRuneIndex, or at least r.prevRuneIdx) - What is int64? A cast? Why would you need to cast what appears to already be an int? - what is a Rune? Why are we setting it to -1? -1 just seems like a special number that would make this more readable as a constant with a relevant name. - Where does copy come from? why does it appear to work backwards compared to any other language array copy implementation? - Why does the last return statement return nothing? I'm not here to start a language war, I truly believe it is my inability to see the forest for the trees, but I find Go code to be hard to read and understand.
- joshavant 12y agoOther people's examples! I frequently search Google for a class name and append 'site: github.com' in order to search all the source hosted on GitHub for examples on how others are using said class.
- seanponeil 12y agoAOSP https://source.android.com/ https://source.android.com/
- nichochar 12y agoThe shitty legacy code people that worked at whatever company i'm working for wrote before me, and usually didn't document...
- nickstefan12 12y agoI read the express.js source quite a few times. I wanted to reason out how one goes from raw node to something like express. I found it well written, but to really understand it I found myself making toy versions of each functionality. http://www.nickstefan.net/blog/view/express-under-the-hood http://www.nickstefan.net/blog/view/express-under-the-hood
- mhluongo 12y agoI used to read the Neo4j source (I wanted to write my own graph database). Then I was always reading the Django ORM source as reference for a Neo4j / Django ORM integration I was working on (https://github.com/scholrly/neo4django https://github.com/scholrly/neo4django). I've found that diving into a code base is a great way to learn about a new topic or language, and to get rid of the "magic".
- cypher543 12y agoLately I've been sifting through the OpenBSD kernel's source code. I'd like to write a driver or two for it some day.
- ioddly 12y agoBack when I was playing with PLDI, I would read the source code for the simpler garbage collectors out there as well as several I wrote myself. Amazing how touchy and error-prone it can be. It taught me a lot, because programming a garbage collector is as one source who I can't remember off the top of my head put it: programming with your back against the wall. Every memory allocation is serious business.
- tel 12y agoFunction Pearls. Generally code in papers.
- oscilloscope 12y agoSeveral hundred of the examples on http://bl.ocks.org/mbostock http://bl.ocks.org/mbostock
- zatkin 12y agoI read a lot of JRE/JDK source code (through IntelliJ, which probably comes from OpenJDK) just to see how they implement their classes. For example, I found out a few days ago that Collections.sort uses a dual pivot quicksort that I had never heard about before. I'm going to check out some Go stdlib next, I had no idea that was open source. Oh, and possibly the OpenBSD kernel.
- swalsh 12y agoAs a C# developer, I used to read the mono code base a lot. There are so many things about the CLR I've learned from reading it.
- humanarity 12y agoWebsite code from the Dev Console / Element Inspector in Chrome. In terms what I return to, I refer to the LZ algorithm quite often. LZ77 and LZ78 are beautiful in the simplicity and power of their logic. I used to like to look at PRNGs, for example the one in RC4, and hashes. I like the question -- "How do they get so much entropy from so few lines?" Occasionally I'll look at a one or two line functional programming routine in Python to see about the question, "How can this be done more neatly, elegantly, simply or quickly?" Algorithms are beautiful, not the code. Code is just worldly forms, which are all an illusion.
- moron4hire 12y agoI have this data access library I've been chipping away at for about 7 years now. Started it when .NET 2 was fresh and hip and we were still stuck on .NET 1.1 at work. I really started it because my coworkers couldn't be trusted to dispose of their command and connection objects correctly. But I've kept using it because I keep finding ways to add features to it to make my next project very productive. So, about once every 6 months I read through it to see if there are any obvious features I can add. Then I read through it to see if there are features I can collapse. It's been interesting to see the features grow linearly, but the code base grow logarithmicaly. People have asked me why I didn't just use LINQ to SQL or Entity Framework or whatever, but I don't think they understand that A) those things didn't exist at the time, and B) there's something about having a tool that you know inside-out.
- cookrn 12y agoAaron Swartz thought we should read djb's code: http://www.aaronsw.com/weblog/djb http://www.aaronsw.com/weblog/djb I haven't gotten to it yet, but hope to!
- JoachimSchipper 12y agoNow submitted as https://news.ycombinator.com/item?id=9155741 https://news.ycombinator.com/item?id=9155741 (not by me.)
- flippant 12y agoI like looking at OdinMS's source for nostalgia.
- kami8845 12y agoWell that just nostalgia'd me hard
- rgrannell1 12y agoI read lodash's source to get a feel for how to write fast, high-level, bug-avoiding js. There are a few good ideas in it, like aliasing base methods like 'Array.splice' to a function 'splice' to prevent someone breaking your code by cloberring/overriding these methods.
- carlosthejackal 12y agoI read a lot of my old Python whenever I need to cheer myself up. Not sure why it helps.
- kidmenot 12y agoI've been using http://referencesource.microsoft.com http://referencesource.microsoft.com quite frequently.
- zoner 12y agoI read Magento's code (both 1.x and 2) every time I implement something for it. I seen some developers just started to write something, resulting the coding style been quite different than it should be. Reading the core also helps to get comfortable with the coding standard and naming conventions. Also, Magento 2's code is beautiful, despite it's written in PHP :)
- nemesisrobot 12y agoI enjoy reading well-written C code, and in my opinion, two very good examples are redis and the Quake 3 source (the same goes for all other id/Carmack code). Reading the Quake stuff is doubly enjoyable thanks to Fabien Sanglard's excellent Code Review series: http://fabiensanglard.net/quake3/ http://fabiensanglard.net/quake3/ (he's also done them for many (all?) of the other open sourced id games.
- batram 12y agoDecompiled ActionScript looking for vulnerabilities. Using https://www.free-decompiler.com/ https://www.free-decompiler.com/ to decompile and analyzing the code in my own tools. I really love the clean code the decompiler gives you, compared to packed JavaScript which is just a pain to read for a human.
- aashu_dwivedi 12y agodjango and AOSP
- lanna 12y agoThe Scala Standard Library: https://github.com/scala/scala/tree/2.11.x/src/library/scala https://github.com/scala/scala/tree/2.11.x/src/library/scala
- macDre 12y agoFor some reason I've always thought that recursive coding is the most beautiful.
- zengr 12y agoMy goto codebase to reference java is https://github.com/elasticsearch/elasticsearch https://github.com/elasticsearch/elasticsearch