Hacker Read top | best | new | newcomments | leaders | about | bookmarklet login

You don't read code to understand it?

You can't understand a small block of code without running it in a debugger?

When you read code in a book, how do you understand it?



sort by: page size:

By reading code, I also meant debugging. They don't really explain how things work and their response is usually 'just read the fucking code!' so I presumed that I just am terrible at reading code.

If you read code and don't think about what it's doing, are you really "reading" it?

Reading code without running it in your head feels like reading prose without actually comprehending it.


I certainly _do not_ want to denigrate the idea of writing code that's designed to be read and studied - I think that's a great idea, and try to do it. But I've never been really able to 'read' a codebase, and I don't really understand what people who do this are doing.

My technique for getting to know a codebase is to look at one thing in particular, probably with the help of a debugger, tracing the call stack and how the data flows and changes. I get to know a whole project in sections, focusing on individual bits of functionality.

And if I'm honest, I rarely ever 'read' code this way either - I usually do it because I want to add a feature or improve performance, or squash a bug, and I stop when I understand enough to be fairly sure I'm doing things in a responsible manner - keeping with the architecture and not making what I leave when I'm done any more additionally complex than is required to accomplish my task.

I don't understand how people can 'read code' in any sort of a straightforward manner. The phrasing seems to suggest to me a linear 'reading' like a book or a technical manual. But you can't read code like that. If a project is of even moderate size, there are too many interweaved dependencies. Even a very well factored codebase is at best a top-down or bottom-up tree, and you (or, at least, I) often can't understand the trunk without understanding the disparate branches and leaves. I generally can't keep all that in my head without a task to focus on.

I think it's arguably even irresponsible to suggest to newcomers that they should 'read code' because it sets them up for failure when they try to do it and the complexity inevitably overwhelms them.

Do I misunderstand what people mean when they say "read code"?


The thing is, reading code is hard. Even well written code is hard to read and grok, the other kind can take days to absorb.

But it is very effective as a way of spotting certain kinds of bugs, while at the same time gaining a much deeper understanding of a codebase.

Reading code in a non trivial way is a very special skill that not many developers have, but that is very worthwhile to attain.

Whenever I come to a new codebase, I start by grabbing a pretty random bug and reading the code in that area. just flicking through, forcing myself to ask questions about it and finding out the answers.

actively engaging with code like that is an amazingly effective way of absorbing a codebase.


The simple fact of the matter is that reading code is hard, maybe even impossible in the general case. You can understand code with some amount of effort, but it often boils down to an exercise in reverse engineering.

One thing this means is that in any substantial codebase you are never going to understand all of it. You will typically only have time to learn a fraction of the system, so if you are going to proactively explore the codebase, you will need to prioritize. You probably (but not necessarily) want to get a handle on the top-level architecture before digging deep anywhere.

My final piece of advice is that I personally find it impossible to understand just about any non-trivial piece of code without running it, and running it multiple times (1). Perhaps even many, many times. You can run under a debugger (single stepping or breakpoints) and this seems to work for many people. I still rely on print statements sprinkled through the code myself, adding and removing them as I run the code in question over and over again as my current point of interest moves from place to place in the code. This might sound scary, but it's not that different from the way you normally debug code.

(1) It's entirely possible that the person that wrote the code in the first place also ran it many, many times (testing each small change) as they wrote it. So it's perhaps not unreasonable that you yourself may need to run it many, many times in order to understand it later.


To people who are arguing that you can't look at the code because it's poorly documented or not readable:

that's probably a sign that you don't want to use that code especially if YOU can't understand what the hell it's doing.

I tend to look at tests first more often then dive into the actual code.


Its not about understanding a line of code - anybody can do that. Its about absorbing kilo-lines of code and gaining an understanding of the whole thing. Without having to draw attention to every line.

I can read code by the page, hitting next page at about a 1Hz rate. If the code is not overheated. That means, avoid lots of syntactic bloat, keep it concise, keep it modular, with low branching. Just about what the OP says.


Hmmm, I'm alway really surprised when people comment on threads like this.

How much time did you take to do this, I can read code, but I simply can't casually take a look at code like this and say, hey that's cool, hey that's interesting, I have to concentrate and go line by line, a more debugging state of mind.

There are programmers that can sort of skim read code which just amazes me.


> Beginners don't read the code to find out what something does, they read the docs and the tutorials.

Until the docs and tutorials fall short, or something unexpected happens, or they want to see why something behaves the way it does.


Maybe it's because I'm a less experienced programmer, but I find I have a lot of trouble understanding source code if I'm not trying to find or change something specific. I can't read it like a book, I have to experiment with it.

I'm not sure I understand this idea. By "read code" do you mean run the program in your head? As in, "start with main(), follow this loop, go to func()..."? Because if that is the case, then I think it is really debugging not reading. It would be great to debug projects and there are many projects that include unit tests which can help guide engineers along a slow path to greater understanding.

Personally, I like writing code and borrowing ideas from other, better engineers. I also like my code to be clean and without cruft: https://github.com/keithb-/Valley

But I don't even know if I could "read" my code. It's a web app; there isn't a main(), there isn't a loop. In order for someone to read it, they would need to mentally "send a request" which means they need to somehow have the concepts or the context in their mind when they sit down with the source code. I just don't know how that is supposed to help an engineer, regardless of age or experience.


4. You best read code by trying it out and seeing what pieces of it do.

Try it with an interpreter running.


This is a bit weird to me since I never thought someone would just go and read source code of some program from start to finish. It would be something like reading an encyclopedia starting from the letter A.

That said, I personally DO read code. Besides code reviews that were mentioned in the article I read the code that my code depend on. Especially when I want to better understand some API, I read not only the headers, but the actual implementations of the functions that I'm using.

Sometimes I read code to better understand some algorithm. I only understood how Transformer architecture worked when I read some of its implementations.

So yeah, reading code is cool. We just don't read it as we read books.


This does not resonate with me at all. I can not stand having things in my code I don't fully understand. Without reading the source code of libraries, without reading relevant standards, without understanding the mathematics behind algorithms, there is always this feeling I may make really dumb mistakes. Really understanding all the bits and pieces obviously does not make your code bug free, but I am usually pretty confident that I did not do something horribly wrong.

You've made some really good points about reading code in both of your posts. I'm fairly new to it, but I've started a daily habit of reading code everyday and I've realized this is an important skill I've omitted in the past.

Since most of the code I read (C# code) can be ran on my platform, I'll sometimes run into that rare piece of code that I can't believe it does what it says it does, in those cases I'll download the repo and debug it and many times be surprised to find there was a gap in my understanding. Reading code helps me find knowledge gaps I didn't even know were there.


I would be interested to know what the OP thinks about stepping through code as opposed to reading it. To me, reading code and stepping through it in a debugger are two complimentary ways of understanding it. I call those the static and the dynamic way of viewing code.

I think reading code to understand because you forked a project and want to continue it is very different to following tutorials.

Then being able to read basic code is a nice thing to aspire for, no?

Read and ask questions about what you don't understand.


No, at least not just "reading code". You need to understand and identify the patterns that underly there... so debugging other people's code and lear how other's strategies are solving their problems should be the actual reason behind "read code".
next

Legal | privacy