what are techniques you all used to learn and understand a large codebase? what are the tools you use?
Ask HN: How to understand the large codebase of an open-source project?
1–10 of 49 posts
Re: Ask HN: How to understand the large codebase of an open-source project?
#2I usually start by running cloc and sloccount to get an idea of the metrics of it, languages line of code estimates etc...
I progress to looking at the tests if there are any. They usually give an idea of how the authors expect things to work.
Once I have browsed some of the tests, in particular integration tests I start following how they work through the code. Your IDE of choice will help out here or failing that use ripgrep, ack, the silver searcher, searchcode server (note I run this so I am biased), sourcegraph.
One thing that I have found especially valuable is running something to determine the cyclomatic complexity of the code. Knowing which parts are complex is a good way to determine where you should focus your time.
Re: Ask HN: How to understand the large codebase of an open-source project?
#3Re: Ask HN: How to understand the large codebase of an open-source project?
#4Re: Ask HN: How to understand the large codebase of an open-source project?
#5The analytic approach is a bit more awkward, since you have no specific goals and need to make them up yourself. So you could pose questions like how a specific behaviour of the application comes about ("why does it do that when this happens?", "how does it do X?") and then try to answer those comprehensively, systematically (a format that works well for me is short snippets of code interleaved with explanations and arguments).
A bottom-up approach is generally easier, because your questions will give you information at the bottom (like specific application messages), which are generally easy to find (ag, grep). A good IDE can be helpful for navigating the code and finding call sites, especially in projects written in dynamic languages where such analyses can become kinda annoying. (However, in more awkward code bases analysers like PyCharm are quickly overwhelmed and are unable to resolve indirections)
Top-down is in my experience less useful, because there are far too many choices on each level for most applications, and the first few layers are generally the least interesting and most arcane/fragile and difficult to follow along (things like initialization sequences).
The most difficult projects are typically those relying on multiple languages, code generation and runtime mutation (reflection, on-the-fly UI generation, overly dynamic Python code are typical examples). Another frequent obstacle is excessive abstraction and indirection (implementing something that could be done in a few lines of easy to understand and reason about C using multiple C++ templates spread out over a bunch of files and a healthy dozen of advanced language features is an almost archetypal example).
Re: Ask HN: How to understand the large codebase of an open-source project?
#6Re: Ask HN: How to understand the large codebase of an open-source project?
#7Then I try to go down the main code path of some examples or the primary binary if available and just check out out how things are called/done around there.
Then run an example through callgrind and visualize the call graph in kcachegrind to get an idea of how often things are called and where and where the heavy lifting happens. That last step is optional and really depends on the type of project.
Then I use my code editor and lots of searching and call site lookups to get a better idea of how things are used.
Re: Ask HN: How to understand the large codebase of an open-source project?
#8Check the documentation, if there is any. I've actually tried to add a small section on "where to start reading the code" to my larger projects. If it's a web application for example, you'd probably want to start where the routes are defined and go from there to whatever subsystem you're trying to modify or understand.