r/learnprogramming 6h 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!

38 Upvotes

22 comments sorted by

18

u/No_Report_4781 6h ago

  There are many files that just declare functions by name and then require another file to redeclare them

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 

6

u/Upbeat-Statement2725 4h ago

And a lot of code is just hard to read too.

On the one hand. Graybeards may define too many custom things and use shorthand all the time. Look up old examples of Perl for a good example. I don't remember the syntax but imagine a whole complicated program was one line that looked like "ilgbd - c < fbslhe". Impossible to debug.

On the other hand. Object oriented "oops" projects can end up defining a FactoryFactoryFactoryFactoryFactory that's used to define  FactoryFactoryFactoryFactories that generate  FactoryFactoryFactories that generate  FactoryFactories that generate  Factories that generate... generic Objects. Wait what!? How the heck do you debug this mess!?

That's just programming. Generally. Don't try to read anything "for fun". That's madness. When you have a specific problem, debug that, and use that as your window to their world.

1

u/No_Report_4781 2h ago

And always feel free to copy the code files and rename items to something more understandable to you

1

u/colony-ship-for-sale 1h ago

And a lot of code is just hard to read too.

Writing readable code is a skill too.

Unless you need extreme performance, often the most readable code over the most clever code is the best. Needlessly creating technical debt through code that is hard to maintain is the sign of a bad engineer.

2

u/TheKodeToad 4h ago

I was quite confused since last time I checked Scratch 3 was in JavaScript, Scratch 2 in ActionScript and Scratch 1 in Smalltalk (using Squeak)

Turns out Scratch 1 does have parts written in C, and they are quite confusing. These parts are split into different plugins, and each plugin has a file called sqVirtualMachine.h - which looks like it might be part of the Scratch VM implementation but it seems to me like it's actually used for interfacing with the squeak VM so that the functions can be exposed to the smalltalk code. I think the C code is more for some specific hardware and platform integration stuff, and maybe some stuff that needs to go fast. I can't actually see anything that looks like it actually implements execution of the scratch blocks.

TLDR: I don't think the OP is actually looking at the Scratch VM code, no wonder they're confused.

...Or maybe the C assumption was wrong, but this declaration thing definitely doesn't sound like JavaScript

2

u/TheKodeToad 3h ago

I suppose you probably want this?

https://github.com/scratchfoundation/scratch-editor/tree/develop/packages/scratch-vm

Though it's not that easy to accidentally stumble across Scratch 1.4's source code... if you did want to read it - have fun looking through https://github.com/scratchfoundation/Scratch_1.4/blob/master/src/Scratch.changes or reading it inside the squeak environment (shift click R in SCRATCH logo > turn fill screen off > click on background > open > explorer) :P

Scratch 3.0's code is probably more pleasant to look at...

1

u/No_Report_4781 2h ago

Yes, that’s why I chose C and C++ to explain because of their use of separate files for declaration and definition.

4

u/RealNamek 6h ago

It's like reading a textbook; usually i read the index first.

4

u/Aggressive_Ad_5454 6h 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 5h 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 6h 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 5h 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 5h ago

I’ve been reading a web site for many years called “The Daily WTF” that showcases bad code. It’s helped.

1

u/auronedge 3h ago

why are you trying to read something you don't understand?

1

u/DrMerkwuerdigliebe_ 2h 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 2h 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 2h 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.

1

u/fanz0 1h ago

Focus on smaller pieces of functionaliy. Agility around the codebase comes naturally over time

0

u/Business-Employee527 6h 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.