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_07_consolidate_duplicate_code.md
+29-29Lines changed: 29 additions & 29 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,7 @@ lab:
6
6
7
7
# Consolidate duplicate code using GitHub Copilot
8
8
9
-
Duplicate code logic is often introduced when developing/extending a codebase that includes similar or related features. It may not be intentional, and it can be as simple as reusing code to prototype a new feature. If duplicated logic evolves to match the surrounding code over time, the issue can become more complicated. Changes to variable names, function names, and code structures in one location (but not the another) can mask duplicated logic. A rushed schedule, poor documentation, and a lack of proper code reviews can exacerbate the issue. In the end, duplicated logic makes the code difficult to read, maintain, debug, and test.
9
+
Duplicate code logic is often introduced when developing/extending a codebase that includes similar or related features. It might not be intentional, and it can be as simple as reusing code to prototype a new feature. If duplicated logic evolves to match the surrounding code over time, the issue can become more complicated. Changes to variable names, function names, and code structures in one location (but not the other) can mask duplicated logic. A rushed schedule, poor documentation, and a lack of proper code reviews can exacerbate the issue. In the end, duplicated logic makes the code difficult to read, maintain, debug, and test.
10
10
11
11
In this exercise, you review an existing project that contains duplicated code logic, analyze your options for consolidation, consolidate the duplicated code logic, and test the refactored code to ensure it works as intended. You use GitHub Copilot in Ask mode to gain an understanding of an existing code project and explore options for consolidating the logic. You use GitHub Copilot in Agent mode to refactor the code by combining duplicate logic into shared helper methods. You test the original and refactored code to ensure the consolidated logic works as intended.
12
12
@@ -16,19 +16,19 @@ This exercise should take approximately **30** minutes to complete.
16
16
17
17
## Before you start
18
18
19
-
Your lab environment must include the following: Git 2.48 or later, .NET SDK 9.0 or later, Visual Studio Code with the C# Dev Kit extension, and access to a GitHub account with GitHub Copilot enabled.
19
+
Your lab environment must include the following resources: Git 2.48 or later, .NET SDK 9.0 or later, Visual Studio Code with the C# Dev Kit extension, and access to a GitHub account with GitHub Copilot enabled.
20
20
21
21
### Configure your lab environment
22
22
23
23
If you're using a local PC as a lab environment for this exercise:
24
24
25
-
- For help configuring your local PC as your lab environment, open the following link in a browser: <ahref="https://go.microsoft.com/fwlink/?linkid=2320147"target="_blank">Configure your lab environment resources</a>.
25
+
- For help with configuring your local PC as your lab environment, open the following link in a browser: <ahref="https://go.microsoft.com/fwlink/?linkid=2320147"target="_blank">Configure your lab environment resources</a>.
26
26
27
-
- For help enabling your GitHub Copilot subscription in Visual Studio Code, open the following link in a browser: <ahref="https://go.microsoft.com/fwlink/?linkid=2320158"target="_blank">Enable GitHub Copilot within Visual Studio Code</a>.
27
+
- For help with enabling your GitHub Copilot subscription in Visual Studio Code, open the following link in a browser: <ahref="https://go.microsoft.com/fwlink/?linkid=2320158"target="_blank">Enable GitHub Copilot within Visual Studio Code</a>.
28
28
29
29
If you're using a hosted lab environment for this exercise:
30
30
31
-
- For help enabling your GitHub Copilot subscription in Visual Studio Code, paste the following URL into a browser's site navigation bar: <ahref="https://go.microsoft.com/fwlink/?linkid=2320158"target="_blank">Enable GitHub Copilot within Visual Studio Code</a>.
31
+
- For help with enabling your GitHub Copilot subscription in Visual Studio Code, paste the following URL into a browser's site navigation bar: <ahref="https://go.microsoft.com/fwlink/?linkid=2320158"target="_blank">Enable GitHub Copilot within Visual Studio Code</a>.
32
32
33
33
- To ensure that the .NET SDK is configured to use the official NuGet.org repository as a source for downloading and restoring packages:
34
34
@@ -96,9 +96,9 @@ Use the following steps to download the sample project and open it in Visual Stu
96
96
97
97
## Exercise scenario
98
98
99
-
You're a software developer working for a consulting firm. Your clients need help consolidating duplicate code logic. Your goal is to improve code maintainability while preserving the existing functionality. You're assigned to the following app:
99
+
You're a software developer working for a consulting firm. Your clients need helpwith consolidating duplicate code logic. Your goal is to improve code maintainability while preserving the existing functionality. You're assigned to the following app:
100
100
101
-
- E-CommerceOrdersAndReturns: This is an E-commerce app that processes customer orders and handles product returns. It includes core business logic for validating orders and returns, calculating shipping costs, sending email notifications, logging audit activities, and managing inventory levels.
101
+
- E-CommerceOrdersAndReturns: This app is an E-commerce app that processes customer orders and handles product returns. It includes core business logic for validating orders and returns, calculating shipping costs, sending email notifications, logging audit activities, and managing inventory levels.
102
102
103
103
This exercise includes the following tasks:
104
104
@@ -111,7 +111,7 @@ This exercise includes the following tasks:
111
111
112
112
The first step in any refactoring effort is to ensure that you understand the existing codebase. It's important to understand the code structure, the business logic, and the results generated when the code runs.
113
113
114
-
In this task, you'll review the main components of the E-commerce order and return processing project, scan the code for duplicate code patterns, and test the code.
114
+
In this task, you review the main components of the E-commerce order and return processing project, scan the code for duplicate code patterns, and test the code.
115
115
116
116
Use the following steps to complete this task:
117
117
@@ -123,13 +123,13 @@ Use the following steps to complete this task:
123
123
124
124
Open **OrderProcessor.cs** and **ReturnProcessor.cs** side by side. These classes represent the core business logic for processing customer orders and product returns respectively.
125
125
126
-
Notice that the two classes have similar method signatures and processing patterns. This is the most obvious type of duplication, but there are additional, more subtle duplications throughout the codebase.
126
+
Notice that the two classes have similar method signatures and processing patterns. This similarity is the most obvious type of duplication, but there are other, more subtle duplications throughout the codebase.
127
127
128
128
1. Review the Services layer.
129
129
130
130
Navigate to the **Services** folder and examine **EmailService.cs**, **AuditService.cs**, and **InventoryService.cs**.
131
131
132
-
You may notice that these services implement similar patterns for handling email notifications, audit logging, and inventory management. Each service has methods that follow similar structures but are duplicated for different business processes (orders vs returns).
132
+
You might notice that these services implement similar patterns for handling email notifications, audit logging, and inventory management. Each service has methods that follow similar structures but are duplicated for different business processes (orders vs returns).
133
133
134
134
1. Run the application and review the console output.
135
135
@@ -145,7 +145,7 @@ Use the following steps to complete this task:
145
145
- Updated inventory levels
146
146
- Security validation tests with various invalid inputs
147
147
148
-
The application runs 5test scenarios to demonstrate both successful processing and security validation failures.
148
+
The application runs five test scenarios to demonstrate both successful processing and security validation failures.
149
149
150
150
1. Take a minute to categorize any duplicate code patterns that you observed.
151
151
@@ -161,7 +161,7 @@ It's important to understand the existing functionality before making changes. B
161
161
162
162
GitHub Copilot Chat's Ask mode is a great tool for analyzing complex codebases and identifying subtle duplication patterns that might not be immediately obvious. In Ask mode, Copilot acts as an intelligent code reviewer that can analyze multiple files simultaneously and identify both obvious and hidden (code logic) duplications.
163
163
164
-
In this task, you'll use GitHub Copilot to systematically identify the various types of duplicate code patterns in the e-commerce application.
164
+
In this task, you use GitHub Copilot to systematically identify the various types of duplicate code patterns in the e-commerce application.
165
165
166
166
Use the following steps to complete this task:
167
167
@@ -193,7 +193,7 @@ Use the following steps to complete this task:
193
193
194
194
1. Take a minute to review GitHub Copilot's response.
195
195
196
-
GitHub Copilot should identify the `Validate()` method duplication and the similar patterns in`CalculateShipping()` methods. It may also note similar patterns in relation to audit logging, error handling, and payment/refund processing.
196
+
GitHub Copilot should identify the `Validate()` method duplication and the similar patterns in`CalculateShipping()` methods. It might also note similar patterns in relation to audit logging, error handling, and payment/refund processing.
197
197
198
198
1. Update the chat context to specify the **EmailService.cs** file.
199
199
@@ -209,7 +209,7 @@ Use the following steps to complete this task:
209
209
210
210
1. Take a minute to review GitHub Copilot's response.
211
211
212
-
GitHub Copilot should identify the duplicate logic for handling order confirmations and return confirmations. This includes a templated approach to preparing and sending emails. GitHub Copilot may also identify duplicate patterns related to helper methods and console output.
212
+
GitHub Copilot should identify the duplicate logic for handling order confirmations and return confirmations. The response includes a templated approach to preparing and sending emails. GitHub Copilot might also identify duplicate patterns related to helper methods and console output.
213
213
214
214
1. Update the chat context to specify the **AuditService.cs** and **InventoryService.cs** files.
215
215
@@ -225,7 +225,7 @@ Use the following steps to complete this task:
225
225
226
226
1. Take a minute to review GitHub Copilot's response.
227
227
228
-
GitHub Copilot should identify patterns like audit entry creation/validation/storage in AuditService, and inventory validation/updating/logging in InventoryService. GitHub Copilot may also identify duplicate patterns related to helper methods.
228
+
GitHub Copilot should identify patterns like audit entry creation/validation/storage in AuditService, and inventory validation/updating/logging in InventoryService. GitHub Copilot might also identify duplicate patterns related to helper methods.
229
229
230
230
1. Ask GitHub Copilot to perform a comprehensive duplication analysis.
231
231
@@ -235,7 +235,7 @@ Use the following steps to complete this task:
235
235
@workspace Analyze the entire ECommerceOrderAndReturn codebase and identify all forms of code duplication, including method-level, service-level, and architectural pattern duplications. Prioritize the duplications by impact and refactoring difficulty. Describe an approach for consolidating this code.
236
236
```
237
237
238
-
After analyzing specific files, you can get a broader view by asking GitHub Copilot to include the entire codebase in its analysis. The increased scope can reveal additional duplication patterns, such as similar error handling, logging, and validation logic across multiple files or layers. Having a single response that informs and prioritizes your refactoring strategy is often helpful.
238
+
After analyzing specific files, you can get a broader view by asking GitHub Copilot to include the entire codebase in its analysis. The increased scope can reveal other duplication patterns, such as similar error handling, logging, and validation logic across multiple files or layers. Having a single response that informs and prioritizes your refactoring strategy is often helpful.
239
239
240
240
>**NOTE**: The `@workspace` and `#codebase` keywords tell GitHub Copilot to include the entire codebase in the context of its analysis. The `@workspace` keyword is only available when using GitHub Copilot in the Ask mode. The `#codebase` keyword can be used in any mode (Ask, Edit, or Agent).
241
241
@@ -261,21 +261,21 @@ Use the following steps to complete this task:
261
261
262
262
Cross-reference GitHub Copilot's findings with your own observations. Ensure that you understand not just what is duplicated, but why these patterns exist and how they should be consolidated while maintaining the business logic integrity.
263
263
264
-
GitHub Copilot's Ask mode is particularly powerful for identifying subtle duplications that go beyond simple copy-paste scenarios. It can recognize similar logical patterns, equivalent business rules implemented differently, and architectural duplications that span multiple files.
264
+
GitHub Copilot's Ask mode is good at identifying subtle duplications that go beyond simple copy-paste scenarios. It can recognize similar logical patterns, equivalent business rules implemented differently, and architectural duplications that span multiple files.
265
265
266
266
> **NOTE**: The analysis generated during this task is used to inform the refactoring strategy that you implement in the next section.
267
267
268
268
### Consolidate duplicate logic using GitHub Copilot Chat (Agent mode)
269
269
270
-
GitHub Copilot's Agent mode enables you to assign complex, multi-step refactoring tasks that span multiple files and architectural layers. The agent can autonomously create new files, modify existing code, and implement comprehensive refactoring strategies while keeping you informed of its progress.
270
+
GitHub Copilot's Agent mode enables you to assign moderately complex, multi-step refactoring tasks that span multiple files and architectural layers. The agent can autonomously create new files, modify existing code, and implement comprehensive refactoring strategies while keeping you informed of its progress.
271
271
272
-
In this task, you'll use GitHub Copilot Agent to systematically eliminate the duplicate code patterns identified in the previous task, starting with the most straightforward duplications and progressing to more complex service-layer consolidations.
272
+
In this task, you use GitHub Copilot Agent to systematically eliminate the duplicate code patterns identified in the previous task, starting with the most straightforward duplications and progressing to more complex service-layer consolidations.
273
273
274
274
Use the following steps to complete this task:
275
275
276
276
1. Switch the GitHub Copilot Chat view to Agent mode.
277
277
278
-
When using Agent mode, GitHub Copilot can make autonomous changes to your codebase. Agent mode is particularly useful for complex, multi-step refactoring tasks.
278
+
When you use Agent mode, GitHub Copilot can make autonomous changes to your codebase. Agent mode is good at moderately complex, multi-step refactoring tasks.
279
279
280
280
To change modes, locate the mode selector (typically in the bottom-left corner of the Chat view) and select**Agent**.
281
281
@@ -309,13 +309,13 @@ Use the following steps to complete this task:
309
309
310
310
The agent's progress should be visible in the chat as it completes the assigned task.
311
311
312
-
To assist the agent as the task is being processed, provide permission to continue or supply additional context as needed. For example, if the agent asks for permission to Build or Run the application, select **Continue**
312
+
To assist the agent as the task is being processed, provide permission to continue or supply more context as needed. For example, if the agent asks for permission to Build or Run the application, select**Continue**
313
313
314
-
The agent will accomplish the following during this task:
314
+
The agent completes the following steps during this task:
315
315
316
316
- Create a new **ValidationService.cs** file in the Services folder
317
317
- Extract the validation logic into a reusable method
318
-
- Update both processor classes to use the new service
318
+
- Update both processor classes (to use the new service)
319
319
- Remove the duplicate private validation methods
320
320
- Verify functionality with a successful build and test run
321
321
@@ -387,9 +387,9 @@ GitHub Copilot Agent excels at complex, multi-file refactoring tasks that requir
387
387
388
388
### Test the refactored E-commerce orders and returns code
389
389
390
-
Manual testing and verification is crucial to ensure that your refactored code maintains the intended business logic and functionality. A successful refactoring process should achieve the intended goal (such as consolidating duplicate code logic) while producing identical behavior to the original implementation.
390
+
Manual testing and verification are crucial to ensure that your refactored code maintains the intended business logic and functionality. A successful refactoring process should achieve the intended goal (such as consolidating duplicate code logic) while producing identical behavior to the original implementation.
391
391
392
-
In this task, you'll verify that the refactored code maintains all original functionality and that the consolidation has been successful.
392
+
In this task, you verify that the refactored code maintains all original functionality and that the consolidation was successful.
393
393
394
394
Use the following steps to complete this task:
395
395
@@ -399,7 +399,7 @@ Use the following steps to complete this task:
399
399
400
400
1. Run the refactored application and capture the output.
401
401
402
-
The application should run all 5test scenarios exactly as before:
402
+
The application should run all five test scenarios exactly as before:
403
403
404
404
- Initial inventory display
405
405
- Valid order processing with complete workflow
@@ -429,14 +429,14 @@ Use the following steps to complete this task:
429
429
- **Maintainability**: Changes to business rules now require updates in only one location
430
430
- **Readability**: The code structure is clear and logical
431
431
- **Reusability**: Shared services can be easily extended for future requirements
432
-
- **Extensibility**: New features can be added with minimal impact on existing code
432
+
- **Extensibility**: New features can be added with minimal effect on existing code
433
433
434
-
Manual testing verifies that your consolidation efforts have achieved the intended goal: eliminating duplicate code while maintaining system functionality. The architecture now provides a more maintainable foundation forfuture development, where business rule changes can be implementedin a single location rather than requiring updates across multiple duplicate implementations.
434
+
Manual testing verifies that your consolidation efforts achieved the intended goal: eliminating duplicate code while maintaining system functionality. The architecture now provides a more maintainable foundation for future development, where business rule changes can be implemented in a single location rather than requiring updates across multiple duplicate implementations.
435
435
436
436
## Summary
437
437
438
438
In this exercise, you learned how to use GitHub Copilot to consolidate duplicate code in an application. You explored the E-commerce Order and Return Processing System, identified duplicate code patterns, and used GitHub Copilot to refactor the codebase for improved maintainability and readability.
439
439
440
440
## Clean up
441
441
442
-
Now that you've finished the exercise, take a minute to ensure that you haven't made changes to your GitHub account or GitHub Copilot subscription that you don't want to keep. If you made any changes, revert them as needed. If you're using a local PC as your lab environment, you can archive or delete the sample projects folder that you created for this exercise.
442
+
Now that you finished the exercise, take a minute to ensure that you haven't made changes to your GitHub account or GitHub Copilot subscription that you don't want to keep. If you made any changes, revert them as needed. If you're using a local PC as your lab environment, you can archive or delete the sample projects folder that you created for this exercise.
0 commit comments