Skip to content

Commit a4bd16c

Browse files
committed
M5 updates
1 parent a5301b0 commit a4bd16c

1 file changed

Lines changed: 55 additions & 12 deletions

File tree

Instructions/Labs/LAB_AK_05_refactor_improve_existing_code.md

Lines changed: 55 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ You need to:
124124
GitHub Copilot's Chat view has three modes: **Ask**, **Edit**, and **Agent**. Each mode is designed for different types of interactions with GitHub Copilot.
125125

126126
- **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.
128128
- **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.
129129

130130
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:
179179
180180
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.
181181
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:
183185
184186
```plaintext
185187
@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:
191193
192194
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.
193195
194-
1. Enter the following prompt:
196+
1. Review and then submit the following prompt:
195197
196198
```plaintext
197199
@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:
210212
211213
> **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.
212214
213-
1. Enter the following prompt:
215+
1. Review and then submit the following prompt:
214216
215217
```plaintext
216218
@@ -226,7 +228,7 @@ Use the following steps to complete this section of the exercise:
226228
227229
1. Take a minute to review the response provided by GitHub Copilot.
228230
229-
The response should be similar to the following example:
231+
The response should be similar to the following markdown and code samples:
230232
231233
```markdown
232234
@@ -330,7 +332,7 @@ The Chat view's Edit mode is designed for editing code in your workspace. You ca
330332
- LoanReturnStatus.cs
331333
- MembershipRenewalStatus.cs
332334
333-
1. Enter the following prompt:
335+
1. Review and then submit the following prompt:
334336
335337
```plaintext
336338
@@ -348,18 +350,40 @@ The Chat view's Edit mode is designed for editing code in your workspace. You ca
348350
349351
```plaintext
350352
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.
352354
353355
```
354356
355357
1. In the Chat view, to accept all updates, select **Keep**.
356358
357359
You could also use the Chat Edits toolbar near the bottom of the code editor tab to accept or reject code updates.
358360
359-
1. To end the Chat session, select **Done**.
360-
361361
1. Take a minute to review the updated **GetDescription** method.
362362
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+
363387
```csharp
364388
365389
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:
501525
}
502526
```
503527
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.
505529
506530
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.
507531
@@ -753,7 +777,26 @@ Use the following steps to complete this section of the exercise:
753777
754778
1. Take a minute to review the suggested update.
755779
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
757800
758801
```csharp
759802
@@ -1053,7 +1096,7 @@ Now that you've refactored the code, it's time to build and run the application
10531096
10541097
## Summary
10551098
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.
10571100
10581101
## Clean up
10591102

0 commit comments

Comments
 (0)