Skip to content

Commit a287674

Browse files
committed
instruction updates
1 parent 01ec749 commit a287674

2 files changed

Lines changed: 124 additions & 57 deletions

File tree

Instructions/Labs/LAB_AK_01_examine_settings_interface.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ Use the following steps to complete this section of the exercise:
204204
205205
1. To open the Chat view, select the **Toggle Chat** button.
206206
207-
The Toggle Chat button is located in the Visual Studio Code Status Bar, just to the right of the Copilot icon.
207+
The Toggle Chat button is located at the top of the Visual Studio Code window, just to the right of the search textbox.
208208
209209
![Screenshot showing the Copilot Toggle Chat button.](./media/m01-github-copilot-toggle-chat.png)
210210

Instructions/Labs/LAB_AK_05_refactor_improve_existing_code.md

Lines changed: 123 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ lab:
66

77
# Refactor and improve existing code sections using GitHub Copilot
88

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.
1010

1111
This exercise should take approximately **15** minutes to complete.
1212

@@ -91,11 +91,19 @@ You're now ready to complete the exercise.
9191

9292
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.
9393
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.
9595
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:
9797
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.
99107
100108
This exercise includes the following tasks:
101109
@@ -151,18 +159,19 @@ Use the following steps to set up the library application:
151159

152160
You'll see some Warnings, but there shouldn't be any Errors.
153161

154-
155162
## Use GitHub Copilot to refactor the EnumHelper class
156163

157164
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.
158165

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.
160167

161168
In this exercise, you use GitHub Copilot to help you refactor the EnumHelper class.
162169

163170
Use the following steps to complete this section of the exercise:
164171

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.
166175
167176
```csharp
168177
using System.ComponentModel;
@@ -194,21 +203,59 @@ Use the following steps to complete this section of the exercise:
194203
}
195204
```
196205
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+
![Screenshot showing the Copilot Toggle Chat button.](./media/m01-github-copilot-toggle-chat.png)
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.
198242

199243
1. Enter the following prompt:
200244

201245
```plaintext
202-
@workspace I want to refactor the `EnumHelper` class using dictionaries rather than reflection to 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.
203247
```
204248

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.
206250
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.
208252
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:
210256
211257
```markdown
258+
212259
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.
213260
214261
Here's how you can update the `EnumHelper` class:
@@ -222,55 +269,56 @@ Use the following steps to complete this section of the exercise:
222269
```
223270
224271
```csharp
272+
225273
using System.ComponentModel;
226274
using System.Collections.Generic;
227275

228-
namespace Library.ApplicationCore.Enums
276+
namespace Library.ApplicationCore.Enums;
277+
278+
public static class EnumHelper
229279
{
230-
public static class EnumHelper
280+
private static readonly Dictionary<MembershipRenewalStatus, string> MembershipRenewalStatusDescriptions = new()
231281
{
232-
private static readonly Dictionary<MembershipRenewalStatus, string> MembershipRenewalStatusDescriptions = new()
233-
{
234-
{ MembershipRenewalStatus.Success, "Membership renewal was successful." },
235-
{ MembershipRenewalStatus.PatronNotFound, "Patron not found." },
236-
{ MembershipRenewalStatus.TooEarlyToRenew, "It is too early to renew the membership." },
237-
{ MembershipRenewalStatus.LoanNotReturned, "Cannot renew membership due to an outstanding loan." },
238-
{ MembershipRenewalStatus.Error, "Cannot renew membership due to an error." }
239-
};
240-
241-
private static readonly Dictionary<LoanReturnStatus, string> LoanReturnStatusDescriptions = new()
242-
{
243-
{ LoanReturnStatus.Success, "Book was successfully returned." },
244-
{ LoanReturnStatus.LoanNotFound, "Loan not found." },
245-
{ LoanReturnStatus.AlreadyReturned, "Cannot return book as the book is already returned." },
246-
{ LoanReturnStatus.Error, "Cannot return book due to an error." }
247-
};
248-
249-
private static readonly Dictionary<LoanExtensionStatus, string> LoanExtensionStatusDescriptions = new()
282+
{ MembershipRenewalStatus.Success, "Membership renewal was successful." },
283+
{ MembershipRenewalStatus.PatronNotFound, "Patron not found." },
284+
{ MembershipRenewalStatus.TooEarlyToRenew, "It is too early to renew the membership." },
285+
{ MembershipRenewalStatus.LoanNotReturned, "Cannot renew membership due to an outstanding loan." },
286+
{ MembershipRenewalStatus.Error, "Cannot renew membership due to an error." }
287+
};
288+
289+
private static readonly Dictionary<LoanReturnStatus, string> LoanReturnStatusDescriptions = new()
290+
{
291+
{ LoanReturnStatus.Success, "Book was successfully returned." },
292+
{ LoanReturnStatus.LoanNotFound, "Loan not found." },
293+
{ LoanReturnStatus.AlreadyReturned, "Cannot return book as the book is already returned." },
294+
{ LoanReturnStatus.Error, "Cannot return book due to an error." }
295+
};
296+
297+
private static readonly Dictionary<LoanExtensionStatus, string> LoanExtensionStatusDescriptions = new()
298+
{
299+
{ 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
250313
{
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()
257318
};
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-
}
272319
}
273320
}
321+
274322
```
275323
276324
```markdown
@@ -279,12 +327,31 @@ Use the following steps to complete this section of the exercise:
279327

280328
```
281329
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 then select **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 for each enum. Use the enum values in 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.
284351
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.
286353
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.
288355
289356
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:
290357

0 commit comments

Comments
 (0)