`
- 浏览:
22237105 次
- 性别:
- 来自:
杭州
-
-
Try to build and run it
. Often this is a simple one step process, like when you're looking at work code (as opposed to random code
).
However this is not always the case and you can learn a lot about the
high level structure of the code from getting it to build and execute.
And, on the subject of work code, you are intimately familiar with how
to build your current project are you not? Builds are often complex, but
there is a lot of understanding to be gleaned from knowing how the
build does its thing to produce the executable bits
.
-
Don't focus on the details just yet
.
The first thing you want to do is get a bit of a feel for the structure
and style of the code you're reading. Start having a browse and try to
figure out what the various bits of the code are trying to do.
This will
familiarise you with the high level structure of the whole codebase as
well as give you some idea of the kind of code you're dealing with (well factored, spaghetti etc.
). This is the time where you want to find the entry point (whatever it happens to be, main function, servlet, controller etc.
)
and see how the code branches out from there. Don't spend too long on
this; it's a step you can come back to at any time as you gain more
familiarity with the code.
-
Make sure you understand all the constructs
.
Unless you happen to be the premier expert on your programming language
there are probably some things you didn't know it could do. As you're
having a high level fly-through of the code, note down any constructs
you may be unfamiliar with.
If there are a lot of these, your next step
is obvious. You're not going to get very far if you have no idea what
the code is doing syntactically.
Even if there are only a few constructs
you're unfamiliar with, it is probably a good idea to go look them up.
You're now discovering things about your language you didn't know
before, I am happy to trade a few hours of code reading just for that.
-
Now that you've got a good idea about most of the constructs, it is time to do a couple of random deep-dives
.
Just like in step 2, start flying though the code, but this time, pick
some random functions or classes and start looking through them line by
line. This is where the hard work really begins, but also where you will
start getting the major pay-offs. The idea here is to really get into
the mindset (the groove
) of the codebase you're looking at.
Once again don't spend too much time on this, but do try and deeply
absorb a few meaty chunks before moving on. This is another step to
which you can come back again and again with a bit more context and get
more out of it every time.
-
There were undoubtedly things
in the previous step you were confused about, so this is the perfect
time to go and read some tests
. You will potentially have a lot
less trouble following these and gain an understating of the code under
test at the same time. I am constantly surprised when developers ignore
a well-written and thorough test suite while trying to read and
understand some code. Of course, sometimes there are no tests.
-
No tests you say, sounds like the perfect time to write some
.
There are many benefits here, you're aiding your own understanding,
you're improving the codebase, you're writing code while reading it,
which is the best of both worlds and gives you something to do with your
hands. Even if there are tests already, you can always write some more
for your own benefit. Testing code often requires thinking about it a little differently
and concepts that were eluding you before can become clear.
-
Extract curious bits of code into standalone programs
.
I find this to be a fun exercise when reading code, even if just for a
change of pace. Even if you don't understand the low level details of
the code, you may have some idea of what the code is trying to do at a
high level. Why not extract that particular bit of functionality into a
separate program. It will make it easier to debug when you can execute a
small chunk by itself, which – in turn – may allow you to take that
extra step towards the understanding you've been looking for.
-
The code is dirty and smelly? Why not refactor it
.
I am not suggesting you rewrite the whole codebase, but refactoring
even small portions of the code can really take your understanding to
the next level. Start pulling out the functionality you do understand
into self-contained functions. Before you know it, the original monster
function is looking manageable and you can fit it in your head.
Refactoring allows you to make the code your own without having to
completely rewrite it. It helps to have good tests for this, but even if
you don't have that, just test as you go and only pull out
functionality you're sure of. Even if the tests seem totally inadequate –
learn to trust your own skill as a developer, sometimes you just need
to go for it (you can always revert if you have to
).
-
If nothing seems to help, get yourself a code reading buddy
.
You’re probably not the only person out there who can benefit from
reading this code, so go grab someone else and try reading it together.
Don't get an expert though, they'll just explain it all to you at a high
level and you will miss all the nuances that you can pick up by going
though the code yourself. However, if nothing works, and you just don't
get it, sometimes the best thing you can do is ask. Ask your co-workers
or if you're reading open source code, try and find someone on the
interwebs. But remember, this is the last step, not the first one.
分享到:
Global site tag (gtag.js) - Google Analytics
相关推荐
条件平差是大地测量、摄影测量以及工程测量等领域中常用的一种数据处理方法,它通过建立一组观测值与...如果你对代码的具体细节有疑问,或者需要进一步的理解,可以提供更详细的代码片段,以便进行深入的讨论和解析。
小学生阅读摘抄本.doc
《小学生阅读摘抄本》是为小学生设计的一种学习工具,旨在培养他们的阅读兴趣,提高语文素养,特别是词汇理解和写作能力。这份文档包含了每日阅读记录、好词好句摘录、仿写练习以及自我反思和总结,是小学生进行自主...
汉王屏幕摘抄精灵是一款专为Windows平台设计的高效工具,旨在帮助用户将屏幕上的文本信息快速、准确地转换成可编辑的电子文档。这款软件的出现极大地减轻了用户手动输入纸质文档到电脑中的繁琐工作,提升了工作效率...
经典IT文章摘抄经典IT文章摘抄经典IT文章摘抄经典IT文章摘抄经典IT文章摘抄
一年级经典句子摘抄大全阅读.doc
汉王屏幕摘抄精灵是一款强大的OCR(Optical Character Recognition,光学字符识别)软件,专为用户设计,能够高效地从图像、PDF文件中提取并转换文字。这款工具的核心功能在于其高精度的文字识别技术,使得用户能够...
【代码重构36计】是关于提升代码质量与可维护性的策略集合,主要针对C++编程语言。重构的核心目标是使程序更容易理解、更便于维护,并优化其结构。以下是其中的五项重构策略: 1. **参数列表对象化**: 当函数的...
高考议论文素材摘抄三则 高考议论文名人素材摘抄精选.docx
《初一摘抄好词好句大全》这份文档,正是为这一目的精心编纂的一份资源,它包含了众多能够激发学生学习兴趣、丰富写作素材、提升语言表达能力的优美词汇和句子。 首先,让我们来探讨一下文档中收录的一些“好词”。...
青铜葵花好词好句摘抄.doc
艾青诗选摘抄20篇.doc
美文摘抄600字.doc
这篇文档虽然标题为“值得阅读的英语经典美文摘抄.doc”,但实际上主要讲述了关于亲情的一段故事,通过一个孩子在考试后经历的情绪波动以及母亲的教诲,展现了母爱的力量和对个人成长的影响。这个故事可以提炼出以下...
星火英语美文听力4篇摘抄参考.doc
斯宾塞的快乐教育读书摘抄.docx
四年级作文摘抄20篇.doc
教师读书笔记摘抄及感悟.doc
通过阅读《千年一叹》,我们可以了解到作者如何通过对各地历史遗迹的探索,洞察历史的脉络,以及他对现代社会与传统文化碰撞的深刻思考。 在《千年一叹》中,余秋雨以独特的视角和深厚的历史底蕴,将历史与现实交织...
在这份“读书笔记摘抄大全”中,我们可以提炼出几个关于成功的关键词:自我发掘、独特性、内在品质与有效行动。 首先,成功不是一种单一的模式。每个人都有自己的特点和长处,不必过分模仿他人,而应该深入发掘自己...