Skip to content

Commit aaab399

Browse files
committed
acrolinx updates - large functions
1 parent a6cd662 commit aaab399

1 file changed

Lines changed: 27 additions & 27 deletions

File tree

Instructions/Labs/LAB_AK_08_refactor_large_functions.md

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

77
# Refactor large functions using GitHub Copilot
88

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

1111
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.
1212

@@ -16,19 +16,19 @@ This exercise should take approximately **30** minutes to complete.
1616
1717
## Before you start
1818

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

2121
### Configure your lab environment
2222

2323
If you're using a local PC as a lab environment for this exercise:
2424

25-
- For help configuring your local PC as your lab environment, open the following link in a browser: <a href="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: <a href="https://go.microsoft.com/fwlink/?linkid=2320147" target="_blank">Configure your lab environment resources</a>.
2626

27-
- For help enabling your GitHub Copilot subscription in Visual Studio Code, open the following link in a browser: <a href="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: <a href="https://go.microsoft.com/fwlink/?linkid=2320158" target="_blank">Enable GitHub Copilot within Visual Studio Code</a>.
2828

2929
If you're using a hosted lab environment for this exercise:
3030

31-
- For help enabling your GitHub Copilot subscription in Visual Studio Code, paste the following URL into a browser's site navigation bar: <a href="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: <a href="https://go.microsoft.com/fwlink/?linkid=2320158" target="_blank">Enable GitHub Copilot within Visual Studio Code</a>.
3232

3333
- To ensure that the .NET SDK is configured to use the official NuGet.org repository as a source for downloading and restoring packages:
3434

@@ -92,7 +92,7 @@ Use the following steps to download the sample project and open it in Visual Stu
9292
9393
## Exercise scenario
9494
95-
You're a software developer working for a consulting firm. Your clients need help refactoring large functions in 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 for a consulting firm. Your clients need help with refactoring large functions in legacy applications. Your goal is to improve code readability and maintainability while preserving the existing functionality. You're assigned to the following app:
9696
9797
- 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.
9898
@@ -107,7 +107,7 @@ This exercise includes the following tasks:
107107
108108
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.
109109

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

112112
Use the following steps to complete this task:
113113

@@ -147,13 +147,13 @@ Use the following steps to complete this task:
147147

148148
1. Take a minute to review the **OrderProcessor** class.
149149

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 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 grew in complexity over time, making it difficult to read, test, and maintain.
151151

152152
1. Right-click the **ProcessOrder** method, and then select **Copilot** > **Explain**.
153153

154154
If prompted to **Select an enclosing range to explain**, select **ProcessOrder**.
155155

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

158158
1. Take a couple minutes to review GitHub Copilot's explanation.
159159
@@ -171,7 +171,7 @@ Use the following steps to complete this task:
171171
dotnet run
172172
```
173173
174-
1. Review the console output that's generated when the application runs.
174+
1. Review the console output generated by the application.
175175
176176
The application generates output for four test cases. Each test case demonstrates a different scenario:
177177
@@ -186,13 +186,13 @@ Use the following steps to complete this task:
186186
187187
The ProcessOrder method has several distinct responsibilities. Each of the corresponding code sections could be extracted into a separate method.
188188
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.
190190
191191
### Identify refactoring opportunities using GitHub Copilot Chat (Ask mode)
192192
193193
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.
194194

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

197197
Use the following steps to complete this task:
198198

@@ -248,7 +248,7 @@ Use the following steps to complete this task:
248248
- Guidance on how to handle errors consistently across methods.
249249
- Explanations of how the refactored structure improves maintainability.
250250
251-
1. Ask for additional guidance on error handling patterns.
251+
1. Ask for more guidance on error handling patterns.
252252
253253
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.
254254
@@ -260,7 +260,7 @@ Use the following steps to complete this task:
260260
261261
1. Take a couple minutes to review the error handling recommendations.
262262
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.
264264
265265
The recommendations should address:
266266
- 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
274274
275275
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.
276276
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.
278278
279279
Use the following steps to complete this task:
280280
@@ -314,7 +314,7 @@ Use the following steps to complete this task:
314314
315315
The code comment should be located after the final closing brace of the ProcessOrder method and before the closing brace of the OrderProcessor class.
316316
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.
318318
319319
1. Ask GitHub Copilot Agent to create stub methods that can be used to hold the extracted code.
320320
@@ -340,7 +340,7 @@ Use the following steps to complete this task:
340340
341341
1. Ask GitHub Copilot Agent to refactor the ProcessOrder method.
342342
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 then test 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.
344344
345345
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.
346346
@@ -356,7 +356,7 @@ Use the following steps to complete this task:
356356
357357
GitHub Copilot Agent should start by describing its plan for refactoring 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 code in ProcessOrder with a method call, and testing the app to ensure that the refactored code works as intended.
358358
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.
360360
361361
GitHub Copilot Agent usually asks for permission to Build or Run the application during the refactoring process. When this occurs, select the **Continue** button in the Chat view to allow the agent to proceed.
362362
@@ -448,9 +448,9 @@ GitHub Copilot Agent excels at systematic refactoring tasks that require underst
448448
449449
### Test the refactored e-commerce order processing code
450450
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.
452452
453-
In this task, you'll test 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.
454454
455455
Use the following steps to complete this task:
456456
@@ -465,9 +465,9 @@ Use the following steps to complete this task:
465465
466466
The refactored code should produce exactly the same results, demonstrating that the business logic has been preserved throughout the refactoring process.
467467
468-
1. Create and test additional edge case scenarios to ensure robustness.
468+
1. Create and test more edge case scenarios to ensure robustness.
469469
470-
Create additional test 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 extra test 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.
471471
472472
For example, you can add the following code snippet before the code that displays the test summary:
473473
@@ -491,13 +491,13 @@ Use the following steps to complete this task:
491491
testResults.Add($"Test 6: {(result6.IsSuccess ? "FAILED" : "PASSED")} - Should reject invalid shipping address");
492492
```
493493
494-
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.
495495
496496
1. Run the application and verify test results.
497497
498498
When you run the application, you should see the test results displayed in the console, indicating whether each test case passed or failed. Pay attention to any error messages or logs that are generated during the test runs.
499499
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:
501501
502502
```text
503503
@@ -523,7 +523,7 @@ Use the following steps to complete this task:
523523
524524
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.
525525
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.
527527
528528
> **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.
529529
@@ -551,12 +551,12 @@ Use the following steps to complete this task:
551551
- Unexpected Errors:
552552
- LogUnexpectedError(string orderId, string error)
553553
554-
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.
555555
556556
## Summary
557557
558558
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.
559559
560560
## Clean up
561561
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

Comments
 (0)