r/ExperiencedDevs
•
u/oneradsn
•
Sep 12 '23
•
1
How to quickly understand large codebases?
Hi all,
I'm a software engineer with a few years of experience hoping to get promoted to a senior level role in my company. However, I realize I have a hard time quickly getting up to speed in a new code base and understanding the details at a deep technical level fast. On a previous team, there was a code base that basically did a bunch of ETL in Java and I found the logic to be totally incomprehensible. Luckily, I was able to avoid having to do any work on it. However, a new engineer was hired and after a few weeks they head created a pretty detailed diagram outlining the logic in the code base. I was totally floored and felt embarrassed by my inability to do the same.
What tips do you guys have for understanding a codebase deeply to enable you to make changes, modifications or refactors? Do you make diagrams to visualize the flow of logic (if so, what tools or resources are there to teach this or help with this)? Looking specifically for resources or tools that have helped you improve this skill.
Thanks!
28
u/senepol Engineering Manager Sep 12 '23
Mental models help. So does being willing to be very wrong publicly.
I like to start by understanding APIs for system to system interactions and types for deeper code flow. Then it’s “just” a matter of refining your understanding (ie learning you’re wrong and updating your model) until it’s good enough. This process never really stops.
84
u/chsiao999 Software Engineer Sep 12 '23 edited Sep 13 '23
My personal approach is to understand concepts above all else as a first priority. I make it my goal to understand what the codebase is meant to represent, and what the goal of the project is.
To do this, I abstract away as much stuff as I can. Assign a purpose to whole directories, don't worry too much about its contents. Try to learn where and why boundaries delimiting modular components of the codebase exist. Then try and see which components are most important to learn, and dive into them.
The goal is get to a point where I can say "I don't know exactly how this works, but I know why it exists, and generally how it fits in." Then use your high level conceptual understanding to guide your foray into the ambiguity of the code.
Then of course a big part of this is sanity checking and verifying. As you start diving in to some components, the concepts you think you understand will be put to the test. Always remember, what you think something should be doing might not actually be what it is doing.
2
u/camelCaseCoffeeTable Sep 13 '23
“Luckily I was able to avoid having to do any work on it.”
May tip us to change that mindset. You can’t learn how you specifically get up to speed quickly without getting uncomfortable and taking on hard tasks. It’s how you grow. Don’t shy away from it, dive in. The new engineer was able to do it because he spent a few weeks actually trying to understand it, from your post it sounds like you spent an hour or two, decided it’s too hard, and prayed you wouldn’t have to work in it.
That’s not the attitude of a senior engineer, and it’s not an attitude that will get you any better at understanding code quickly. You have to go get experience with unfamiliar code based to get better at understanding them. It’s as simple as that.
21
u/poralexc Sep 12 '23 edited Sep 12 '23
Reading code is its own skill, different from leetcode or greenfield programming. It’s almost like sightreading in music.
For practice, start with some of your favorite oss libs/tools and skim through their repo. And I really mean skim—readmes and top level source dirs only. Look at names, why are files grouped together?
Once you feel like you have a grasp of the shape of things, start to look at the parts of the codebase most interesting to you.
For diagrams I like plantUML, but there are a billion different tools for different preferences.
Also, use tools—code is data and can be queried. At one point I built a gradle plugin to build class diagrams in UML, though I think IntelliJ can probably do something like that now.
Old school cli utilities like grep, sed, jq can get you a lot of info.
Personally I always have a notebook/scratch paper for drawing pictures and thinking out loud.
12
u/Jmc_da_boss Sep 13 '23
Get it running, poke at it
11
u/[deleted] Sep 12 '23
You need to understand dependencies first - what calls what. Overall system architecture kind of thing. Understanding the details inside the code is only necessary if you have to change the code.
There are certain lines of code to search for if you were having to generate this diagram yourself. For example, an API call would use certain libraries to accomplish that task.
7
u/kasakka1 Sep 12 '23
I recently started at a new client, and the things that helped me understand their quite complex codebase was just someone showing me how the code vs end result relates and works. So don't be afraid to ask someone knowledgeable to run you through it. People are generally happy to explain if they have the knowledge.
Otherwise the best way is to simply dig in, start from doing something easy like fixing a relatively simple bug or adding a small feature. As time goes on you get into it deeper and deeper.
7
u/WhiskyStandard Lead Developer / 20+ YoE / US Sep 13 '23 edited Sep 13 '23
Profile a couple of the most common workloads. I rarely see profilers called out as anything but an optimization tool, but they’re incredibly useful for understanding the code as it’s executed (rather than as someone thought it would be). A flame/icicle graph will show you many of the most important areas of code and give you a roadmap to where you should dedicate you code reading time.
Even better: if no one has ever profiled the code before you’ll probably find a 3-5% low hanging fruit performance improvement and everyone will be like “you just got here, how did you do that?”
5
u/puremourning Arch Architect. 20 YoE, Finance Sep 13 '23
Use a debugger. And a notebook.
5
u/Scientific_Artist444 Sep 29 '23
In my experience, domain understanding helps a lot.
It's easy nowadays to clone git repositories. But if you don't understand the software as a user, you will have a hard time understanding what the code is doing.
If the software is documented, I would first look at the documentation to get a high level understanding of the software. And then look at the code to map the same information in the docs to look for its implementation in code.
If the software is undocumented, I would not bother reading the whole code. Only the part where changes are to be made would be what I work on. And then if something breaks later during testing, I fix it. That's why testing and requirements are so important. Testing well requires understanding of requirements. When you understand requirements well, your code reflects it.
4
u/EntshuldigungOK Sep 13 '23
By now, you have a good birds eye view. You can go into details depending on what's available.
Ex: If you can run the code in test environment, rest of the understanding is a slam dunk.
3
u/crazylikeajellyfish Sep 12 '23
I think there's a last mile where someone with knowledge needs to explain intentions, as plenty of code isn't self-explanatory. However, you can get 90% of the way there by starting from where you're working and then tracing every function/class you use back to its definition, then repeat the process with those files.
At the end of the day, you've gotta read code. I recommend working out from what you know, rather than taking a top-down approach which likely includes stuff you won't ever care about. You only really need to understand the dependent graph of what your work touches -- that could be most of the codebase, or it could be a set of standardized internal APIs.
Maybe ask an engineer to spend an hour with you explaining the purpose of all the top-level directories, though, so you have a starting point when you encounter an entirely new area of the codebase.
Edit: I'll also add that this skill -- reading code you don't understand and figuring out what you can -- is one of the most concrete things that separate experienced engineers from newbies. Figure out absolutely everything you can from source code and docs, then ask people with experience to fill in whatever you're still confused on. Starting by asking someone to explain it, rather than independently researching, is a pretty negative signal to me.
3
u/kronik85 Sep 13 '23
Damn, there's a book out there that covers techniques for learning a code base quickly. It had studies supporting different strategies.
One strategy was speed reading the code base in an hour. Just getting the general shape of the structure and classes.
One strategy was interviewing developers using informed questions from the speed reading, etc.
Wish I could remember the title.
2
u/InterpretiveTrail Staff Engineer Sep 12 '23
I usually start to figuring if I know what the inputs and outputs of the system are. Because worst/best case depending on how you view it, I'm going to hold my breath, patch a thing, and then regression test the shit out of it.
However, if we're actually trying to have a deeper rewrite (or eventual replacement), having that understanding of inputs and outputs is what starts to narrow down 'areas of interest' for me in the code base.
I like to take quick passes going through the code itself and try to document what logic is happening. Where's my "faucet" where's my "sink" for my input and output respectfully.
Then I write pseudo code that a product owner could understand as a quick 'map' of things. Like think you've 10 sentences to sum it all up. Keep it HIGH level what's happening and don't get bogged down in the swamp of code. But I'm a big believer in writing wiki-pages (something as simple as a markdown in a github page to more formal wiki systems like those in Jira or whatever you company uses).
Usually making a wiki page that other people can reference is useful. Because it's likely not just my/my-team problem (the first party) but if I can help others understand a bit more about the process (Product Owners, Directors, etc). then I think I've "won" more. Because I'm either gaining more empathy from others when they want shit changed in legacy, or have a better understanding of the risk that legacy poses and hopefully encourage more resources to fix/replace. Either way, knowledge is power.
Then I just start taking passes on where I think I need to start diving deeper into areas to gain an understanding of what it is that I'm trying to do. I like to take it one layer at a time. I'm an archeologist and I don't know when I might find something of note, so I must be gentle with the dirt and debris that I remove.
Sometimes layers are fast, sometimes, slow.
I guess TL;DR of my approach for legacy stuff: Read code. Document. Repeat at a finer level of detail where necessary.
Regardless if any of that might be of use, best of luck!
2
u/brystephor Sep 12 '23
I ask these questions to start
From there it's just a matter of digging into the core APIs and seeing how they work. The side secondary stuff doesn't matter much to get an understanding of the core flow.
2
u/chunky_kereru Sep 13 '23
I like to start (in this order) with:
From there I find it’s usually pretty do-able to start bucketing things into user journeys / business flows and the data they interact with. At that point I can typically dive into any piece of code and understand how it fits in.
2
u/[deleted] Sep 13 '23
Start with the database. Once you understand what data is stored and the relationship between the tables (assuming it’s a relational database) it’s a lot easier to go up one layer and understand how that data is making it into the database.
2
u/local_eclectic Sep 13 '23
I try to use it first before digging into the code. What are the inputs and outputs? What is it all for?
Then, I'll start pulling threads to figure out the nitty gritty details.
2
u/[deleted] Sep 13 '23
I map each screen/page on a Figma file and try to figure out all the use cases I have in there, I also sniff out the network and see what backend API calls the app/page makes. It is not a bullet-proof solution, but it helps to understand the large picture and raise meaningful questions about what is what.
1
u/leetlode Mar 23 '24
This is crazy! I have surfaced your question on how to quickly understand large codebases to every team I worked on. I worked at Manulife, SAP, and now Amazon. They all have the same issue, lack of documentation that maps to the source code implementation!
I build this tool where you can create diagrams as usual but then you can link the diagram nodes to actual source code and add onboarding tutorials and simulations on top.
It has allowed me and my team to build the diagram once, link its components to the source code, then add tutorials and simulations of app logic on that diagram. I also created a GitHub action that runs on new PRs to keep the diagram in sync with code changes.
The app is not perfect by any means so let me know your thoughts!
Here you go: https://www.code-canvas.com/
1
u/More-Shop9383 Nov 15 '24
If you're working through a large codebase and want a straightforward way to understand it, Devgen can help. I created Devgen to make it easier for developers to get answers from complex codebases quickly. You can ask questions about the code, find relevant code references, and understand how different elements interact—all in one tool. Devgen also lets you discuss GitHub issues, pull requests, and commits in a chat interface, so you and your team can easily collaborate on code changes, even if some members aren’t coding experts. Take a look at https://devgen.xyz/ to learn more!
1
u/Equivalent-Cream-224 May 23 '25
One of the best thing you can do is
Try to go to the first commit and then move commit by commit and read the commit message
But this only works given that you have a maintained version control system
1
u/urbansong Sep 12 '23
Some of form of this is pretty much answer. My workplace had a C4 diagram and once I started using it to reference things (even when the documentation turned out to be wrong), it really accelerated my understanding. At my next job, I would like to make a C4 diagram (or similar) myself or make it my task to update the existing documentation. I don't think anyone minds if you ask a bunch of stupid question with the very visible intent of writing it down and sharing it.
I am currently using MS Whiteboard to understand new things but I plan to switch to Documentation as Code, particularly Structurizr, soon. I'd still use the Whiteboard but DaC seems like a promising tool.
1
u/[deleted] Sep 13 '23 edited Sep 13 '23
I'm using zoom-in zoom-out approach. I start with a 10000 foot view, where this codebase is a single blackbox. I look at all the connections what are downstream and unstream services, what is the data used by this blackbox. What are the business purpose and expectations. What is the code lifecycle.
Then I starting to zoom-in to some particular component staring from the entry point and going all the way down as deep as possible trying to understand internals of the codebase, code style, implementation choices, etc. I'm doing it with multiple components.
Then I often break stuff and see how tests catching it or write my own tests and use them as a playground. In general I feel like healthy TDD helps a lot with new codebases, especially if you need to start making contributions fast.
1
u/secretBuffetHero Eng Leader, 20+ yrs Sep 13 '23
I do two things:
I use these two diagrams as high level road maps to explain the story of how my component works and how it works within the system.
The lower level details are bound to change so I don't diagram these, I just take it case by case. Sometimes you might go into the unit tests to see what each component does.
1
u/supercargo Sep 13 '23
Specifics really matter here, but unless the codebase is an unmitigated disaster, there is usually a larger structure or repeating pattern that, if you can find it, really helps grok what all the pieces are. An ETL task will be different from a service endpoint, for example.
For statically typed languages like Java, getting everything up in a good IDE is super helpful for me. I will start navigating forward, and then backward, through method invocations. Like “A calls B”, quick glance at B, what else calls B? Pay attention to what types and packages those methods are defined in.
Something else that helps me is to read with intent, like adding a feature, fixing a bug, adding a test case, etc. This can provide a filter for which paths to follow and which to set aside. This helps me if things in my head remain too abstract for me to make important connections.
Also, if there are frameworks in play, go read the documentation for any you’re unfamiliar with. Code bases built on frameworks will tend to follow the conventions of the framework without calling them out specifically. A simple example would be if you don’t know what dependency injection is, and start reading a code base that uses an inversion of control container, you might be left wondering how the hell that thing ever “starts”.
1
u/Infamous-Emotion-747 Sep 13 '23 edited Sep 13 '23
Wrap your head around
From here, you can then start predicting what classes/methods should exist. This tests your knowledge, if the entity does not exist, either
I recently drew a diagram with basically 4 boxes, listing our 2 major concepts in our system, and the 2 major processes and then how they all relate. The entire managerial team went quiet, before someone said ... "nobody has ever explained it like that before". I was reminded at how predictive/powerful just focussing on broad concepts can be.
If you want to dazzle people with complexity of diagrams, use an automated tool to walk the system and generate a diagram for you.
1
u/Nater5000 Sep 13 '23
A lot of people are providing a lot of good answers, but I wanted to provide an alternative solution that may be worth the effort.
If you're willing to jump through some hoops, you can get GPT to internalize a code base well enough to answer questions about it. This can be quite powerful, especially if you use continuously (rather than just as an upfront information dump). Someone else in the thread mentioned "ChatGPT," but that's not gonna cut it. You'd need to use proper tooling around the GPT API to handle this effectively (such tooling already exists out of the box and/or as a service, but it's not too difficult to set this up yourself).
I know plenty of people on this sub will probably hate this answer, but it's important to keep in mind that this is just another tool that we should all be embracing, especially when it fits this use-case perfectly. It doesn't mean that you shouldn't try to understand the codebases using more "classical strategies," but you only stand to gain from using new tools which (other than the cost of a little labor) are immediately available and accessible.
1
u/engineerFWSWHW Software Engineer, 10+ YOE Sep 13 '23
This is an important skill especially if you are going to a senior level. I work on c/c++ projects and i use eclipse cdt to dissect and understand larger codebases. I specifically use the call hierarchy feature to see the functional calls from a callee and callers perspective which helps me develop a mental map of the codebase, and bookmark/task tags to be quickly jump between sections i looked before. I rarely use diagrams nowadays.
1
u/sammymammy2 Software Engineer Sep 12 '23
You can always run it in a debugger and step function by function. Or read code. Assuming you understand what the point of the codebase is.
-6
u/restarting_today Sep 13 '23
ChatGPT
6
u/[deleted] Feb 28 '26
[removed] — view removed comment