Related learning recommendations: javascript
I like programming, it is also my job, and I am very happy to be able to Spend most of your time developing software. Like many programmers, I was both fascinated and confused by how good the code I wrote was, and how I could write it better.
Over the years I have read many articles and books about software development. There are many ink books (in books or online) that tell you how to improve programming and become a professionally trained programming master like a ninja! Most of these suggestions have something in common, one of which is to read the source code. However, compared to other suggestions, reading source code usually boils down to a simple sentence: find some great open source software, or any software you like, open it (or print it out) and read it. Although in general, this is indeed a good suggestion, it is shallow on paper, and there are many problems when you actually put it into practice. In this article, I will try to give some practical advice on reading source code, but before that, let's first enumerate the problems.
When people talk about reading source code, the general impression given to you is that they are like programming masters who can simply sit in a chair and read like a novel. Get started with the code. Well, I'm sure there are some superb programmers who can enjoy drinking coffee while looking at a bunch of mysterious symbols similar to English sentences, and can also build the entire class hierarchy and system in their minds. structure. Obviously this article is not for them. Its audience is people like me who feel that staring at a pile of source code is like looking at some boring and meaningless exercises. Of course, one could argue that one can learn from a complete project by looking at a single class or a single function bit by bit, but in my opinion, most software is internally dependent on each other except for the simplest problems. It's often impossible to understand the design ideas and principles behind a specific function or class without understanding the rest of the system.
The next question is where to get the source code that can be read (of course, before that, you have to be able to identify which source code is worth reading). There is a lot of excellent software, both open source software is available for free, and closed source software requires licensing. Open source repositories include Sourceforge and GitHub. If you work for a software development company, you have access to proprietary code in source code repositories. A third common approach is programs that accompany software development books, or are offered as educational resources (Minix is a prime example). Indeed, the multitude of options makes it difficult for us to choose, so finding the right one for us to read in the vast world of code is a difficult but essential task.
Another problem is the programming language used in the program. It is difficult enough to read other people's code. If you also need to become familiar with a new language mixed with weird syntax, the burden it brings will be too much for me. It seemed like a disaster that would bring great frustration. So you need to find code written in a language you are familiar with. But if the code you're looking at is from a book or provided as an educational resource, it doesn't matter if you know the new language because there are instructors who can explain the context. If you know that there are tigers in the mountains, but you prefer to read an unfamiliar programming language without the guidance of a book or a tutor, then I suggest that you at least need to learn it and reach the point where you can write your own program (Hello World It doesn’t count haha).
The previous question about context brings me to my next question, it is much harder to figure out what the code is doing if you are not familiar with the software itself. For example, if you don't use Linux every day and know the Linux boot sequence, it's hard to figure out what the runlevels are after looking at Linux code. The experience and knowledge gained from using a certain software can help us better read its source code, including commonly used terminology, functions and features of the software, and even the various errors you encounter.
For me, I realized that "reading the source code" does not accurately describe the activities I am engaged in. It would be more appropriate to use "understanding the source code" to express it. It's very difficult for me to sit in front of a laptop screen (or print it out on paper) and just read a screen full of code. I need other things besides code. For example, I like to look through the documentation, play with the software, step through the code and even write test code to run it, and then I can really appreciate it. Because I will invest so much time and energy into this, I have to be very selective and find software that I want to "read" (understand).
My first level of filtering is by programming language. For me, I only read the code of programs written in C#, VB.NET, Python and Javascript (although I am also familiar with C, Ruby and F#, but I don't think I have the level to understand other people's code). Next is looking for software I've used that will make me feel like I'm already on board because I know the intent of the code, what it can't do and what its limitations are (if I'm familiar enough with it). Open source software that I use every day is an excellent candidate (for example, I use the open source tools Cruise Control.NET, NANT, and NUnit written in C#)
I happen to work at a software products company (a Microsoft company), so one of the source code selections I read was our company's code in the source code repository. If you happen to also work for a software company, you can look at other projects or even earlier versions of the project you worked on. This way, in addition to gaining a deeper understanding of your code, you'll also get a good idea of what you've tried before and after. There are some caveats to note though:
Look for well-documented projects (this applies to open source as well as proprietary code). What I mean is that such documentation should highlight the overall design and explain the rationale behind the code. If it were simply an automatically generated Java Doc type document, it wouldn't be considered the documentation I'm describing :-). One way to find this is through software created for education (such as Minix). Since their purpose is to teach through software, they are usually very clearly documented and have a lot of material explaining the design principles behind the code.
So, now that you have identified the software you want to read the source code and downloaded its source code and documentation, let us read and understand it step by step:
"Source code reading" in my opinion is more than just reading, it is a unique set of activities that work together to help people understand the code. This may seem more intimidating than simply "reading code", but it's worth the effort.
Now, can you "read the source code" more easily and happily?
If you want to know more about programming learning, please pay attention to the php training column!
The above is the detailed content of How to read source code for beginners. For more information, please follow other related articles on the PHP Chinese website!