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 · 0 comments
Open

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

cssmagic opened this issue Feb 29, 2024 · 0 comments

Comments

@cssmagic
Copy link
Owner

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
  • Python 函数以及它们在软件设计中的作用
  • 适合由 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 sub-parts, 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’ll have been struggling with syntax in code that’s 5-10 lines long for about a month before we typically introduce functions and, 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 all 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, you’ll see us employ the central workflow of interacting with Copilot; specifically, the cycle of writing prompts, receiving code from Copilot, and testing to see if that code is correct. In the functions produced by Copilot, you’ll begin to see the core programming tools, like loops, conditionals, and lists, that we’ll expand on in the next chapter.

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

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

No branches or pull requests

1 participant