Live data from Hacker News

Ask YC: Learning to read code?

news.ycombinator.com

1–10 of 13 posts

Ask YC: Learning to read code?

#1
I'm a high schooler who has worked on a few small projects of my own. Recently I've started to work with a friend[1] and I've discovered that reading code really is an order of magnitude harder than writing it. What's the best way to learn to read code? If the solution is just "read a lot of code", do you have any suggestions for reading material?

--

[1] pg is right about cofounders. At least for some people, including me.

Re: Ask YC: Learning to read code?

#2
Do you have a favorite open source project? Download the source and try to build the runtime files yourself. Then you have an environment to poke through the code and play with. for example, do you like Python? download the source and create a debug build of it.

Another thing to do is to go to code samples online at the example sites. Type them in and/or download them and play with them.

Yet another thing to try is to do a port of a program to another platform. Even if you are not successful, you should learn a lot.

The key is to take more than one approach, as there is no one right way, there is not one true path. Immerse yourself in different programming activities. The important part is practice, practice, practice, like the story on going to Carnegie Hall.

Me? One of my earliest bigger projects was porting an old star trek game written in Altair basic to Apple Basic. I had only mimeographed printouts published in a book of computer games back in the 1970's. Arrays were not handled the same between the two versions of basic, so I had to come up with a different array scheme than was in the book.

Re: Ask YC: Learning to read code?

#5
There's actually two separate skills here. One is learning to just read code, as in, understanding what the code is saying. The second is understanding the code, which would be what the code is doing.

Learning to just read the code requires writing alot in that particular language (I find writing teaches you alot more than reading). When some piece of code uses some aspect of a language in a way you don't understand, go figure out what they are doing, internalize that concept, and go write some code yourself that uses that same concept in that language.

In my experience, writing alot of code is the best way to learn to read it. You'll discover and truly understand why certain things are done certain ways once you've had to go through the pain of doing it any other way. One of the 'first' examples you might run into is how every single header file in a c/c++ library seems to start with an #ifdef/#define combo, and end with an #endif. It might seem a bit bewildering at first. But when you run into your first case of multiple includes of the same file, you'll quickly see why they do it.

As for actually 'understanding' a big pile of code, that's more difficult. This is where you understand the language just fine but you need to try to wrap your mind a giant pile of someone else's code. You have to immerse yourself in their codebase, figure out where all the important files are, and so on. Take it small chunks. Pick a feature, trace the code, put in print statements, etc. And you -really- need a way of quickly navigating the code (jumping to decelerations, switching between header/source, etc). Many IDEs provide some level of functionality to do this. Personally, I use emacs and etags.

Re: Ask YC: Learning to read code?

#6
Try stepping through the code in an interactive debugger. I typically run a gdb-style debugger under Emacs, but anything which lets you step through code and inspect data works well.

Some people really loathe these tools, but I think they're invaluable when someone drops a pile of spaghetti code in your lap and tells you to support it. Figure out the entry point to the code, set a breakpoint in main() or something like that, and start stepping. After ploughing through a few of the code's basic transactions, you should have a sense for the data structures and abstractions it relies on, if any. Then, you should be able to read individual functions and such to get the gritty details. The use of threads substantially complicates the picture, of course.

Re: Ask YC: Learning to read code?

#7
I don't find reading well-written code any more difficult than reading a math book. But of course it is an order of magnitude harder than reading prose. You have to force yourself to slow down and understand what each part is saying. As opposed to ordinary text, every word is important, and you need to know what it's doing there.

Some of this is just proficiency in the specific language, but it's more about the concepts and idioms of programming in general. I've fixed a bug in an open source python project, and I couldn't write a "Hello World" program in python without a reference.

But do note that "well-written" is mandatory. If the code isn't well-written, it will take you a lot longer to figure out. The code I wrote in high school was pretty bad.

Variables should be named to describe what they represent. You should have a reasonable number of comments, not too many or too few (ironically, this amount changes as you get better). Divide your code into manageable chunks and split them into subroutines. Within subroutines, separate the chunks with at least one blank line.

Re: Ask YC: Learning to read code?

#8
When reading code don't treat the task like that of reading a book, the best way I find is to interact with the code that you are reading. So it's crucial to get it to run, then get it to do something useful, then refactor it slightly by making functions shorter and giving the new functions specific name. E.g. instead of saying (first lst) maybe say (node-name lst), ideally the library functions should have names to do with the languages/library data structures and your fns should have names which correspond to the problem you are looking at. This is the approach I take when I want to rewrite something.

If you want to use the code as in a library, then try not to read the details of it until you really have to. Use the interface or write an appropriate interface for you to use. Work through some examples in the debugger to see what it does.

If you need to fix the code then you need to read the code. I try to focus on just the problem, rather than understand the whole thing. This approach makes it much simpler, I get in and out really quick, just focus on the problem and fix it. If I find something else that's a problem then I look at that too, otherwise I avoid figuring out how the code works.

Post reply on HN