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_08_refactor_large_functions.md
+27-27Lines changed: 27 additions & 27 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
# Refactor large functions using GitHub Copilot
8
8
9
-
Large functions can be difficult to read, maintain, and test. They often contain multiple responsibilities and can be challenging to understand at a glance. Refactoring large functions into smaller, more focused functions can improve code readability and maintainability.
9
+
Large functions can be difficult to read, maintain, and test. They often contain multiple responsibilities and can be challenging to understand at a glance. Code readability and maintainability improves when large functions are refactored into smaller, more focused functions.
10
10
11
11
In this exercise, you review an existing project that contains a large function, analyze your options for smaller single-responsibility functions, refactor the large function into smaller functions, 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 refactoring the logic. You use GitHub Copilot in Agent mode to refactor the code by extracting code sections from the large function to create smaller functions. You test the original and refactored code to ensure the refactored code 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
@@ -92,7 +92,7 @@ Use the following steps to download the sample project and open it in Visual Stu
92
92
93
93
## Exercise scenario
94
94
95
-
You're a software developer working fora consulting firm. Your clients need help refactoring large functionsin legacy applications. Your goal is to improve code readability and maintainability while preserving the existing functionality. You're assigned to the following app:
95
+
You're a software developer working fora consulting firm. Your clients need help with refactoring large functionsin legacy applications. Your goal is to improve code readability and maintainability while preserving the existing functionality. You're assigned to the following app:
96
96
97
97
- E-CommerceOrderProcessing: This e-commerce app is used to process customer orders. The process includes order validation, inventory management, payment processing, shipping coordination, and customer notifications. The application uses Clean Architecture principles with a layered structure, but contains a large method in the **OrderProcessor** class that handles multiple responsibilities and needs to be refactored into smaller, more focused methods.
98
98
@@ -107,7 +107,7 @@ This exercise includes the following tasks:
107
107
108
108
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.
109
109
110
-
In this task, you'll review the main components of the E-commerce order processing project and run the app to observe its functionality.
110
+
In this task, you review the main components of the E-commerce order processing project and run the app to observe its functionality.
111
111
112
112
Use the following steps to complete this task:
113
113
@@ -147,13 +147,13 @@ Use the following steps to complete this task:
147
147
148
148
1. Take a minute to review the **OrderProcessor** class.
149
149
150
-
Notice the ProcessOrder method. This method represents the core business logic for processing customer orders. Notice that it handles multiple distinct operations. The ProcessOrder method is intentionally large and complex to demonstrate real-world scenarios where business logic has accumulated over time, making it difficult to read, test, and maintain.
150
+
Notice the ProcessOrder method. This method represents the core business logic forprocessing customer orders. Notice that it handles multiple distinct operations. The ProcessOrder method is intentionally large and complex to demonstrate real-world scenarios where business logic grewin complexity over time, making it difficult to read, test, and maintain.
151
151
152
152
1. Right-click the **ProcessOrder** method, and thenselect**Copilot**>**Explain**.
153
153
154
154
If prompted to **Select an enclosing range to explain**, select**ProcessOrder**.
155
155
156
-
GitHub Copilot will analyze the ProcessOrder method and provide a detailed explanation of what the code does, helping you understand the business logic before you investigate refactoring options.
156
+
GitHub Copilot analyzes the ProcessOrder method and provide a detailed explanation of what the code does, helping you understand the business logic before you investigate refactoring options.
157
157
158
158
1. Take a couple minutes to review GitHub Copilot's explanation.
159
159
@@ -171,7 +171,7 @@ Use the following steps to complete this task:
171
171
dotnet run
172
172
```
173
173
174
-
1. Review the console output that's generated when the application runs.
174
+
1. Review the console output generated by the application.
175
175
176
176
The application generates output for four test cases. Each test case demonstrates a different scenario:
177
177
@@ -186,13 +186,13 @@ Use the following steps to complete this task:
186
186
187
187
The ProcessOrder method has several distinct responsibilities. Each of the corresponding code sections could be extracted into a separate method.
188
188
189
-
Understanding the existing functionality and identifying refactoring opportunities will help you create a refactoring strategy that maintains business logic while improving code structure. The layered architecture already provides good separation of concerns at the project level, but the large ProcessOrder method needs attention.
189
+
Understanding the existing functionality and identifying refactoring opportunities helps you create a refactoring strategy that maintains business logic while improving code structure. The layered architecture already provides good separation of concerns at the project level, but the large ProcessOrder method needs attention.
190
190
191
191
### Identify refactoring opportunities using GitHub Copilot Chat (Ask mode)
192
192
193
193
GitHub Copilot Chat's Ask mode is a great tool for analyzing complex code and identifying opportunities for refactoring large methods. In Ask mode, Copilot can analyze your code structure and suggest ways to break down monolithic methods into smaller, more focused methods.
194
194
195
-
In this task, you'll use GitHub Copilot to evaluate the ProcessOrder method and identify refactoring opportunities that maintain business logic while improving code structure.
195
+
In this task, you use GitHub Copilot to evaluate the ProcessOrder method and identify refactoring opportunities that maintain business logic while improving code structure.
196
196
197
197
Use the following steps to complete this task:
198
198
@@ -248,7 +248,7 @@ Use the following steps to complete this task:
248
248
- Guidance on how to handle errors consistently across methods.
249
249
- Explanations of how the refactored structure improves maintainability.
250
250
251
-
1. Ask foradditional guidance on error handling patterns.
251
+
1. Ask formore guidance on error handling patterns.
252
252
253
253
Understanding the error handling process is crucial for maintaining existing behavior when you refactor the ProcessOrder method. You can have GitHub Copilot analyze the current error handling strategy and suggest a way to maintain or improve the existing behavior.
254
254
@@ -260,7 +260,7 @@ Use the following steps to complete this task:
260
260
261
261
1. Take a couple minutes to review the error handling recommendations.
262
262
263
-
GitHub Copilot should provide guidance on maintaining consistent error handling patterns across the refactored methods. This is critical because the current method has complex error handling with rollback procedures that must be preserved.
263
+
GitHub Copilot should provide guidance on maintaining consistent error handling patterns across the refactored methods. This guidance is critical because the current method has complex error handling with rollback procedures that must be preserved.
264
264
265
265
The recommendations should address:
266
266
- How to maintain the current rollback behavior (like releasing inventory on payment failures).
@@ -274,7 +274,7 @@ GitHub Copilot's Ask mode excels at analyzing complex code structures and provid
274
274
275
275
Agent mode enables you to assign complex code refactoring tasks to GitHub Copilot. The assigned tasks can include creating and/or updating multiple files. GitHub Copilot Agent processes tasks autonomously, testing and debugging updates as it works, and keeps you informed by reporting its progress in the Chat view.
276
276
277
-
In this task, you'll use GitHub Copilot Agent to systematically refactor the ProcessOrder method by extracting smaller, focused methods while preserving the existing business logic and error handling behavior.
277
+
In this task, you use GitHub Copilot Agent to systematically refactor the ProcessOrder method by extracting smaller, focused methods while preserving the existing business logic and error handling behavior.
278
278
279
279
Use the following steps to complete this task:
280
280
@@ -314,7 +314,7 @@ Use the following steps to complete this task:
314
314
315
315
The code comment should be located after the final closing brace of the ProcessOrder method and before the closing brace of the OrderProcessor class.
316
316
317
-
You'll instruct GitHub Copilot to use this location when it creates the new single-purpose methods inside the OrderProcessor class.
317
+
You can instruct GitHub Copilot to use this location when it creates the new single-purpose methods inside the OrderProcessor class.
318
318
319
319
1. Ask GitHub Copilot Agent to create stub methods that can be used to hold the extracted code.
320
320
@@ -340,7 +340,7 @@ Use the following steps to complete this task:
340
340
341
341
1. Ask GitHub Copilot Agent to refactor the ProcessOrder method.
342
342
343
-
Refactoring large methods works best when you're able to break the task down into manageable stages. In this case, the stages align to the single-process methods that you've already identified. The same approach should be applied when using an agent to refactor the code for you. Write a task that instructs the agent to refactor one section at a time, and thentest the updates before moving on to the next section.
343
+
Refactoring large methods works best when you're able to break the task down into manageable stages. In this case, the stages align to the single-process methods that you already identified. The same approach should be applied when using an agent to refactor the code for you. Write a task that instructs the agent to refactor one section at a time, and then test the updates before moving on to the next section.
344
344
345
345
However, working with GitHub Copilot Agent can be like having a developer available who can work independently on a series of assignments. In this case, the series of assignments is to refactor each code section and test the updates before moving on to the next section. In other words, you can write a single task (prompt) that asks GitHub Copilot Agent to refactor each of the code sections, testing each of the single-purpose methods before it moves on to the refactoring the next section.
346
346
@@ -356,7 +356,7 @@ Use the following steps to complete this task:
356
356
357
357
GitHub Copilot Agent should start by describing its plan forrefactoring each section of the ProcessOrder method. The plan should include a step-by-step approach for each code section that includes: moving code from the ProcessOrder method into the corresponding single-process method, replacing the extracted codein ProcessOrder with a method call, and testing the app to ensure that the refactored code works as intended.
358
358
359
-
The agent will also provide updates in the Chat view that describe its progress, including any issues it encounters. You can interact with the agent to clarify instructions or provide additional context as needed.
359
+
The agent also provides updates in the Chat view that describe its progress, including any issues it encounters. You can interact with the agent to clarify instructions or provide additional context as needed.
360
360
361
361
GitHub Copilot Agent usually asks forpermission to Build or Run the application during the refactoring process. When this occurs, select the **Continue** buttonin the Chat view to allow the agent to proceed.
362
362
@@ -448,9 +448,9 @@ GitHub Copilot Agent excels at systematic refactoring tasks that require underst
448
448
449
449
### Test the refactored e-commerce order processing code
450
450
451
-
Manual testing and verification ensures that your refactored code maintains the intended business logic and functionality. A successful refactoring process should improve code structure while producing identical behavior to the original implementation.
451
+
Manual testing and verification ensure that your refactored code maintains the intended business logic and functionality. A successful refactoring process should improve code structure while producing identical behavior to the original implementation.
452
452
453
-
In this task, you'lltest the refactored code to verify that all business logic has been preserved and that the refactoring has achieved its goals of improved maintainability and readability.
453
+
In this task, you test the refactored code to verify that all business logic is preserved and that code readability and maintainability are improved.
454
454
455
455
Use the following steps to complete this task:
456
456
@@ -465,9 +465,9 @@ Use the following steps to complete this task:
465
465
466
466
The refactored code should produce exactly the same results, demonstrating that the business logic has been preserved throughout the refactoring process.
467
467
468
-
1. Create and testadditional edge case scenarios to ensure robustness.
468
+
1. Create and testmore edge case scenarios to ensure robustness.
469
469
470
-
Create additionaltest scenarios to verify that error handling still works correctly in various edge cases. You can modify the test cases in **Program.cs** temporarily to test additional scenarios.
470
+
Create extratest scenarios to verify that error handling still works correctly in various edge cases. You can modify the test cases in **Program.cs** temporarily to test other scenarios.
471
471
472
472
For example, you can add the following code snippet before the code that displays the test summary:
473
473
@@ -491,13 +491,13 @@ Use the following steps to complete this task:
These additional tests help verify that the refactored validation logic handles edge cases correctly and that error messages remain consistent with the original implementation.
494
+
These extra tests help verify that the refactored validation logic handles edge cases correctly and that error messages remain consistent with the original implementation.
495
495
496
496
1. Run the application and verify test results.
497
497
498
498
When you run the application, you should see the test results displayed in the console, indicating whether each testcase passed or failed. Pay attention to any error messages or logs that are generated during the test runs.
499
499
500
-
For example, if you added the test 5 and test 6 scenarios listed above, the new test output and updated summary should look similar to the following:
500
+
For example, if you added the test 5 and test 6 scenarios listed above, the new test output and updated summary should look similar to the following sample:
501
501
502
502
```text
503
503
@@ -523,7 +523,7 @@ Use the following steps to complete this task:
523
523
524
524
Check the **order_audit_log.txt** file to ensure that audit logging is still functioning properly throughout the refactored methods. The most recent events are located at the bottom of the file.
525
525
526
-
The audit trail should be complete and demonstrate that logging has been preserved across all the extracted methods.
526
+
The audit trail should be complete and demonstrate that logging is preserved across all the extracted methods.
527
527
528
528
>**TIP**: The order_audit_log.txt file is created/updated in the current working directory of the application. Depending on how you choose to run the ECommerce.Console project, the working directory could be the "src/ECommerce.Console/bin/Debug/net9.0" directory rather than the "src/ECommerce.Console" directory. To generate the audit file in the "src/ECommerce.Console" directory, run the application from the Terminal using a .NET CLI command.
529
529
@@ -551,12 +551,12 @@ Use the following steps to complete this task:
Manual testing verifies that your refactoring efforts have successfully achieved the goal of improving code structure while maintaining system functionality. The refactored code now provides a much more maintainable foundation where each method has a clear, focused responsibility, making future enhancements and bug fixes significantly easier to implement.
554
+
Manual testing verifies that your refactoring efforts achieved the goal of improving code structure while maintaining system functionality. The refactored code now provides a much more maintainable foundation where each method has a clear, focused responsibility, making future enhancements and bug fixes easier to implement.
555
555
556
556
## Summary
557
557
558
558
In this exercise, you learned how to use GitHub Copilot to refactor large functions in an application. You explored the E-commerce Order Processing System, identified large functions that needed refactoring, and used GitHub Copilot to break down monolithic methods into smaller, more focused functions for improved maintainability and readability.
559
559
560
560
## Clean up
561
561
562
-
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.
562
+
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