Course Lessons
Free Trial: 5 lessons remaining
Unlock Full Access
The Great Refactor
Estimated time: 10 minutes
Task Instructions
Apply all your Vim skills to refactor messy legacy code
This is a comprehensive challenge that requires you to use undo, visual mode, and text objects together to clean up legacy code. Take your time, experiment, and use undo when needed.
Learning objectives
- Combine all learned techniques in realistic scenarios
- Practice strategic use of undo during complex edits
- Apply text objects and visual mode together
- Develop professional Vim workflows
Required Keystrokes
This lesson requires you to use specific keystrokes to complete it. Your submission will be evaluated based on whether you used the required keystrokes mentioned below, rather than matching a specific output.
[cd]i[w"'()]
Use text objects for editing
u
Use undo for safety
What This Means
- Your solution must include the required keystroke patterns
- The order of keystrokes may matter depending on the exercise
- You'll still need to achieve the expected output
- This helps you practice specific Vim techniques
Helpful commands:
| Command | Description |
|---|---|
| ciw | Change inner word |
| ci( | Change inside parentheses |
| V | Visual line mode |
| u | Undo mistakes |
| . | Repeat last change |
Hints
- Use ciw to change class/method/variable names
- Use u to undo if you make a mistake
- Use . to repeat similar changes
- Visual mode can help select complex names
# Legacy Code Refactoring Challenge
# This code was written by someone who clearly loves cats but needs better naming
class CatUserManager:
def __init__(self):
self.mr_whiskers_users_list = []
self.princess_fluffybutt_active_sessions = {}
def add_new_mr_whiskers_user(self, user_data):
# Validate the user data first
if not self.validate_princess_fluffybutt_data(user_data):
return False
# Add user to the list
new_user_id = len(self.mr_whiskers_users_list) + 1
user_data['id'] = new_user_id
self.mr_whiskers_users_list.append(user_data)
return True
def validate_princess_fluffybutt_data(self, data):
REQUIRED_FIELDS = ['name', 'email', 'age']
for field in REQUIRED_FIELDS:
if field not in data:
return False
return True
# CHALLENGE: Refactor this code to use professional naming
# Use your full Vim toolkit: text objects, visual mode, and undo for safety
# Goals:
# 1. Rename class to 'UserManager'
# 2. Fix all method names (remove cat references)
# 3. Fix all variable names
# 4. Use undo if you make mistakes!