Home > Web Front-end > Front-end Q&A > How to read source code for beginners

How to read source code for beginners

coldplay.xixi
Release: 2020-09-18 18:01:47
forward
2461 people have browsed it

How to read source code for beginners

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.

Misunderstanding of reading source code

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.

Understanding the source code

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:

  • First, if you don’t have access to other projects, you need to ask for permission, as some companies take their “intellectual property” very seriously.
  • Secondly, the quality of these software may not be as high as you think, because typically, proprietary code has not undergone as rigorous a code walkthrough as open source code. It is important to note that without regular code reviews, the quality of the code may be poor.
  • Thirdly (this point is inspired by feedback provided by my friends), if your company develops business software (HR, Finance, ERP, etc.), there are many business relationships that need to be understood first . Also, since most code is influenced by business functionality factors, it is generally not as modular as an application or API.

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.

Summary

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:

  • Go through the design documentation and try to understand how the code is structured. Good software projects follow certain architectural patterns, which determine the organization of the code. Once you master this, understanding the code becomes much easier. If you can also draw class diagrams, you can better understand the overall layout.
  • The next thing to do is compile and run it. Depending on the project and its documentation, this may be easy or difficult.
  • Now it’s time to open your favorite IDE and start exploring. A good starting point for exploration is to try stepping through the code of a feature you are familiar with. This way, you can traverse the various layers and subsystems and understand how they are related. For example, when I explored NUnit, I first wrote a test case and then looked at the classes involved.
  • Try to identify the design patterns used in the code. If you still don’t know what design patterns are, stop reading this article immediately and read a classic book on design patterns. Become familiar with design patterns, they are a great way to identify and understand the design contained in good code. Once you become familiar with it, it will be easier to keep it in mind as you read the code. It can also help you more easily identify the subtle adjustments and magic changes made by the code author on the original design pattern.
  • Try writing test cases for your code to fully understand it, this is a very useful way to understand the dependencies between different parts of your code. Before writing test cases, you first need to meet all dependencies. Next, learn about the possible entry points and return values ​​of your code. This can improve your understanding of the code and help you get to the next level.
  • Finally, try to refactor the code. At this point, you've moved from simply understanding the code to becoming familiar enough to be able to modify it. As the complexity of the refactoring increases, so will your understanding. At this point, you can contribute your own code to the project if desired.

"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!

Related labels:
source:juejin.im
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template