Skip to content

Commit dfd87c3

Browse files
committed
adding Ex8 - refactor large functions
1 parent 3f6623b commit dfd87c3

3 files changed

Lines changed: 121 additions & 0 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net9.0</TargetFramework>
6+
<RootNamespace>refactor_large_functions</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
</Project>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// See https://aka.ms/new-console-template for more information
2+
Console.WriteLine("Hello, World!");
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<!-- ---
2+
lab:
3+
title: 'Exercise - Refactor large functions using GitHub Copilot'
4+
description: 'Learn how to analyze a complex codebase and consolidate duplicated code logic using GitHub Copilot tools.'
5+
--- -->
6+
7+
# Refactor large functions using GitHub Copilot
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.
10+
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+
13+
This exercise should take approximately **30** minutes to complete.
14+
15+
> **IMPORTANT**: To complete this exercise, you must provide your own GitHub account and GitHub Copilot subscription. If you don't have a GitHub account, you can <a href="https://github.com/" target="_blank">sign up</a> for a free individual account and use a GitHub Copilot Free plan to complete the exercise. If you have access to a GitHub Copilot Pro, GitHub Copilot Pro+, GitHub Copilot Business, or GitHub Copilot Enterprise subscription from within your lab environment, you can use your existing GitHub Copilot subscription to complete this exercise.
16+
17+
## Before you start
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.
20+
21+
### Configure your lab environment
22+
23+
If you're using a local PC as a lab environment for this exercise:
24+
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>.
26+
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>.
28+
29+
If you're using a hosted lab environment for this exercise:
30+
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>.
32+
33+
- Open a command terminal and then run the following commands:
34+
35+
To ensure that Visual Studio Code is configured to use the correct version of .NET, run the following command:
36+
37+
```bash
38+
39+
dotnet nuget add source https://api.nuget.org/v3/index.json -n nuget.org
40+
41+
```
42+
43+
To ensure that Git is configured to use your name and email address, update the following commands with your information, and then run the commands:
44+
45+
```bash
46+
47+
git config --global user.name "John Doe"
48+
49+
```
50+
51+
```bash
52+
53+
git config --global user.email johndoe@example.com
54+
55+
```
56+
57+
### Download sample code project
58+
59+
Use the following steps to download the sample project and open it in Visual Studio Code:
60+
61+
1. Open a browser window in your lab environment.
62+
63+
1. To download a zip file containing the sample app project, open the following URL in your browser: [GitHub Copilot lab - consolidate duplicate code](https://github.com/MicrosoftLearning/mslearn-github-copilot-dev/raw/refs/heads/main/DownloadableCodeProjects/Downloads/GHCopilotEx8LabApps.zip)
64+
65+
The zip file is named **GHCopilotEx8LabApps.zip**.
66+
67+
1. Extract the files from the **GHCopilotEx8LabApps.zip** file.
68+
69+
For example:
70+
71+
1. Navigate to the downloads folder in your lab environment.
72+
73+
1. Right-click *GHCopilotEx8LabApps.zip*, and then select **Extract all**.
74+
75+
1. Select **Show extracted files when complete**, and then select **Extract**.
76+
77+
1. Copy the **GHCopilotEx8LabApps** folder to a location that's easy to access, such as your Windows Desktop folder.
78+
79+
1. Open the **GHCopilotEx8LabApps** folder in Visual Studio Code.
80+
81+
For example:
82+
83+
1. Open Visual Studio Code in your lab environment.
84+
85+
1. In Visual Studio Code, on the **File** menu, select **Open Folder**.
86+
87+
1. Navigate to the Windows Desktop folder, select **GHCopilotEx8LabApps** and then select **Select Folder**.
88+
89+
1. In the Visual Studio Code SOLUTION EXPLORER view, verify the following solution structure:
90+
91+
- GHCopilotEx8LabApps\
92+
- ?
93+
94+
## Exercise scenario
95+
96+
You're a software developer working for a consulting firm. Your clients need help refactoring large functions in legacy applications. Your goal is to code readability and maintainability while preserving the existing functionality. You're assigned to the following app:
97+
98+
- ?: This is an ? app that ?. It includes ?.
99+
100+
This exercise includes the following tasks:
101+
102+
1. Review the ? codebase manually.
103+
1. Identify potential refactoring opportunities in large functions using GitHub Copilot Chat (Ask mode).
104+
1. Refactor large functions into smaller, more manageable functions using GitHub Copilot Chat (Agent mode).
105+
1. Test the refactored ? code.
106+
107+
### Review the ? codebase manually
108+

0 commit comments

Comments
 (0)