As you develop as a programmer, software developer, or coder, IMHO, you begin to see different (best or worst) practices, different algorithms, and "more than one way to do it". Seeing this code can be a great learning experience for you, even though you did not write the code. But is doing this only going to confuse you?

For example, let's say you have a library in any language that was created by a colleague, and you have been using it for a while. You decide to look at the actual source code, regardless of how extensive it is, and get a better look at how this library is written. For the sake of example, the function you use most often from this library is the max function, which finds the largest of two numbers. But this function is a lot more complicated than it needs to be. The way it is written is confusing the heck out of you, and you don't know how this works.

Will this make you a better programmer, because you realize how complicated it is for such a simple function, or will it make you a worse coder because you feel less confidant?

So my question, in general, is does reading source code make you a better programmer and if so how? If not why do people still do it?.

9 Answers
9

You read the code of a function and it's surprisingly complex: You could get discouraged because it's complicated and scary and it hurts your confidence. Or you could try to understand it. You might fail, but I think in general you'll still be better off doing it, as you'll probably learn something from it, and even if you learn something very small, it's still better than learning nothing at all.

I don't know many people that just sit down and start reading source code recreationally. I don't do it, I don't find it fun. Usually if I'm "reading code" it's as an example in some documentation, or it's part of an existing system I'm working on (maybe adding some features, fixing bugs, whatever). Both of these can be very good learning sources.

Reading source code is difficult for many people. It takes large amount of effort to learn to do it properly so that you ignore the irrelevant details, and concentrate on the important stuff. The time frame for learning this stuff is like 10-20 years. You can't expect people who have written code for short amount of time to know how to read code properly.

On the other hand, it's a skill that can be learned. It requires concentrated effort during several years to learn to do it. It does help tremendously once you learn it. It's like finding problems in the code that were completely impossible to find before -- sometimes even finds problems in modules which you have never even seen the code.

10-20 years? Sure, more experience helps but you can start reading source code after 1 or 2 years of programming. You will be slow and be confused quite often but it is doable. Also: what kind of source code are you reading? Multi-threading? Concurrency? Hardcore C++? Wordpress templates or plugins? It really depends.
–
Niels BomOct 31 '12 at 21:32

You won't always have the platter when it comes to source code. Some people write elegant code in a manner that everyone can consume them but some write in a manner that even they can't understand it later.

Either way, you will have something to learn from. If the code is written well, you can take it into your stride or else if the code is very complicated, you will have your mind rolling to learn how this can be achieved in a better and cleaner manner.

Although it will contribute I don't believe just reading code is the main reason for becoming a better programmer. If it was then I would be a super programmer by now with all the code I've read over the years (o I can only dream).

I think it's a combination of trying to understand the code you are reading and hence in the process determining if that code is a good/bad solution.

In the process of coming to this decision you tend to do things like:

Query the original developer on their purpose when writing said code

Read articles of other developers on whether parts of the code is good programming practice

Decide if you understood the code from one scan through. Often if so then that is a good starting point for knowing you are reading good code.

Try to figure out a better way of doing a piece of code in the language you are developing in.

Putting these elements together will help you on the way to becoming a better programmer.

So in summary. The reading of the code is the means. The methodology with which you go about processing/analyzing that code is what will help you become better at programming.

Why do people do it? Because you have to learn from somewhere and reading your peer's code gives you one avenue by which to achieve what most programmers desire. That being an elegant/clean/extensible solution that they can sit back at the end of the day with a nice cold beer and say job done!

There I don't think there are a great number of people out there who just spin the wheel and start reading source code of random programs.

But in the interest of continuing education, generally what I do is pick one of the games I like (I use Ubuntu on my Desktop so most of the games I play are available as source), and try to run through in my head how it works. How would I do it if I were to write it from scratch without seeing the code first? Not just generalized but think about the interactions between the characters and the world, or the characters and each other, or perhaps how the map is dynamically generated or displayed to the screen, or how the AI works.
Then, with that in your head, see how close you were. In comparing the way you would have done it with the way it is currently written, you will see other ways of programming it, and it will be more engaging than just picking a program and diving in as if it were a sci-fi novel or the like.

It is very beneficial to read some interresting codes of my collegues. But not programs as a whole, because the most of all programs is just a boilerplate code. The best you can do, is read blogs of good programmers, because they will show you the interresting part and even tell you, why they used such a construction - you will learn to thing their way. And this the most important thing - not the code itself, but the thoughts behind it.

I think I need not to add that even better is to argue with your colleagues and friends programmers how to solve some issue - you will find many new thoughts and together you might event devise approach, which is new to both (all) of you.

I definitely think it does; the trick is finding good code to read. I think I learned almost as much from reading the source to the old CDC6000 Pascal compiler as I did from my compiler course. I also learned a great deal about operating system internals from perusing the 4.2 BSD kernel sources. Nowadays, I'd probably take a look at the L4 microkernel or the OpenJDK sources.

I occasionally peruse source code of software. It provides bits and pieces of tactical information. More strategic information can't be gathered without spending actual time working on it and studying the system overall, I think.

nginx has some quite pretty C code. It's been described as geometric.

SBCL code is pretty nice as well, and is pretty old; you can get a sense of the historical flow of code when you read it.