Skip to content

Commit 82be2a0

Browse files
committed
fix: text style
1 parent 5297f37 commit 82be2a0

File tree

4 files changed

+367
-398
lines changed

4 files changed

+367
-398
lines changed

content/chapter-4/410.md

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ title: "[译] [XXX] XXXXXXXXXXXXXXXX"
44
---
55

66

7-
## 4.1 **Why we need to read code**
7+
## 4.1 Why we need to read code
88

99
When we talk about reading code, what we mean is being able to understand what code does by looking at it. There are two such levels of understanding.
1010

@@ -18,25 +18,28 @@ The second is being able to determine the overall purpose of a program. As profe
1818

1919
At the end of these two chapters, we’ll want you to be able to do both at the level of being able to interpret the code produced by Copilot. We’ll start focusing on that line-by-line understanding but toward the end, you’ll start being able to look at a small chunk of code and determine its purpose.
2020

21-
**Listing 4.1 Best Word function for Scrabble, reprinted from Chapter 3**
22-
23-
def best\_word(word\_list): """
24-
25-
word\_list is a list of words.
26-
27-
Return the word worth the most points. """
28-
29-
best\_word = "" best\_points \= 0
30-
31-
for word in word\_list: points \= num\_points(word) if points > best\_points:
32-
33-
best\_word = word best\_points \= points
34-
35-
return best\_word
36-
37-
We can illustrate the difference between these two levels of reading code by referring back to our best\_word function from Chapter 3, reprinted in listing
38-
39-
4.1 above. A **tracing description** of what this program does would be a description of each line. For example, we would say that we're defining a function called best\_word. We have a variable called best\_word that we start off as a string with no characters, otherwise known as the empty string. (It’s unfortunate that the function and this variable are both called \`best\_word\`, because it makes it trickier to refer to one or the other, but that’s what Copilot gave us.) We also have another variable best\_points that we start at 0. Then we have a for loop over each word in the word\_list. Inside the for loop, we call our num\_points helper function... and so on. (We’ll be explaining how we know what each line of code does over the next two chapters!).
21+
We can illustrate the difference between these two levels of reading code by referring back to our `best\_word` function from Chapter 3, reprinted in the following listing.
22+
23+
> Listing 4.1 Best Word function for Scrabble, reprinted from Chapter 3
24+
25+
```python
26+
def best_word(word_list):
27+
"""
28+
word_list is a list of words.
29+
30+
Return the word worth the most points.
31+
"""
32+
best_word = ""
33+
best_points = 0
34+
for word in word_list:
35+
points = num_points(word)
36+
if points > best_points:
37+
best_word = word
38+
best_points = points
39+
return best_word
40+
```
41+
42+
A **tracing description** of what this program does would be a description of each line. For example, we would say that we're defining a function called `best\_word`. We have a variable called `best\_word` that we start off as a string with no characters, otherwise known as the empty string. (It’s unfortunate that the function and this variable are both called `best\_word`, because it makes it trickier to refer to one or the other, but that’s what Copilot gave us.) We also have another variable `best\_points` that we start at 0. Then we have a for loop over each word in the `word\_list`. Inside the for loop, we call our `num\_points` helper function... and so on. (We’ll be explaining how we know what each line of code does over the next two chapters!).
4043

4144

4245

@@ -50,12 +53,6 @@ There are three reasons why we want you to be able to read code.
5053

5154
2. **Informing testing.** Understanding what the code is doing line by line is useful on its own, but it also helps turbocharge your ability to effectively test. For example, in the next chapter, you'll learn about loops; that they can cause your code to repeat zero times, one time, two times, or as many times as needed. You'll be able to combine that knowledge with what you already know about testing to help you identify important categories of cases to test.
5255

