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
Copy file name to clipboardExpand all lines: Instructions/Labs/LAB_AK_05_refactor_improve_existing_code.md
+55-12Lines changed: 55 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -124,7 +124,7 @@ You need to:
124
124
GitHub Copilot's Chat view has three modes: **Ask**, **Edit**, and **Agent**. Each mode is designed for different types of interactions with GitHub Copilot.
125
125
126
126
- **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.
127
-
- **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.
127
+
- **Edit**: Use this mode to edit selected code files. You can use GitHub Copilot to refactor code, add comments, or make other changes to your code.
128
128
- **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.
129
129
130
130
In this section of the exercise, you use the Chat view in Ask mode to analyze your coding assignment.
@@ -179,7 +179,9 @@ Use the following steps to complete this section of the exercise:
179
179
180
180
The current Chat mode is displayed near the bottom-right corner of the Chat view. Chat responses are displayed in the Chat view when you're working in **Ask** mode.
181
181
182
-
1. Select the code in the EnumHelper.cs file, and then enter the following prompt:
182
+
1. Select the code in the EnumHelper.cs file.
183
+
184
+
1. Review and then submit the following prompt:
183
185
184
186
```plaintext
185
187
@workspace Explain how the GetDescription method uses reflection to assign the return value.
@@ -191,7 +193,7 @@ Use the following steps to complete this section of the exercise:
191
193
192
194
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 attributes of type **DescriptionAttribute**. If any attributes are found, it returns the description; otherwise, it returns the string representation of the enum value.
193
195
194
-
1. Enter the following prompt:
196
+
1. Review and then submit the following prompt:
195
197
196
198
```plaintext
197
199
@workspace Which files in this workspace are used to store the enum values passed to the GetDescription method?
@@ -210,7 +212,7 @@ Use the following steps to complete this section of the exercise:
210
212
211
213
>**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.
212
214
213
-
1. Enter the following prompt:
215
+
1. Review and then submit the following prompt:
214
216
215
217
```plaintext
216
218
@@ -226,7 +228,7 @@ Use the following steps to complete this section of the exercise:
226
228
227
229
1. Take a minute to review the response provided by GitHub Copilot.
228
230
229
-
The response should be similar to the following example:
231
+
The response should be similar to the following markdown and code samples:
230
232
231
233
```markdown
232
234
@@ -330,7 +332,7 @@ The Chat view's Edit mode is designed for editing code in your workspace. You ca
330
332
- LoanReturnStatus.cs
331
333
- MembershipRenewalStatus.cs
332
334
333
-
1. Enter the following prompt:
335
+
1. Review and then submit the following prompt:
334
336
335
337
```plaintext
336
338
@@ -348,18 +350,40 @@ The Chat view's Edit mode is designed for editing code in your workspace. You ca
348
350
349
351
```plaintext
350
352
351
-
@workspace Use the description values in LoanExtensionStatus.cs to update the LoanExtensionStatus dictionary in the EnumHelper class. Provide the updated code for the LoanExtensionStatus dictionary in the EnumHelper class.
353
+
#codebase Use the description values in LoanExtensionStatus.cs to update the LoanExtensionStatus dictionary in the EnumHelper class. Provide the updated code for the LoanExtensionStatus dictionary in the EnumHelper class.
352
354
353
355
```
354
356
355
357
1. In the Chat view, to accept all updates, select **Keep**.
356
358
357
359
You could also use the Chat Edits toolbar near the bottom of the code editor tab to accept or reject code updates.
358
360
359
-
1. To end the Chat session, select **Done**.
360
-
361
361
1. Take a minute to review the updated **GetDescription** method.
362
362
363
+
GitHub Copilot should have updated the **GetDescription** method to use pattern matching and static dictionaries instead of reflection. The updated method should look similar to one of the following examples:
364
+
365
+
```csharp
366
+
367
+
public static string GetDescription(Enum value)
368
+
{
369
+
if (value == null)
370
+
return string.Empty;
371
+
372
+
// Use type checks to select the correct dictionary
373
+
if (value is LoanExtensionStatus les && LoanExtensionStatusDescriptions.TryGetValue(les, out var lesDesc))
374
+
return lesDesc;
375
+
if (value is LoanReturnStatus lrs && LoanReturnStatusDescriptions.TryGetValue(lrs, out var lrsDesc))
376
+
return lrsDesc;
377
+
if (value is MembershipRenewalStatus mrs && MembershipRenewalStatusDescriptions.TryGetValue(mrs, out var mrsDesc))
378
+
return mrsDesc;
379
+
380
+
return value.ToString();
381
+
}
382
+
383
+
```
384
+
385
+
or
386
+
363
387
```csharp
364
388
365
389
public static string GetDescription<TEnum>(TEnum value) where TEnum : Enum
@@ -501,7 +525,7 @@ Use the following steps to complete this section of the exercise:
501
525
}
502
526
```
503
527
504
-
Notice that a LINQ query is used to replace the **foreach (Loan loan in Loans!)** loop.
528
+
Notice that a LINQ query is used to replace the foreach loop.
505
529
506
530
The LINQ code uses the object initializer to assign object properties to the new **Patron** object. This removes the requirement for a separate **populated** instance of the **Patron** object. Overall, the updated code is shorter and more readable.
507
531
@@ -753,7 +777,26 @@ Use the following steps to complete this section of the exercise:
753
777
754
778
1. Take a minute to review the suggested update.
755
779
756
-
The suggested update should look similar to the following code:
780
+
The suggested update should look similar to one of the following examples:
781
+
782
+
```csharp
783
+
784
+
public async Task UpdateLoan(Loan loan)
785
+
{
786
+
var loans = _jsonData.Loans!;
787
+
var index = loans.FindIndex(l => l.Id == loan.Id);
788
+
789
+
if (index >= 0)
790
+
{
791
+
loans[index] = loan;
792
+
await _jsonData.SaveLoans(loans);
793
+
await _jsonData.LoadData();
794
+
}
795
+
}
796
+
797
+
```
798
+
799
+
or
757
800
758
801
```csharp
759
802
@@ -1053,7 +1096,7 @@ Now that you've refactored the code, it's time to build and run the application
1053
1096
1054
1097
## Summary
1055
1098
1056
-
In this exercise, you learned how to refactor code using GitHub Copilot. You used the Chat view in Edit mode to refactor the **EnumHelper** class, replacing reflection with static dictionaries. You also used the Chat view in Edit mode to refactor the **JsonData** and **JsonLoanRepository** classes, replacing foreach loops with LINQ queries. Finally, you used the Chat view in Agent mode to refactor the **JsonPatronRepository** class, replacing foreach loops with LINQ queries.
1099
+
In this exercise, you learned how to refactor code using GitHub Copilot. You used the Chat view in Edit mode to refactor the **EnumHelper** class, replacing reflection with static dictionaries. You also used the inline chat and Edit mode to refactor the **JsonData** and **JsonLoanRepository** classes, replacing foreach loops with LINQ queries. Finally, you used the Agent mode to refactor the **JsonPatronRepository** class, replacing foreach loops with LINQ queries.
0 commit comments