Three realms of reading source code

Original http://mp.weixin.qq.com/s/PDhEKM2XG_qzOmBjWb-M7Q

Theming Design Patterns Java Programmer

"How do inexperienced and inexperienced junior programmers read project source code?"

"Has anyone read the source code of mybatis? Just looking at an initialization process makes me dizzy. Let's help you guys!"

"How to read the source code, I have tried to read some source code, such as the sqlparser part of alibaba's druid, spring-mvc, but found it very difficult, they said that debug is the best way to read, I often lost the phenomenon when debugging ...just walking around feels like I'm getting into some minutiae that I don't pay much attention to at the moment."

。。。。。。

It is estimated that many people have such doubts.

I can understand the pain of my friends very much, because I also came through in such pain.

Everyone knows the benefits of reading excellent source code. Learning from other people's excellent design, reasonable abstraction, and concise code... In short, there are many benefits.

But really putting a huge code in front of you is like a huge maze. It is really difficult to find a way through the east and west, figure out the whole structure of the maze, and understand the core idea.

When reading code written in object-oriented languages ​​such as Java, you will find that interfaces and specific implementations often do not correspond, and it is not clear how a function can be found in which implementation class. Unlike the C language, the function calls the function, which is relatively better.

If it is a dynamic language such as Ruby, Python, the type of a variable is not even easy to know, and the difficulty of reading is greatly increased.

There is another important reason. The source code we see now has basically been developed for several years and has been continuously improved by many people. There are many branches and branches, and the devil is in the details. It's easy to get caught up in reading. After reading dozens of layers of function calls, I'm completely stunned and give up: Don't care if you blow the source code into hype, I'll never read it again.

After a lot of painful struggles, I also have some successful experiences. Today, I will use the three realms of scholarship as an analogy to share with you:

Last night, the west wind withered the green trees, I climbed the tall buildings alone, looking at the end of the world

If you want to understand the source code and understand it thoroughly, you must first climb up and look into the distance, observe the path, clarify the goal and direction, and understand the overview of the source code.

So some preparations have to be done.

1. 阅读源码之前,需要有一定的技术储备。

比如设计模式,在很多Java源码中几乎就是标配,尤其是这几个:模板方法,单例,观察者,工厂方法,代理,策略,装饰者。

再比如阅读Spring源码,肯定得先了解IoC是怎么回事,AOP的实现方式,CGLib,Java动态代理等,自己动手,写点相关的代码,把这些知识点掌握了。

2. 必须得会使用这个框架/类库, 最好是精通各种各样的用法。

上面刚提过,魔鬼都在细节中,如果有些用法根本不知道,可能你能看明白代码是什么意思,但是不知道它为什么这些写。

3. 先去找书,找资料,了解这个软件的整体设计。

都有哪些模块? 模块之间是怎么关联的?怎么关联的?

可能一下子理解不了,但是要建立一个整体的概念,就像一个地图,防止你迷航。

在读源码的时候可以时不时看看自己在什么地方。

4. 搭建系统,把源代码跑起来!

相信我,Debug是非常非常重要的手段, 你想通过只看而不运行就把系统搞清楚,那是根本不可能的!

衣带渐宽终不悔,为伊消得人憔悴。

5. 根据你对系统的理解,设计几个主要的测试案例,定义好输入,输出。

运行系统,慢慢地debug ,一步步地走,这是个死功夫,没有办法绕过。

Debug一遍肯定是不行的,需要Debug很多遍。

第一遍尽可能抛弃细节,抓住主要流程, 比如有些看起来不重要的方法就不进去看了。

第二遍、第三遍....再去看那些细节。

一个非常重要的工作就是记笔记(又是写作!),画出系统的类图(不要依靠IDE给你生成的), 记录下主要的函数调用, 方便后续查看。

文档工作极为重要 ,因为代码太复杂,人的大脑容量也有限,记不住所有的细节。 文档可以帮助你记住关键点, 到时候可以回想起来,迅速地接着往下看。

要不然,你今天看的,可能到明天就忘个差不多了。

给大家看看我做的一些笔记, 格式不重要,很随意,方便自己看懂就行。

6. 主要的测试案例搞明白了,丰富测试案例,考虑一些分支流程。

继续Debug......

总之,静态地看代码 + 动态地debug (从业务的角度), 就会慢慢揭开这个黑暗森林的面纱。

这一步会非常非常地花费时间,但是你做完了,对系统的理解绝对有质的飞跃。

众里寻他千百度,蓦然回首,那人却在灯火阑珊处。

没有千百度的上下求索,不会有瞬间的顿悟和理解,衷心祝愿阅读源码的朋友们都能达到这一境界。

最后一点,也是最关键的一点: 要能坚持下去。

我不是一个聪明人, 但是笨人自有笨办法:什么事都架不住不断的重复,一遍看不明白,再来第二遍, 两遍搞不明白,再来第三遍......

可能有人要问: 你怎么能这么坚持地刨根问底呢?

答案就是好奇心: 这玩意儿到底是怎么实现的?!

欢迎加入我的知识星球“码农翻身”!

在那里我主要分享我是如何积累起现在的知识结构,学习过程的血和泪,职业发展的经验和教训,日常的所思所想,希望能帮助大家少走点儿弯路。

对于加入知识星球的同学提供 特别的福利 ,即2017编程提高群第一季和第二季的部分“加餐”视频 :

漫谈计算机组成原理和计算机编程语言

程序的机器级表示

漫谈操作系统之虚拟内存

进程和线程

新人在职场

Java并发编程

如何用Antlr实现自定义的简单脚本语言

缓存

BTree及其在数据库的应用

分布式文件系统FastDFS

如果想学习Java性能优化,工程化、高性能及分布式、深入浅出。微服务、Spring,MyBatis,Netty源码分析的朋友可以加下454377428群里有阿里大牛直播讲解技术,以及Java大型互联网技术的视频免费分享给大家。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326048120&siteId=291194637