Skip to content

Commit 69f2d13

Browse files
committed
Add AI-Assisted Code Refactoring module with introductory notebooks and exercises
1 parent f1f53e5 commit 69f2d13

File tree

3 files changed

+557
-0
lines changed

3 files changed

+557
-0
lines changed

04-refactoring/README.md

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# AI-Assisted Code Refactoring Module
2+
3+
This directory contains notebooks for learning code refactoring using AI assistance tools like GitHub Copilot. The notebooks provide hands-on exercises to improve code quality through modern programming practices.
4+
5+
## Notebooks
6+
7+
- [`refactoring - easy.ipynb`](refactoring%20-%20easy.ipynb): An introductory notebook focusing on basic refactoring principles using a simple GradeCalculator class. Perfect for beginners learning about code improvement techniques.
8+
9+
- [`refactoring - intermediate.ipynb`](refactoring%20-%20intermediate.ipynb): A more advanced notebook using a SoilDataProcessor class that demonstrates complex refactoring scenarios with real-world data processing examples.
10+
11+
## Getting Started
12+
13+
To begin the refactoring workshop:
14+
15+
1. Ensure you have access to GitHub Copilot in your development environment
16+
2. Open the notebooks in order (easy → intermediate)
17+
3. Follow the exercises and prompts within each notebook
18+
4. Try the refactoring suggestions and experiment with your own improvements
19+
20+
## Workshop Structure
21+
22+
Each notebook follows a consistent structure:
23+
24+
1. Initial code presentation
25+
2. Code analysis exercise
26+
3. Guided refactoring using Copilot
27+
4. Prompt crafting practice
28+
5. Discussion of improvements
29+
30+
## Learning Objectives
31+
32+
Through these notebooks, participants will learn:
33+
- How to identify code that needs improvement
34+
- Effective use of AI tools for code refactoring
35+
- Modern Python programming practices including:
36+
- Using pandas for data processing
37+
- Implementing proper error handling
38+
- Adding type hints and documentation
39+
- Using logging instead of print statements
40+
- Writing more maintainable code
+225
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# AI-Assisted Code Refactoring Workshop\n",
8+
"## Using GitHub Copilot for Code Improvement\n",
9+
"\n",
10+
"This notebook will guide you through practical exercises in code refactoring using AI assistance tools like GitHub Copilot."
11+
]
12+
},
13+
{
14+
"cell_type": "markdown",
15+
"metadata": {},
16+
"source": [
17+
"### Activity 1: Refactoring OOP-Based Python Code\n",
18+
"\n",
19+
"First, let's examine a sample `SoilDataProcessor` class that needs improvement. This class represents common patterns that could benefit from refactoring."
20+
]
21+
},
22+
{
23+
"cell_type": "code",
24+
"execution_count": null,
25+
"metadata": {},
26+
"outputs": [],
27+
"source": [
28+
"class GradeCalculator:\n",
29+
" def __init__(self):\n",
30+
" self.grades = []\n",
31+
" \n",
32+
" def add_grade(self, grade):\n",
33+
" \"\"\"Add a grade\"\"\"\n",
34+
" self.grades.append(grade)\n",
35+
" \n",
36+
" def get_average(self):\n",
37+
" \"\"\"Get average grade\"\"\"\n",
38+
" if len(self.grades) == 0:\n",
39+
" return 0\n",
40+
" return sum(self.grades) / len(self.grades)\n",
41+
" \n",
42+
" def print_summary(self):\n",
43+
" \"\"\"Print grade summary\"\"\"\n",
44+
" print(f\"Total grades: {len(self.grades)}\")\n",
45+
" print(f\"Average: {self.get_average():.2f}\")\n",
46+
" print(f\"Highest: {max(self.grades)}\")\n",
47+
" print(f\"Lowest: {min(self.grades)}\")"
48+
]
49+
},
50+
{
51+
"cell_type": "code",
52+
"execution_count": null,
53+
"metadata": {},
54+
"outputs": [],
55+
"source": [
56+
"# Example usage:\n",
57+
"calculator = GradeCalculator()\n",
58+
"\n",
59+
"calculator.add_grade(85)\n",
60+
"calculator.add_grade(90)\n",
61+
"calculator.add_grade(78)\n",
62+
"\n",
63+
"calculator.print_summary()"
64+
]
65+
},
66+
{
67+
"cell_type": "markdown",
68+
"metadata": {},
69+
"source": [
70+
"#### 🔍 Exercise 1: Code Analysis\n",
71+
"Take a moment to review the code above and identify potential issues:\n",
72+
"1. What are the main inefficiencies?\n",
73+
"2. Where could error handling be improved?\n",
74+
"3. How might you improve the design?"
75+
]
76+
},
77+
{
78+
"cell_type": "code",
79+
"execution_count": null,
80+
"metadata": {},
81+
"outputs": [],
82+
"source": [
83+
"# Your analysis here\n",
84+
"\n",
85+
"\"\"\"\n",
86+
"\n",
87+
"\n",
88+
"\"\"\""
89+
]
90+
},
91+
{
92+
"cell_type": "markdown",
93+
"metadata": {},
94+
"source": [
95+
"#### 💡 Exercise 2: Refactoring with Copilot\n",
96+
"Now, let's use GitHub Copilot to help refactor this code. Here are some prompts to try:\n",
97+
"\n",
98+
"1. General improvement prompt:"
99+
]
100+
},
101+
{
102+
"cell_type": "code",
103+
"execution_count": 1,
104+
"metadata": {},
105+
"outputs": [
106+
{
107+
"data": {
108+
"text/plain": [
109+
"'\\n\\nRefactor this GradeCalculator class to use pandas and follow better design principles\\n\\n'"
110+
]
111+
},
112+
"execution_count": 1,
113+
"metadata": {},
114+
"output_type": "execute_result"
115+
}
116+
],
117+
"source": [
118+
"\"\"\"\n",
119+
"\n",
120+
"Refactor this GradeCalculator class to use pandas and follow better design principles\n",
121+
"\n",
122+
"\"\"\""
123+
]
124+
},
125+
{
126+
"cell_type": "markdown",
127+
"metadata": {},
128+
"source": [
129+
"2. Specific enhancement prompt:"
130+
]
131+
},
132+
{
133+
"cell_type": "code",
134+
"execution_count": 2,
135+
"metadata": {},
136+
"outputs": [
137+
{
138+
"data": {
139+
"text/plain": [
140+
"'\\n\\nRefactor this class to:\\n1. Use pandas for efficient data processing\\n2. Implement proper error handling with custom exceptions\\n3. Add type hints and documentation\\n4. Implement logging instead of print statements\\n\\n'"
141+
]
142+
},
143+
"execution_count": 2,
144+
"metadata": {},
145+
"output_type": "execute_result"
146+
}
147+
],
148+
"source": [
149+
"\"\"\"\n",
150+
"\n",
151+
"Refactor this class to:\n",
152+
"1. Use pandas for efficient data processing\n",
153+
"2. Implement proper error handling with custom exceptions\n",
154+
"3. Add type hints and documentation\n",
155+
"4. Implement logging instead of print statements\n",
156+
"\n",
157+
"\"\"\""
158+
]
159+
},
160+
{
161+
"cell_type": "markdown",
162+
"metadata": {},
163+
"source": [
164+
"### Activity 2: Crafting Effective Refactoring Prompts\n",
165+
"\n",
166+
"Now that you've seen some example prompts, let's practice creating our own. Consider what makes a prompt effective:\n",
167+
"- Specificity about desired improvements\n",
168+
"- Clear constraints and requirements\n",
169+
"- Examples of expected behavior"
170+
]
171+
},
172+
{
173+
"cell_type": "code",
174+
"execution_count": null,
175+
"metadata": {},
176+
"outputs": [],
177+
"source": [
178+
"# Your prompts here:\n",
179+
"\"\"\"\n",
180+
"1.\n",
181+
"\n",
182+
"2.\n",
183+
"\n",
184+
"3.\n",
185+
"\"\"\""
186+
]
187+
},
188+
{
189+
"cell_type": "markdown",
190+
"metadata": {},
191+
"source": [
192+
"### Conclusion\n",
193+
"Remember these key takeaways when working with AI coding assistants:\n",
194+
"1. Start with clear, specific prompts\n",
195+
"2. Review and understand the suggested changes\n",
196+
"3. Verify that the refactored code maintains the original functionality\n",
197+
"4. Test the refactored code thoroughly\n",
198+
"5. Use AI suggestions as a starting point and apply your own judgment\n",
199+
"\n",
200+
"_Note: This is a learning exercise. In real-world applications, additional considerations like testing, documentation, and specific requirements would need to be addressed._"
201+
]
202+
}
203+
],
204+
"metadata": {
205+
"kernelspec": {
206+
"display_name": "githubcopilotworkshop2",
207+
"language": "python",
208+
"name": "python3"
209+
},
210+
"language_info": {
211+
"codemirror_mode": {
212+
"name": "ipython",
213+
"version": 3
214+
},
215+
"file_extension": ".py",
216+
"mimetype": "text/x-python",
217+
"name": "python",
218+
"nbconvert_exporter": "python",
219+
"pygments_lexer": "ipython3",
220+
"version": "3.10.14"
221+
}
222+
},
223+
"nbformat": 4,
224+
"nbformat_minor": 4
225+
}

0 commit comments

Comments
 (0)