You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
+
11
12
***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.
12
13
***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.
13
14
15
+
In this article, we'll learn both by working through an example: reusing Python code that calculates the factorial of a number.
16
+
14
17
## Using other people's code snippets in your project
15
18
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.
17
20
18
21
### 1. Finding and understanding a code snippet
19
22
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.
21
24
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:
23
26
24
27
```python
25
28
# Initialize the factorial result to 1
@@ -35,29 +38,41 @@ for i in range(1, number + 1):
35
38
print(f"The factorial of {number} is {factorial}")
36
39
```
37
40
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
+

39
44
40
-

45
+
In the chat window, ask {% data variables.product.prodname_copilot_short %}:
46
+
47
+
```text copy
48
+
Explain this program.
49
+
```
41
50
42
51
### 2. Understanding project licensing
43
52
44
53
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.
45
54
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**.
47
56
48
57

49
58
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
-
52
59
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.
53
60
61
+
> [!TIP] You can learn what's allowed by other common licenses with the [Choose a license](https://choosealicense.com/licenses/) tool.
62
+
54
63
### 3. Using and modifying code snippets
55
64
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.
57
68
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:
59
70
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:
61
76
62
77
```python copy
63
78
defcalculate_factorial(number):
@@ -84,7 +99,7 @@ Congratulations! You've successfully found, understood, and modified an example
84
99
85
100
## Using code from libraries in your project
86
101
87
-
Using librariesis 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.
88
103
89
104
In this section, we'll continue working with the Python factorial calculator example from the previous section. For reference, here's our current code:
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.
111
126
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.
113
134
114
135
### 2. Prioritizing security in your project
115
136
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.
117
140
118
141
#### Using popular libraries
119
142
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
+
```
121
150
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.
123
152
124
153
#### Enabling {% data variables.product.prodname_dependabot_alerts %} for your project
125
154
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.
127
156
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.
129
158
130
159

131
160
132
161
### 3. Implementing code from a library
133
162
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.
135
164
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:
137
172
138
173
```python copy
139
174
import math
@@ -146,3 +181,7 @@ print(f"The factorial of {number} is {result}")
146
181
```
147
182
148
183
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!
0 commit comments