Phát triển Thủ tục
Đã giới thiệu| English | Ý nghĩa |
|---|---|
| define/dɪˈfaɪn/ | xác định |
| parameters/pəˈræmɪtəz/ | tham số hình thức |
| procedural abstraction/prəˈsiːdʒərəl əbˈstrækʃn/ | trừu tượng hóa thủ tục |
| readability/ˌriːdəˈbɪlɪti/ | tính dễ đọc |
| duplication/ˌdjuːplɪˈkeɪʃn/ | sự trùng lặp |
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.
Để định nghĩa một thủ tục, bạn cần cung cấp cho nó:
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Ba phần này tạo thành một thủ tục có thể tái sử dụng.
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.

Decomposing a program into procedures and sub-procedures
Tiêu chí tốt cho một thủ tục?
Trích xuất thủ tục khi một đoạn công việc có một mục đích rõ ràng hoặc lặp lại ở nhiều nơi; một dòng làm riêng biệt độc đáo không cần thủ tục riêng.
Tại sao greet(name) lại dùng tham số?
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Người gọi cung cấp một tên khác nhau mỗi lần gọi.
Chia một vấn đề lớn thành các thủ tục nhỏ hơn, mỗi thủ tục thực hiện một công việc duy nhất, là ______ trừu tượng hóa.
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Trừu tượng hóa thủ tục giúp giữ cho các chương trình lớn dễ quản lý.
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.
Đặt đoạn mã lặp lại vào bên trong một thủ tục chủ yếu giúp giảm:
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Một thủ tục thay thế nhiều bản sao của cùng một dòng mã.
Với một thủ tục chào hỏi, việc thay đổi lời chào chỉ cần chỉnh sửa ở một nơi, không phải ba nơi.
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Ít nơi cần chỉnh sửa nghĩa là ít cơ hội xảy ra lỗi hơn.
Một thủ tục có tên rõ ràng như calculateTax() giúp người đọc hiểu ý định nhanh chóng.
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Tên gọi tốt cải thiện khả năng đọc và tái sử dụng.
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.
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).