Developing Procedures · 开发过程
| English | 中文 | Pinyin · 拼音 |
|---|---|---|
| define/dɪˈfaɪn/ | 定义 | dìng yì |
| parameters/pəˈræmɪtəz/ | 形参 | xíng cān |
| procedural abstraction/prəˈsiːdʒərəl əbˈstrækʃn/ | 过程抽象 | guò chéng chōu xiàng |
| readability/ˌriːdəˈbɪlɪti/ | 可读性 | kě dú xìng |
| duplication/ˌdjuːplɪˈkeɪʃn/ | 重复代码 | chóng fù dài mǎ |
Writing your own procedures
- Writing your own procedures is a core programming skill.
- To define 定义 a procedure, give it a clear name, its parameters, and a body of instructions.
- Then you can call it wherever you need that job done.
- A good procedure does one clear thing.
编写你自己的过程
- 编写你自己的过程是一项核心编程技能。
- 要定义(define)一个过程,给它一个清晰的名字、它的形参和一个指令主体。
- 然后你能在任何需要那个任务完成的地方调用它。
- 一个好的过程做一件清晰的事。
To define a procedure you give it: · 要定义一个过程,你给它:
Those three parts make a reusable procedure. · 这三部分构成一个可重用的过程。
Parameters give flexibility
- Use parameters 形参 so a procedure can work with different input values.
greet(name)can greet anyone, because the caller supplies the name each time.- Without parameters, you would need a separate procedure per name.
- Parameters are what make one procedure reusable across many cases.
形参给出灵活性
- 用形参(parameters),让一个过程能处理不同的输入值。
greet(name)能问候任何人,因为调用者每次提供名字。- 没有形参,你会需要每个名字一个单独的过程。
- 形参是让一个过程跨许多情况可重用的东西。
Good candidate for a procedure? · 适合做成一个过程吗?
Extract a procedure when a chunk of work has one clear purpose or repeats in several places; a unique one-off line does not need its own procedure. · 当一块工作有一个清晰的目的或在几个地方重复时,提取一个过程;一个独特的一次性行不需要它自己的过程。
Why does greet(name) use a parameter? · 为什么 greet(name) 用一个形参?
The caller supplies a different name each call. · 调用者每次调用提供不同的名字。
Breaking a large problem into smaller procedures, each with one job, is ______ abstraction. · 把一个大问题分解成更小的过程,每个有一个任务,是______抽象。
Procedural abstraction keeps big programs manageable. · 过程抽象让大程序可管理。
Procedural abstraction
- Procedures let you apply procedural abstraction 过程抽象 — breaking a large problem into smaller, manageable parts.
- Each part becomes a procedure with one clear job.
- You reason about the small parts, not the whole tangled program at once.
- This is how large programs stay understandable.
过程抽象
- 过程让你应用过程抽象(procedural abstraction)——把一个大问题分解成更小、可管理的部分。
- 每个部分成为一个有一个清晰任务的过程。
- 你对小部分推理,而非一次面对整个纠缠的程序。
- 这是大程序保持可理解的方式。
Putting repeated code inside one procedure mainly reduces: · 把重复的代码放进一个过程主要减少:
One procedure replaces many copies of the same lines. · 一个过程取代同样代码行的许多副本。
With a greet procedure, changing the greeting means editing one place, not three. · 有一个 greet 过程,改问候语意味着改一个地方,而非三个。
Fewer places to edit means fewer chances for errors. · 更少的编辑处意味着更少的出错机会。
A well-named procedure like calculateTax() helps a reader understand the intent quickly. · 一个命名良好的过程如 calculateTax() 帮助读者快速理解意图。
Good names improve readability and reuse. · 好的名字改善可读性和重用。
Two big benefits
- Well-named procedures improve readability 可读性 and reuse: a reader sees
calculateTax()and gets the intent. - Putting repeated code in a procedure reduces duplication 重复代码.
Remove duplication. A program prints the same three-line greeting in three places — that is duplication. Define one greet(name) with those lines; now each place is a single call: greet("Ana"), greet("Bo"), greet("Cy"). Change the greeting once, not three times.
两大好处
- 命名良好的过程改善可读性(readability)和重用:读者看到
calculateTax()就明白意图。 - 把重复的代码放进一个过程减少重复代码(duplication)。
去除重复。 一个程序在三个地方打印同样的三行问候语——那是重复。定义一个 greet(name) 含那些行;现在每个地方是一个单独的调用:greet("Ana")、greet("Bo")、greet("Cy")。改问候语一次,而非三次。
To define a procedure, give it a name, parameters, and a body. Parameters let one procedure handle many inputs (greet(name)). This is procedural abstraction — split a problem into small jobs. The payoff: better readability and less duplication (edit one procedure, not many copies).
要定义一个过程,给它一个名字、形参和一个主体。形参让一个过程处理许多输入(greet(name))。这是过程抽象——把一个问题分成小任务。回报:更好的可读性和更少的重复代码(改一个过程,而非许多副本)。