53-
3. **Helping you write code.** We know—you want Copilot to write all of your code! We want that, too. But inevitably, there will be code that Copilot just doesn't get right, no matter how much prompt engineering you do. Or maybe enough prompt engineering could finally cajole
54-
55-
56-
57-
Copilot to write the correct code, but it would be simpler and faster to just do it ourselves. In writing this book, the two of us strive to have Copilot write as much code as possible. But, because of our knowledge of Python programming, we are often able to see a mistake and just fix it without going through any hoops to have Copilot fix it for us. More
58-
59-
long-term, we want you to be empowered to learn more programming on your own, and having an understanding of Python is our way to bridge you from this book to other resources later. There is research evidence that being able to trace and explain code is prerequisite to being able to write code \[1\].
56+
3. **Helping you write code.** We know—you want Copilot to write all of your code! We want that, too. But inevitably, there will be code that Copilot just doesn't get right, no matter how much prompt engineering you do. Or maybe enough prompt engineering could finally cajole Copilot to write the correct code, but it would be simpler and faster to just do it ourselves. In writing this book, the two of us strive to have Copilot write as much code as possible. But, because of our knowledge of Python programming, we are often able to see a mistake and just fix it without going through any hoops to have Copilot fix it for us. More long-term, we want you to be empowered to learn more programming on your own, and having an understanding of Python is our way to bridge you from this book to other resources later. There is research evidence that being able to trace and explain code is prerequisite to being able to write code \[1\].
6057

6158
Before we get to it, we need to be clear about the level of depth that we're striving for. We're not going to teach you every nuance of every line of code. Doing so would revert us back to the traditional way that programming was taught prior to tools like Copilot. Rather, through a combination of Copilot tools and our own explanations, we're going to help you understand the gist or overall goal of each line of code. You will need more than this if you endeavor to write large portions of programs in the future. We are trying to target the sweet spot between "this code is magic" and "I know exactly how every line of the code works."

content/chapter-4/420.md

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ title: "[译] [XXX] XXXXXXXXXXXXXXXX"
44
---
55

66

7-
## 4.2 **Asking Copilot to explain code**
7+
## 4.2 Asking Copilot to explain code
88

99
In Chapter 2, when setting up your computer to use GitHub Copilot, you installed the GitHub Copilot Labs extension to Visual Studio Code. This is an experimental extension that is changing rapidly and is designed to offer new features that are not quite ready for everyday use. We're going to show you one of its best features right now: explaining what Python code does!
1010

1111
We suspect that, soon, the Copilot Labs extension, or parts of it, will be folded into the main Copilot extension. If that happens, the specific steps we give here may vary somewhat, and in that case, we encourage you to consult more general GitHub Copilot documentation.
1212

13-
For now, with the Copilot Labs extension installed, you can highlight some code that you want Copilot to describe to you. Let's try this with our best\_word function.
13+
For now, with the Copilot Labs extension installed, you can highlight some code that you want Copilot to describe to you. Let's try this with our `best\_word` function.
1414

1515

1616

@@ -24,56 +24,49 @@ For now, with the Copilot Labs extension installed, you can highlight some code
2424

2525
First, click on the Copilot Labs tab in your Activity Bar (on the left-hand side of VSCode) and you should see a window similar to figure 4.1.
2626

27-
> Figure 4.2 The code from the best\_word function highlighted in the editor.
28-
29-
![](chapter-4.files/chapter-48299.png)
30-
3127

3228

