Prompt file imported from guigui42/mcp-vosdroits (
.github/prompts/refactor-code.prompt.md). Copyright stays with the author.
Refactor Code
Refactor the specified code to improve quality, maintainability, and adherence to best practices.
Refactoring Goals
Ask the user which areas to focus on if not specified:
- Simplify complexity - Reduce nested logic, improve readability
- Improve error handling - Better error messages, proper wrapping
- Extract functions - Break down large functions
- Reduce duplication - DRY principle
- Improve naming - More descriptive names
- Add type safety - Replace
anywith specific types - Optimize performance - Reduce allocations, improve efficiency
- Improve testability - Better separation of concerns
Refactoring Process
1. Analyze Current Code
- Identify code smells
- Find duplication
- Locate complex functions
- Check error handling patterns
- Review naming conventions
2. Plan Refactoring
- Ensure tests exist before refactoring
- Refactor in small, incremental steps
- Run tests after each change
- Keep commits focused
3. Common Refactorings
Extract Function
Break down large functions:
// Before
func ProcessData(data []byte) error {
// 50 lines of code
}
// After
func ProcessData(data []byte) error {
validated, err := validateData(data)
if err != nil {
return err
}
transformed := transformData(validated)
return saveData(transformed)
}
Simplify Conditionals
Use early returns:
// Before
func Process(input string) error {
if input != "" {
// lots of nested code
} else {
return errors.New("empty input")
}
}
// After
func Process(input string) error {
if input == "" {
return errors.New("empty input")
}
// un-nested code
}
Reduce Duplication
Extract common code:
// Before - duplication in multiple functions
func HandleA() { /* setup code */ /* operation A */ /* cleanup */ }
func HandleB() { /* setup code */ /* operation B */ /* cleanup */ }
// After
func withSetup(operation func() error) error {
// setup code
defer cleanup()
return operation()
}
Improve Error Handling
Add context to errors:
// Before
if err != nil {
return err
}
// After
if err != nil {
return fmt.Errorf("failed to process data: %w", err)
}
Interface Extraction
For better testability:
// Before - hard to test
type Service struct {
httpClient *http.Client
}
// After - easy to mock
type HTTPClient interface {
Do(*http.Request) (*http.Response, error)
}
type Service struct {
client HTTPClient
}
4. Verify Refactoring
After each refactoring:
- Run tests:
go test ./... - Check for races:
go test -race ./... - Verify formatting:
gofmt -s -w . - Run linter:
golangci-lint run - Ensure behavior unchanged
Refactoring Principles
- Don't change behavior - Refactoring should not change functionality
- Test first - Ensure good test coverage before refactoring
- Small steps - Make incremental changes
- One thing at a time - Focus on one improvement per refactoring
- Keep it simple - Don't over-engineer
Common Code Smells to Address
- Long functions - Extract smaller functions
- Deep nesting - Use early returns
- Magic numbers - Use named constants
- Poor names - Rename to be descriptive
- Duplicate code - Extract common functionality
- Large structs - Split into smaller types
- Too many parameters - Use struct or context
- Global variables - Use dependency injection
Provide clear explanations for each refactoring decision and show before/after comparisons.