Skip to content

Commit 4fbeade

Browse files
lecoursenheiskr
andauthored
Make the example in "Reusing other people's code..." more explicitly instructional (#54519)
Co-authored-by: Kevin Heis <heiskr@users.noreply.github.com>
1 parent 1349649 commit 4fbeade

File tree

1 file changed

+59
-20
lines changed

1 file changed

+59
-20
lines changed

content/get-started/learning-to-code/reusing-other-peoples-code-in-your-projects.md

Lines changed: 59 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,21 @@ allowTitleToDifferFromFilename: true
88
---
99

1010
One of the best things about open source software is the ability to reuse other people's code. Repurposing code helps you save time, discover new functionality, and learn other programming styles. There are two main ways to reuse code:
11+
1112
* **Copying and pasting a code snippet directly into your project.** If you're new to coding, this is the quickest way to start reusing code.
1213
* **Importing a library into your project.** While this approach takes some time to learn, it's ultimately easier and more efficient. It's also a foundational skill for software development.
1314

15+
In this article, we'll learn both by working through an example: reusing Python code that calculates the factorial of a number.
16+
1417
## Using other people's code snippets in your project
1518

16-
As you are learning to code, you might copy and paste other people's code snippets into your project. While using code snippets is a great way to learn to code and save time, there are a few key steps you should always take before copying another developer's code.
19+
When you're first learning to code, you might start with reuse by copying and pasting other people's code snippets into your project. It's a great way to save time, but there are a few key steps you should always take before copying another developer's code.
1720

1821
### 1. Finding and understanding a code snippet
1922

20-
Your first step is to choose a code snippet you want to reuse. To find eligible code, try [searching {% data variables.product.github %}](https://github.com/search). You can narrow your search with search qualifiers like `language:YOUR-SEARCH-LANGUAGE`.
23+
First, you need to find and understand the code snippet you want to reuse. For this example, we'll choose the [`new2code/python-factorial`](https://github.com/new2code/python-factorial) repository.
2124

22-
For example, let's say you want to reuse Python code that calculates the factorial of a number. By searching {% data variables.product.github %} for `factorial language:python is:repository`, we find the [`new2code/python-factorial`](https://github.com/new2code/python-factorial) repository, which implements the calculator in [`factorial_finder.py`](https://github.com/new2code/python-factorial/blob/main/factorial_finder.py) using a loop:
25+
First, **open** [`factorial_finder.py`](https://github.com/new2code/python-factorial/blob/main/factorial_finder.py), which implements the calculator using a loop:
2326

2427
```python
2528
# Initialize the factorial result to 1
@@ -35,29 +38,41 @@ for i in range(1, number + 1):
3538
print(f"The factorial of {number} is {factorial}")
3639
```
3740

38-
If you don't immediately understand a code snippet, you can ask {% data variables.product.prodname_copilot_short %} to explain it. In the menu bar at the top of the [`factorial_finder.py`](https://github.com/new2code/python-factorial/blob/main/factorial_finder.py) file, click {% octicon "copilot" aria-label="Ask Copilot about this file" %} to start a conversation about the file, then ask {% data variables.product.prodname_copilot_short %} to `Explain this program`.
41+
Then, in the menu bar at the top of the file, click {% octicon "copilot" aria-label="Ask Copilot about this file" %} to start a conversation with {% data variables.product.prodname_copilot_short %}.
42+
43+
![Screenshot of the {% data variables.product.prodname_copilot_short %} button, outlined in dark orange, at the top of the file view.](/assets/images/help/copilot/factorial-finder-copilot-button.png)
3944

40-
![Screenshot of the {% data variables.product.prodname_copilot_short %} button, outlined in dark orange, at the top of the file view.](/assets/images/help/copilot/factorial-finder-copilot-button.png)
45+
In the chat window, ask {% data variables.product.prodname_copilot_short %}:
46+
47+
```text copy
48+
Explain this program.
49+
```
4150

4251
### 2. Understanding project licensing
4352

4453
Before you can reuse the code you've found, you need to understand its licensing. Licenses determine how you can use the code in a project, including your ability to copy, modify, and distribute that code.
4554

46-
You can find the license for a project in the "About" section of the main page of the repository. For example, we can see that the [`new2code/python-factorial`](https://github.com/new2code/python-factorial) repository is licensed under the MIT license. To read the license, click {% octicon "law" aria-hidden="true" %} **MIT license**.
55+
To identify the license for [new2code/python-factorial](https://github.com/new2code/python-factorial), locate the "About" section on the repository's main page. There, you'll see that the repository is licensed under the MIT license. To read the license, click {% octicon "law" aria-hidden="true" %} **MIT license**.
4756

4857
![Screenshot of the main page of the new2code/python-factorial repository. In the right sidebar, "MIT license" is outlined in dark orange.](/assets/images/help/repository/license-info-python-factorial.png)
4958

50-
> [!NOTE] If the code you want to use is under a different license, use the license summaries in [Licenses](https://choosealicense.com/licenses/) to understand it.
51-
5259
We want to copy the entire `factorial_finder.py` file, so the MIT license indicates that we should include a copy of the license in our own project. At the top of your Python file, paste the license as a comment.
5360

61+
> [!TIP] You can learn what's allowed by other common licenses with the [Choose a license](https://choosealicense.com/licenses/) tool.
62+
5463
### 3. Using and modifying code snippets
5564

56-
Now that you understand the code and its licensing, you can paste the code snippet into your project. While you may be able to use the code as is, you'll often need to modify it for your specific use case.
65+
Now, you're ready to paste the code snippet into your project. While you'll sometimes to be able to use code snippets as they are, you will often want to **modify** them for your specific use case. Let's practice that now!
66+
67+
Let's say we want to quickly calculate the factorials of 5, 7, 9, and 10. Instead of copying and pasting the entire program for each number, we can move our calculator into a **function** that takes a number as an argument.
5768

58-
For our factorial example, let's say we want to quickly calculate the factorials of 5, 7, 9, and 10. Instead of copying and pasting the entire program for each number, we can move our calculator into a function that takes a number as an argument.
69+
Use [{% data variables.product.prodname_copilot_chat_short %}](https://github.com/copilot) to suggest and explain an implementation. Paste our current code into the chat window, followed by this prompt:
5970

60-
To help us make these changes, let's [ask {% data variables.product.prodname_copilot_short %}](https://github.com/copilot) to suggest and explain an implementation. We can paste our current code into the chat window, then prompt {% data variables.product.prodname_copilot_short %} to `Wrap the Python code above in a function`. {% data variables.product.prodname_copilot_short %} will generate code that looks something like this:
71+
```text copy
72+
Wrap the Python code above in a function.
73+
```
74+
75+
{% data variables.product.prodname_copilot_short %} will generate code that looks something like this:
6176

6277
```python copy
6378
def calculate_factorial(number):
@@ -84,7 +99,7 @@ Congratulations! You've successfully found, understood, and modified an example
8499

85100
## Using code from libraries in your project
86101

87-
Using libraries is standard practice for developers. Libraries are essentially collections of code written by other developers to perform specific tasks. You can import libraries into your project to use the pre-written code, saving you time and effort.
102+
Now, let's learn how to use libraries, which is **standard practice** for developers. Libraries are essentially collections of code written by other developers to perform specific tasks. You can import libraries into your project to use the pre-written code, saving you time and effort.
88103

89104
In this section, we'll continue working with the Python factorial calculator example from the previous section. For reference, here's our current code:
90105

@@ -109,31 +124,51 @@ print(calculate_factorial(10))
109124

110125
Once you know what functionality you want to add to your project, you can search for a library with relevant code. {% data variables.product.prodname_copilot_chat_short %} is an easy way to search for libraries, since you can use natural language to describe exactly what you're looking for.
111126

112-
Returning to our factorial calculator, finding a factorial is a pretty common function, and there's a good chance someone included that function in an existing library. If we [ask {% data variables.product.prodname_copilot_short %}](https://github.com/copilot) `Is there a Python library with a function for calculating a factorial?`, {% data variables.product.prodname_copilot_short %} will tell us a factorial function is included in the [`math`](https://docs.python.org/3/library/math.html) module from the standard Python library.
127+
Finding a factorial is a pretty common function, and there's a good chance someone included that function in an existing library. Open [{% data variables.product.prodname_copilot_chat_short %}](https://github.com/copilot), then ask:
128+
129+
```text copy
130+
Is there a Python library with a function for calculating a factorial?
131+
```
132+
133+
{% data variables.product.prodname_copilot_short %} will tell us a factorial function is included in the [`math`](https://docs.python.org/3/library/math.html) module from the standard Python library.
113134

114135
### 2. Prioritizing security in your project
115136

116-
When you add a library or module to your project, you create what's called a dependency. Dependencies are pre-written code bundles that your project relies on to function correctly, and if they aren't carefully written or maintained, they can introduce security vulnerabilities to your work. Thankfully, there are some steps you can take to best protect your project.
137+
When you add a library or module to your project, you create what's called a **dependency**. Dependencies are pre-written code bundles that your project relies on to function correctly. If they aren't carefully written or maintained, they can introduce security vulnerabilities to your work.
138+
139+
Thankfully, there are some steps you can take to best protect your project. Let's practice them now.
117140

118141
#### Using popular libraries
119142

120-
Popular libraries are more likely to be secure because they are actively maintained and used by many developers. One good marker of popularity is the number of stars a repository has. If you can't find the {% data variables.product.github %} repository for a dependency, try [asking {% data variables.product.prodname_copilot_short %}](https://github.com/copilot) for help.
143+
Popular libraries are more likely to be secure, because they are actively maintained and used by many developers. One good marker of popularity is the number of **stars** a repository has. If you can't find the {% data variables.product.github %} repository for a dependency, you can ask {% data variables.product.prodname_copilot_short %} for help.
144+
145+
Open [{% data variables.product.prodname_copilot_chat_short %}](https://github.com/copilot), then ask:
146+
147+
```text copy
148+
Find the GitHub repository containing the code for the math module in Python.
149+
```
121150

122-
For example, for the `math` module, we can prompt {% data variables.product.prodname_copilot_short %} to `Find the GitHub repository containing the code for the math module in Python`. {% data variables.product.prodname_copilot_short %} will tell us that the `math` module is defined in [`python/cpython`](https://github.com/python/cpython), which has over 64,000 stars.
151+
{% data variables.product.prodname_copilot_short %} will tell you that the `math` module is defined in [`python/cpython`](https://github.com/python/cpython), which has over 64,000 stars.
123152

124153
#### Enabling {% data variables.product.prodname_dependabot_alerts %} for your project
125154

126-
If enabled, {% data variables.product.prodname_dependabot_alerts %} are automatically generated when {% data variables.product.prodname_dependabot %} detects a security issue in your dependencies, helping you quickly fix vulnerabilities. {% data variables.product.prodname_dependabot %} is available for free on all open source {% data variables.product.github %} repositories.
155+
When enabled, {% data variables.product.prodname_dependabot_alerts %} are automatically generated when {% data variables.product.prodname_dependabot %} detects a security issue in your dependencies, helping you quickly fix vulnerabilities. {% data variables.product.prodname_dependabot %} is available for **free** on all open source {% data variables.product.github %} repositories.
127156

128-
To turn {% data variables.product.prodname_dependabot_alerts %} on, click the **Security** tab for your project's {% data variables.product.github %} repository. Next to {% data variables.product.prodname_dependabot_alerts %}, click **Enable {% data variables.product.prodname_dependabot_alerts %}**. You can access {% data variables.product.prodname_dependabot_alerts %} from the **{% data variables.product.prodname_dependabot %}** tab of the sidebar.
157+
Turn {% data variables.product.prodname_dependabot_alerts %} on for your repository now. Click the **Security** tab for your project's {% data variables.product.github %} repository. Next to {% data variables.product.prodname_dependabot_alerts %}, click **Enable {% data variables.product.prodname_dependabot_alerts %}**. You can access {% data variables.product.prodname_dependabot_alerts %} from the **{% data variables.product.prodname_dependabot %}** tab of the sidebar.
129158

130159
![Screenshot of the "Security" page of a repository. The "Security" tab, "{% data variables.product.prodname_dependabot %}" tab, and "Enable {% data variables.product.prodname_dependabot_alerts %}" button are all outlined in dark orange.](/assets/images/help/dependabot/learners-enable-dependabot.png)
131160

132161
### 3. Implementing code from a library
133162

134-
Now that you have taken steps to secure your work, you need to import the library into your project, then use its contents in your code. You can read the documentation for the library to learn how to do it yourself, or you can ask {% data variables.product.prodname_copilot_short %} to suggest and explain an implementation for you.
163+
Now you're ready to import the library into your project, then use its contents in your code. You can read the documentation for the library to learn how to do it yourself, or you can ask {% data variables.product.prodname_copilot_short %} to suggest and explain an implementation for you.
135164

136-
For our factorial program, we can [ask {% data variables.product.prodname_copilot_short %}](https://github.com/copilot) something like `How do I use the factorial function of the math module in my Python project?` {% data variables.product.prodname_copilot_short %} will then suggest a version of the following code:
165+
Open [{% data variables.product.prodname_copilot_chat_short %}](https://github.com/copilot), then ask:
166+
167+
```text copy
168+
How do I use the factorial function of the math module in my Python project?
169+
```
170+
171+
{% data variables.product.prodname_copilot_short %} will then suggest a version of the following code:
137172

138173
```python copy
139174
import math
@@ -146,3 +181,7 @@ print(f"The factorial of {number} is {result}")
146181
```
147182

148183
After you replace the existing code in your project with the above implementation, you've successfully reused code from a library in your example project!
184+
185+
## Further reading
186+
187+
* [AUTOTITLE](/get-started/learning-to-code/finding-and-understanding-example-code)

0 commit comments

Comments
 (0)