3329
Next, highlight all of the code for our best\_word function as is highlighted in figure 4.2. (You may need to have Copilot generate the code for you again if you didn't save it from Chapter 3.)
3430

31+
> Figure 4.2 The code from the `best\_word` function highlighted in the editor.
32+
33+
![](chapter-4.files/chapter-48299.png)
34+
35+
After highlighting the code, you should now see the code appear in the left in the EXPLAIN feature as shown in figure 4.3
36+
3537
> Figure 4.3 The code from the best\_word function appearing in Copilot Labs.
3638
3739

3840

3941
![](chapter-4.files/chapter-48572.png)
4042

41-
After highlighting the code, you should now see the code appear in the left in the EXPLAIN feature as shown in figure 4.3
43+
44+
45+
Figure 4.4 shows the different prompts provided by Copilot that you can use to ask for code explanations. Each will generally yield different responses that vary in how specific they are and whether they produce fewer or more examples. We'll leave it at the default prompt of “Explain Code,” but if you like, you can try other prompts from the drop-down box (shown in figure 4.4). The current options are "Explain Code," "Code Does Following," "Code Does Following (English)," and "Show Example Code." There's also a "Custom" option that allows you to use whatever prompt you like.
4246

4347
> Figure 4.4 The different options for explaining your code in Copilot Labs.
4448
4549

4650

4751
![](chapter-4.files/chapter-48774.png)
4852

49-
50-
51-
Figure 4.4 shows the different prompts provided by Copilot that you can use to ask for code explanations. Each will generally yield different responses that vary in how specific they are and whether they produce fewer or more examples. We'll leave it at the default prompt of “Explain Code,” but if you like, you can try other prompts from the drop-down box (shown in figure 4.4). The current options are "Explain Code," "Code Does Following," "Code Does Following (English)," and "Show Example Code." There's also a "Custom" option that allows you to use whatever prompt you like.
52-
5353
The last step is to click Ask Copilot (as shown as the bottom button in figure 4.3). You'll get an explanation of the code. As usual, Copilot is nondeterministic, so your results will likely differ from ours. In fact, if an explanation is confusing you, you might try clicking Ask Copilot again or changing the prompt to get a different explanation. As educators, this is the start of a dream come true, where students will be able to ask for as many explanations as they need to fully understand how code works.
5454

55-
**COPILOT lab explanations can be wrong**
56-
57-
As with the code produced by Copilot, the explanations from Copilot Labs can be wrong too. You might be thinking, “Wait, what’s the value of a tool to understand code if it can be wrong?!” First, let us say that we agree, this can be really frustrating. However, it is right often enough in our experience that it can be really helpful, particularly for understanding code basics like the things we’re teaching in these two chapters. Moreover, what students do now when they are confused about their code is to ask friends or websites about the code and, candidly, those make mistakes too. A way to help mitigate the potential for error is to ask Copilot for its explanation a couple times as you are less likely to get grossly incorrect answers across multiple tries.
55+
> **Copilot lab explanations can be wrong**
56+
>
57+
> As with the code produced by Copilot, the explanations from Copilot Labs can be wrong too. You might be thinking, “Wait, what’s the value of a tool to understand code if it can be wrong?!” First, let us say that we agree, this can be really frustrating. However, it is right often enough in our experience that it can be really helpful, particularly for understanding code basics like the things we’re teaching in these two chapters. Moreover, what students do now when they are confused about their code is to ask friends or websites about the code and, candidly, those make mistakes too. A way to help mitigate the potential for error is to ask Copilot for its explanation a couple of times as you are less likely to get grossly incorrect answers across multiple tries.
5858
5959
Here's what Copilot gave us the first time we asked:
6060

61+
```python
6162
""" Here is the explanation for the code above:
62-
63-
1. We define a function called best\_word that takes one argument,
64-
65-
2. We define the variables best\_word and best\_points and set them
66-
67-
3. We use a for loop to iterate over the elements in word\_list.
68-
69-
4. We define a variable called points that is equal to the result
70-
71-
5. We use an if statement to check if points is greater than best
72-
73-
6. If it is, we set best\_word equal to word and best\_points equal
74-
75-
76-
77-
7. Finally, we return best\_word. """
63+
1. We define a function called best_word that takes one argument, word_list.
64+
2. We define the variables best_word and best_points and set them equal to the empty string and 0, respectively.
65+
3. We use a for loop to iterate over the elements in word_list.
66+
4. We define a variable called points that is equal to the result of calling num_points with word as an argument.
67+
5. We use an if statement to check if points is greater than best_points.
68+
6. If it is, we set best_word equal to word and best_points equal to points.
69+
7. Finally, we return best_word. """
70+
```
7871

7972
To us, this is an excellent description of what the code does line by line. Notice, however, in order to make full sense of it, you need to know important Python terminology and features. The explanation from Copilot above refers to functions, arguments, variables, strings, for loops, if statements, and returning. You know much of this terminology already, but if you did not, then this explanation would be much less useful. By the end of this and the next chapter, you will know the main terminology and features of Python that you need to interpret a typical explanation from Copilot.

0 commit comments

Comments
 (0)