Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[译] [300] 第三章 设计函数 #27

Open
cssmagic opened this issue Feb 29, 2024 · 1 comment
Open

[译] [300] 第三章 设计函数 #27

cssmagic opened this issue Feb 29, 2024 · 1 comment

Comments

@cssmagic
Copy link
Owner

cssmagic commented Feb 29, 2024

3 Designing functions

第三章 设计函数

This chapter covers

本章内容概要

  • Functions in Python and their role in designing software
  • Reasonable tasks for Copilot to solve
  • The standard workflow when interacting with Copilot
  • Examples of writing good functions using Copilot
  • 函数在软件设计中的作用
  • 适合交给 Copilot 解决的任务
  • 与 Copilot 交互的标准工作流程
  • 利用 Copilot 编写优秀函数的案例

One of the hardest challenges for programming novices is to know what a reasonable task is to give to Copilot so that it finds a good solution. If you give it too big a task, it will often fail in spectacular ways that can be extremely difficult to fix. What, then, is a reasonable task?

对编程初学者而言,识别出哪些任务适合交给 Copilot 并让它提出有效的解决方案,是一项巨大的挑战。如果交待给 Copilot 的任务过于庞大,它往往会失败得很夸张,而且这类失败往往也非常难以修正。那么,什么样的任务才算是合理的呢?

This question is important for our use of Copilot but goes far beyond it. Human programmers struggle with complexity, too. If experienced software engineers try to write code to solve a problem that’s too complex without breaking it down into smaller, more solvable subparts, they often have trouble as well. The solution for humans has been to use something called a function, whose job is to perform one task. There are various rules of thumb for how to write a reasonable function in terms of number of lines of code, but fundamentally these rules try to capture writing something that (1) performs a single task and (2) is not so complex that it’s hard to get right.

这个问题不仅对于使用 Copilot 至关重要,对于我们自身的编程之路同样意义深远。因为程序复杂度同样也是人类程序员头疼不已的问题。即使是经验丰富的软件工程师,如果试图一口气搞定一个复杂的大问题,而不是将其分解为更小、更易于解决的子问题,往往也会陷入困境。人类程序员最终找到“函数”作为解决方案,函数的职责就是完成一个特定任务。虽然有许多关于如何编写合理函数的经验法则,比如代码不能超过多少行之类,但总的来说,这些规则的根本目的是要确保所写的内容(1)仅执行一个单一任务,并且(2)其复杂度不至于难以正确完成。

For students who learned to program the old-fashioned way, without Copilot, they may have struggled with syntax in code that’s 5–10 lines long for about a month before we typically introduce them to functions. At that point, it’s a natural segue to tell them not to write more code in a single function than they can handle testing and debugging. As you are learning to work with Copilot rather than syntax directly, our job in this chapter is to teach you about functions and what are reasonable and unreasonable tasks to ask Copilot to solve in a single function.

对于那些未接触 Copilot、仅通过传统方式学习编程的学生,我们通常会留出一个月的时间,让他们充分感受 5~10 行长度的代码逻辑所带来的挑战和折磨,然后才会向他们介绍函数。在经历了这个过程之后,再向他们强调不要在单个函数中放入超过他们测试和排错能力上限的代码,往往就顺理成章了。鉴于你们正学习如何驾驭 Copilot 而不是直接处理语法,本章的使命是向你们介绍有关函数的知识,并且说清楚在单个函数中要求 Copilot 解决哪些任务是合理的,哪些是不合理的。

To help you gain perspective on functions, we’ll provide you with a number of examples in this chapter. For those examples, we’ll employ the central workflow of interacting with Copilot—specifically, the cycle of writing prompts, receiving code from Copilot, and testing to see whether that code is correct. In the functions produced by Copilot, you’ll begin to see the core programming tools, like loops, conditionals, and lists, which we’ll expand on in the next two chapters.

为了加深你对函数的理解,我们将在本章提供若干示例。在这些示例中,你将体验到与 Copilot 互动的核心流程——具体来说,就是编写提示词、从 Copilot 接收代码、测试代码是否正确的循环过程。通过 Copilot 创建的函数,你将初步了解像循环、条件语句和列表这样的核心编程工具,这些内容将在后面两章进一步探讨。

@cssmagic
Copy link
Owner Author

cssmagic commented Aug 8, 2024

👉 想要更多译稿预览,请前往 总目录 中按章节阅读。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant