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
# Refactor and improve existing code sections using GitHub Copilot
8
8
9
-
GitHub Copilot can be used to evaluate your entire codebase and suggest changes that improve code quality, reliability, performance, and security. In this exercise, you use GitHub Copilot to refactor and improve specified sections of a C# application.
9
+
GitHub Copilot can be used to evaluate your entire codebase and suggest changes that refactor and/or improve your code. In this exercise, you use GitHub Copilot to refactor specified sections of a C# application while making improvements to code quality, reliability, performance, and security.
10
10
11
11
This exercise should take approximately **15** minutes to complete.
12
12
@@ -91,11 +91,19 @@ You're now ready to complete the exercise.
91
91
92
92
You're a developer working in the IT department of your local community. The backend systems that support the public library were lost in a fire. Your team needs to develop a temporary solution to help the library staff manage their operations until the system can be replaced. Your team chose GitHub Copilot to accelerate the development process.
93
93
94
-
You've created an initial version of the library application, but there are sill opportunities to improve performance, readability, maintainability, and security. During a code review, the team identified the following issues:
94
+
You handed off an initial version of the library application for review. The review team identified opportunities to improve code quality, performance, readability, maintainability, and security.
95
95
96
-
- Refactor the EnumHelper class to use dictionaries instead of reflection. Using dictionaries improves performance by reducing the overhead of reflection. Eliminating reflection also improves code readability, maintainability, and security.
96
+
The following updates are assigned to you:
97
97
98
-
- Refactor the data access methods to use LINQ (Language Integrated Query) rather than foreach loops. Using LINQ provides a more concise and readable way to query collections, databases, and XML documents. Using LINQ can improve code readability, maintainability, and performance.
98
+
1. Refactor the EnumHelper class to use static dictionaries instead of reflection.
99
+
100
+
- Using static dictionaries will improve performance (removes the overhead of reflection).
101
+
- Eliminating reflection also improves code readability, maintainability, and security.
102
+
103
+
1. Refactor the data access methods to use LINQ (Language Integrated Query) rather than foreach loops.
104
+
105
+
- Using LINQ provides a more concise and readable way to query collections, databases, and XML documents.
106
+
- Using LINQ can improve code readability, maintainability, and performance.
99
107
100
108
This exercise includes the following tasks:
101
109
@@ -151,18 +159,19 @@ Use the following steps to set up the library application:
151
159
152
160
You'll see some Warnings, but there shouldn't be any Errors.
153
161
154
-
155
162
## Use GitHub Copilot to refactor the EnumHelper class
156
163
157
164
Reflection is a powerful feature that allows you to inspect and manipulate objects at runtime. However, reflection can be slow and there are potential security risks associated with reflection that should be considered.
158
165
159
-
The existing EnumHelper class uses reflection to retrieve the description attribute of an enum value. You can refactor the EnumHelper class to use dictionaries instead of reflection. Using dictionaries can improve performance and eliminate any security concerns associated with using reflection.
166
+
The existing EnumHelper class uses reflection to retrieve the description attribute of an enum value. You need to refactor the EnumHelper class to use dictionaries instead of reflection. Using dictionaries can improve performance and eliminate any security concerns associated with using reflection.
160
167
161
168
In this exercise, you use GitHub Copilot to help you refactor the EnumHelper class.
162
169
163
170
Use the following steps to complete this section of the exercise:
164
171
165
-
1. Use the Solution Explorer view to open the EnumHelper.cs file.
172
+
1. Use Visual Studio Code's SOLUTION EXPLORER view to expand the **Library.ApplicationCore** folder, and then expand the **Enums** folder.
173
+
174
+
1. Open the EnumHelper.cs file and review the code.
166
175
167
176
```csharp
168
177
using System.ComponentModel;
@@ -194,21 +203,59 @@ Use the following steps to complete this section of the exercise:
194
203
}
195
204
```
196
205
197
-
1. Open the Chat view, and then add the following files to the Chat context: `EnumHelper.cs`, `LoanExtensionStatus.cs`, `LoanReturnStatus.cs`, `MembershipRenewalStatus.cs`.
206
+
The `GetDescription` method uses reflection to retrieve the description attribute of an enum value. The method checks if the `value` parameter is null. If it is, the method returns an empty string. Otherwise, it uses reflection to get the field information for the enum value and retrieves the custom attributes of type `DescriptionAttribute`. If any attributes are found, it returns the description; otherwise, it returns the string representation of the enum value.
207
+
208
+
1. Open the GitHub Copilot Chat view.
209
+
210
+
The Chat view provides a managed conversational interface for interacting with GitHub Copilot.
211
+
212
+
You can toggle the Chat view between open and closed using the **Toggle Chat** button, which is located at the top of the Visual Studio Code window, just to the right of the search textbox.
213
+
214
+

