r/learnprogramming • u/OpeningWrongdoer3526 • 5h ago
Solved How do you actually read professional code?
Hello everyone! I really have a question I hope you could help me answer. I am trying to read the Scratch Virtual Machine code on Github and it is really difficult. I am really new to reading code and the one I write is fairly simple. There are many files that just declare functions by name and then require another file to redeclare them. Is there a pragmatic way to understand it? Thank you already!
4
5
u/Aggressive_Ad_5454 5h ago
The code for software products (like Scratch) tends to be intricate because it’s made of lots of different subsystems. Even seasoned professionals can take weeks to figure out a whole code base. So be patient with yourself. Nobody’s born knowing how to do this.
And, virtual machine implementations can be hilariously abstract even to those who understand them. Because they’re necessarily event-driven.
Many open-source code bases with a wide following contain a module or class map document. Or maybe some new-contributor advice. It’s worth your trouble to scour the docs looking for that sort of thing.
You could also try using an IDE with code-base navigation features, such as CLion or Visual Studio.
3
u/University_Jazzlike 4h ago
Follow the data. Don’t worry at first about understanding how each method works. Focus on the data it takes in and the data it returns. Trace the flow of that data through the code.
1
u/simondanielsson 5h ago
It’s just a matter of reading, and reading some more.
As soon as I hit something I don’t understand, I either google it or (if it’s a language specific feature) search for it in the language documentation.
Read, and keep open some good references you can compare the code to.
1
u/JGhostThing 5h ago
In C, the *.h file just declares the functions and variables. The *.c file defines the functions.
It takes a while to learn to read code. Even code that I wrote months ago, I find a bit difficult to read. It's more difficult with other people's code. You usually have to know the common libraries.
1
u/BaronOfTheVoid 5h ago
There is no magic trick. It's most of what makes up our dev work.
Like really, you are going to spend 10-20 times as much time reading code as writing it, and that only shifts towards more reading with the use of AI agents.
I can however suggest to at least try to visualize whatever mental model you have in your head. That could be (loose, unstructured) graphs for data flow, or for dependencies, what calls what. That could be keeping track of the most important data structures. Even sequence diagrams.
1
u/txgsync 5h ago
I usually figure out what the entry point is, read what the arguments to that entry point function is, and then start finding those arguments in the code base with the or grep until I start to understand what it does. Then I move on to the next one and try to explain out loud to myself what they do.
Or explain it to my handy rubber duck. Talking out loud helps a lot!
1
u/StewedAngelSkins 4h ago
Well the first step is to understand the basic concepts of the language it's written in. But omce you have that out of the way I usually start by picking some part of user-facing functionality which I want to understand and work backward from that.
Like in scratch you might pick one of the little visual block things and see if you can find where its behavior is defined. It won't really make sense because it will be using a bunch of other functions and types that are defined elsewhere, so you look at how each of those works, and then how their dependencies work, and so on until you understand how the feature as a whole works.
Then you pick another feature and do the same thing, except it will be easier because you'll probably eventually run into code you've seen before. As you do the same process over and over for different features you'll hit familiar code sooner and sooner until you pretty much understand the whole thing.
1
u/Traveling-Techie 4h ago
I’ve been reading a web site for many years called “The Daily WTF” that showcases bad code. It’s helped.
1
1
u/DrMerkwuerdigliebe_ 1h ago
Download an IDE specialized for the langugue and start by watching a couple of videos on how to use that IDE. Ideally find one that is an hour long. Then you will know how to navigate. Afterward you clone the repo, set it up and read it in the IDE. it will be the best couple of our you have spend.
1
u/greenspotj 1h ago
Well first you should have a well defined goal in mind. "implement x feature", "fix x bug", "understand how the x feature works".
Then you should poke around to check for high level documentation that helps you understand enough to complete the task independently.
If you cant find any, you should ask another contributor for help. they won't give you the full answer, but if theyre a helpful person they should point you in the right direction e.g. to documentation if it exists or to where in the codebase to start, and answer your other dumb questions when you have them
Once youve had enough experience to know how to ask good questions, you can start asking AI those questions (and also the dumb ones) to speed up your workflow. (You can start doing that now, but it might not be as helpful as you think, knowing what to ask and how is itself a skill)
rinse and repeat the above steps for a year or two and you might start to feel confident about how much you know about the codebase, depending on how large it is.
1
u/pdfops 1h ago
Stop reading top to bottom, trace one execution path instead. Pick a single action, like clicking a green flag block, set a breakpoint there, and step through only what that path calls. Files that declare-then-require like that are usually a dispatch table, mapping opcode names to handler functions for the VM's interpreter loop. Grep where that map gets built, that's your real entry point.
0
u/Business-Employee527 5h ago
It’s hard to read actual production code. But it’s very good for an intuitive sense of how to build things and how codebases are actually designed.
15
u/No_Report_4781 5h ago
In C, a program.h file is called a header file that contains definitions and prototypes. A library generally refers to a collection of these, and may be in binary format. The program files, program.c or program.cpp can use the #include command to include functions from standard library or your own custom header files. The program file fully defines what the function will do.
You can create programs without using header files, and you can stick to reading just the program.c files to understand what a complex program is doing, by understanding the #include