search
HomeWeb Front-endFront-end Q&AHow to read source code for beginners

How to read source code for beginners

Sep 17, 2020 pm 05:38 PM
Source code

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!

Statement
This article is reproduced at:juejin. If there is any infringement, please contact admin@php.cn delete
Frontend Development with React: Advantages and TechniquesFrontend Development with React: Advantages and TechniquesApr 17, 2025 am 12:25 AM

The advantages of React are its flexibility and efficiency, which are reflected in: 1) Component-based design improves code reusability; 2) Virtual DOM technology optimizes performance, especially when handling large amounts of data updates; 3) The rich ecosystem provides a large number of third-party libraries and tools. By understanding how React works and uses examples, you can master its core concepts and best practices to build an efficient, maintainable user interface.

React vs. Other Frameworks: Comparing and Contrasting OptionsReact vs. Other Frameworks: Comparing and Contrasting OptionsApr 17, 2025 am 12:23 AM

React is a JavaScript library for building user interfaces, suitable for large and complex applications. 1. The core of React is componentization and virtual DOM, which improves UI rendering performance. 2. Compared with Vue, React is more flexible but has a steep learning curve, which is suitable for large projects. 3. Compared with Angular, React is lighter, dependent on the community ecology, and suitable for projects that require flexibility.

Demystifying React in HTML: How It All WorksDemystifying React in HTML: How It All WorksApr 17, 2025 am 12:21 AM

React operates in HTML via virtual DOM. 1) React uses JSX syntax to write HTML-like structures. 2) Virtual DOM management UI update, efficient rendering through Diffing algorithm. 3) Use ReactDOM.render() to render the component to the real DOM. 4) Optimization and best practices include using React.memo and component splitting to improve performance and maintainability.

React in Action: Examples of Real-World ApplicationsReact in Action: Examples of Real-World ApplicationsApr 17, 2025 am 12:20 AM

React is widely used in e-commerce, social media and data visualization. 1) E-commerce platforms use React to build shopping cart components, use useState to manage state, onClick to process events, and map function to render lists. 2) Social media applications interact with the API through useEffect to display dynamic content. 3) Data visualization uses react-chartjs-2 library to render charts, and component design is easy to embed applications.

Frontend Architecture with React: Best PracticesFrontend Architecture with React: Best PracticesApr 17, 2025 am 12:10 AM

Best practices for React front-end architecture include: 1. Component design and reuse: design a single responsibility, easy to understand and test components to achieve high reuse. 2. State management: Use useState, useReducer, ContextAPI or Redux/MobX to manage state to avoid excessive complexity. 3. Performance optimization: Optimize performance through React.memo, useCallback, useMemo and other methods to find the balance point. 4. Code organization and modularity: Organize code according to functional modules to improve manageability and maintainability. 5. Testing and Quality Assurance: Testing with Jest and ReactTestingLibrary to ensure the quality and reliability of the code

React Inside HTML: Integrating JavaScript for Dynamic Web PagesReact Inside HTML: Integrating JavaScript for Dynamic Web PagesApr 16, 2025 am 12:06 AM

To integrate React into HTML, follow these steps: 1. Introduce React and ReactDOM in HTML files. 2. Define a React component. 3. Render the component into HTML elements using ReactDOM. Through these steps, static HTML pages can be transformed into dynamic, interactive experiences.

The Benefits of React: Performance, Reusability, and MoreThe Benefits of React: Performance, Reusability, and MoreApr 15, 2025 am 12:05 AM

React’s popularity includes its performance optimization, component reuse and a rich ecosystem. 1. Performance optimization achieves efficient updates through virtual DOM and diffing mechanisms. 2. Component Reuse Reduces duplicate code by reusable components. 3. Rich ecosystem and one-way data flow enhance the development experience.

React: Creating Dynamic and Interactive User InterfacesReact: Creating Dynamic and Interactive User InterfacesApr 14, 2025 am 12:08 AM

React is the tool of choice for building dynamic and interactive user interfaces. 1) Componentization and JSX make UI splitting and reusing simple. 2) State management is implemented through the useState hook to trigger UI updates. 3) The event processing mechanism responds to user interaction and improves user experience.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.