215
+
216
+
You can also use the keyboard shortcut **Ctrl+Alt+I** to toggle the Chat view.
217
+
218
+
1. Notice that the Chat view opens in **Ask** mode by default.
219
+
220
+
The Chat mode is displayed near the bottom-right corner of the Chat view.
221
+
222
+
The Set Mode menu includes three chat modes: **Ask**, **Edit**, and **Agent**. Each mode is designed for different types of interactions with GitHub Copilot.
223
+
224
+
- **Ask**: Use this mode to ask GitHub Copilot questions about your codebase. You can ask GitHub Copilot to explain code, suggest changes, or provide information about the codebase.
225
+
- **Edit**: Use this mode to edit code in your workspace. You can use GitHub Copilot to refactor code, add comments, or make other changes to your code.
226
+
- **Agent**: Use this mode to run GitHub Copilot as an agent. You can use GitHub Copilot to run commands, execute code, or perform other tasks in your workspace.
227
+
228
+
1. Ensure that the Chat mode is set to **Ask**.
229
+
230
+
GitHub Copilot's responses are displayed in the Chat view wen you're working in **Ask** mode.
231
+
232
+
1. Add the following files to the Chat context:
233
+
234
+
- EnumHelper.cs
235
+
- LoanExtensionStatus.cs
236
+
- LoanReturnStatus.cs
237
+
- MembershipRenewalStatus.cs
238
+
239
+
You can use a drag-and-drop operation to add the files from Visual Studio Code's explorer view to the Chat view. You can also use the **Add Context** button in the Chat view to add the files.
240
+
241
+
>**NOTE**: Adding files to the Chat context ensures that GitHub Copilot considers those files when generating a response. The relevance and accuracy of responses increase when GitHub Copilot understands the context associated with your prompts.
198
242
199
243
1. Enter the following prompt:
200
244
201
245
```plaintext
202
-
@workspace I want to refactor the `EnumHelper` class using dictionaries rather than reflectionto get enum description attributes. I want a separate dictionary for each enum. The enum values are in the `LoanExtensionStatus.cs`, `LoanReturnStatus.cs`, and `MembershipRenewalStatus.cs` files. Explain how to update the EnumHelper class to use dictionaries and provide the updated code.
246
+
@workspace I need to refactor the `EnumHelper` class and remove any code that uses reflection. Use static dictionaries to supply enum description attributes. Use a separate dictionary for each enum. The dictionaries should use values from the `LoanExtensionStatus.cs`, `LoanReturnStatus.cs`, and `MembershipRenewalStatus.cs` files. Explain how to update the EnumHelper class using dictionaries rather than reflection and provide the updated code.
203
247
```
204
248
205
-
This prompt asks GitHub Copilot to provide an explanation of how to update the EnumHelper class to use dictionaries instead of reflection. It also asks for the updated code that uses dictionaries for each enum value.
249
+
When writing a prompt that asks GitHub Copilot how to solve a problem, be sure explain the problem that you're trying to solve. Use concise sentences to provide details and specify constraints. Also, be sure to tell GitHub Copilot what to include in the response.
206
250
207
-
1. Take a minute to review the explanation provided by GitHub Copilot.
251
+
In this case, your prompt begins by explaining the problem. You tell GitHub Copilot that you need to refactor the `EnumHelper` class and remove any code that uses reflections. You tell GitHub Copilot that you want to use dictionaries to supply enum description attributes. You specify that you want a separate dictionary for each enum, and you tell GitHub Copilot that the enum values should be sourced from specific files that you specify by name. You finish by asking GitHub Copilot to explain how to update the `EnumHelper` class, and you ask for a code sample containing the updated code.
208
252
209
-
The explanation should be similar to the following description:
253
+
1. Take a minute to review the response provided by GitHub Copilot.
254
+
255
+
The response should be similar to the following example:
210
256
211
257
```markdown
258
+
212
259
To refactor the `EnumHelper` class to use dictionaries for each enum, you need to create a dictionary for each enum type that maps the enum values to their descriptions. This will eliminate the need for reflection at runtime.
213
260
214
261
Here's how you can update the `EnumHelper` class:
@@ -222,55 +269,56 @@ Use the following steps to complete this section of the exercise:
{ LoanExtensionStatus.Success, "Book loan extension was successful." },
300
+
{ LoanExtensionStatus.LoanNotFound, "Loan not found." },
301
+
{ LoanExtensionStatus.LoanExpired, "Cannot extend book loan as it already has expired. Return the book instead." },
302
+
{ LoanExtensionStatus.MembershipExpired, "Cannot extend book loan due to expired patron's membership." },
303
+
{ LoanExtensionStatus.LoanReturned, "Cannot extend book loan as the book is already returned." },
304
+
{ LoanExtensionStatus.Error, "Cannot extend book loan due to an error." }
305
+
};
306
+
307
+
public static string GetDescription(Enum value)
308
+
{
309
+
if (value == null)
310
+
return string.Empty;
311
+
312
+
return value switch
250
313
{
251
-
{ LoanExtensionStatus.Success, "Book loan extension was successful." },
252
-
{ LoanExtensionStatus.LoanNotFound, "Loan not found." },
253
-
{ LoanExtensionStatus.LoanExpired, "Cannot extend book loan as it already has expired. Return the book instead." },
254
-
{ LoanExtensionStatus.MembershipExpired, "Cannot extend book loan due to expired patron's membership." },
255
-
{ LoanExtensionStatus.LoanReturned, "Cannot extend book loan as the book is already returned." },
256
-
{ LoanExtensionStatus.Error, "Cannot extend book loan due to an error." }
314
+
MembershipRenewalStatus status => MembershipRenewalStatusDescriptions[status],
315
+
LoanReturnStatus status => LoanReturnStatusDescriptions[status],
316
+
LoanExtensionStatus status => LoanExtensionStatusDescriptions[status],
317
+
_ =>value.ToString()
257
318
};
258
-
259
-
public static string GetDescription(Enum value)
260
-
{
261
-
if (value == null)
262
-
return string.Empty;
263
-
264
-
return value switch
265
-
{
266
-
MembershipRenewalStatus status => MembershipRenewalStatusDescriptions[status],
267
-
LoanReturnStatus status => LoanReturnStatusDescriptions[status],
268
-
LoanExtensionStatus status => LoanExtensionStatusDescriptions[status],
269
-
_ =>value.ToString()
270
-
};
271
-
}
272
319
}
273
320
}
321
+
274
322
```
275
323
276
324
```markdown
@@ -279,12 +327,31 @@ Use the following steps to complete this section of the exercise:
279
327
280
328
```
281
329
282
-
> [!NOTE]
283
-
> If GitHub Copilot doesn't populate the dictionaries with the correct descriptions, ensure that the GitHub Copilot extension isn't requesting a restart. If it is, restart the extension and try the prompt again.
330
+
1. In the Chat view, hover the mouse pointer over the code snippet included in the response.
331
+
332
+
1. Notice the three buttons that appear in the top-right corner of the code snippet.
333
+
334
+
1. Hover the mouse pointer over each of the buttons to see a tooltip that describes the action.
335
+
336
+
These buttons can the used to either insert the code into the code editor or copy the code to the clipboard.
337
+
338
+
1. In the Chat view, select**Set Mode**, and thenselect**Edit**.
339
+
340
+
When prompted to confirm the change, select**Yes**.
341
+
342
+
In **Edit** more, GitHub Copilot displays responses as code update suggestions in code editor. The Edit mode is generally used when implementing a new feature, fixing a bug, or refactoring code.
343
+
344
+
1. Enter the following prompt:
345
+
346
+
```plaintext
347
+
@workspace Refactor the `EnumHelper` class using dictionaries rather than reflection to get enum description attributes. Use a separate dictionary foreach enum. Use the enum valuesin the `LoanExtensionStatus.cs`, `LoanReturnStatus.cs`, and `MembershipRenewalStatus.cs` files.
348
+
```
349
+
350
+
This prompt tells GitHub Copilot to refactor the `EnumHelper` class using dictionaries rather than reflection to get enum description attributes. It also specifies that a separate dictionary should be used for each enum, and that the enum values should be taken from the specified files.
284
351
285
-
1. Use GitHub Copilot's suggested `EnumHelper` class to update your EnumHelper.cs file.
352
+
1. Take a minute to review the suggested code updates.
286
353
287
-
1. Ensure that your updated `EnumHelper` class is using the enum values from the `LoanExtensionStatus.cs`, `LoanReturnStatus.cs`, and `MembershipRenewalStatus.cs` files.
354
+
Review the suggested updates to ensure that the enum values are coming from the `LoanExtensionStatus.cs`, `LoanReturnStatus.cs`, and `MembershipRenewalStatus.cs` files.
288
355
289
356
Open each of the enum files and verify that the enum values in the dictionaries are correct. If there are discrepancies, have GitHub Copilot update the dictionaries for each enum individually. For example, you can use the following prompt for the `LoanExtensionStatus` enum:
0 commit comments