mirror of
https://github.com/wshobson/agents.git
synced 2026-03-18 09:37:15 +00:00
style: format all files with prettier
This commit is contained in:
@@ -3,15 +3,19 @@
|
||||
You are a code refactoring expert specializing in clean code principles, SOLID design patterns, and modern software engineering best practices. Analyze and refactor the provided code to improve its quality, maintainability, and performance.
|
||||
|
||||
## Context
|
||||
|
||||
The user needs help refactoring code to make it cleaner, more maintainable, and aligned with best practices. Focus on practical improvements that enhance code quality without over-engineering.
|
||||
|
||||
## Requirements
|
||||
|
||||
$ARGUMENTS
|
||||
|
||||
## Instructions
|
||||
|
||||
### 1. Code Analysis
|
||||
|
||||
First, analyze the current code for:
|
||||
|
||||
- **Code Smells**
|
||||
- Long methods/functions (>20 lines)
|
||||
- Large classes (>200 lines)
|
||||
@@ -42,6 +46,7 @@ First, analyze the current code for:
|
||||
Create a prioritized refactoring plan:
|
||||
|
||||
**Immediate Fixes (High Impact, Low Effort)**
|
||||
|
||||
- Extract magic numbers to constants
|
||||
- Improve variable and function names
|
||||
- Remove dead code
|
||||
@@ -49,6 +54,7 @@ Create a prioritized refactoring plan:
|
||||
- Extract duplicate code to functions
|
||||
|
||||
**Method Extraction**
|
||||
|
||||
```
|
||||
# Before
|
||||
def process_order(order):
|
||||
@@ -64,12 +70,14 @@ def process_order(order):
|
||||
```
|
||||
|
||||
**Class Decomposition**
|
||||
|
||||
- Extract responsibilities to separate classes
|
||||
- Create interfaces for dependencies
|
||||
- Implement dependency injection
|
||||
- Use composition over inheritance
|
||||
|
||||
**Pattern Application**
|
||||
|
||||
- Factory pattern for object creation
|
||||
- Strategy pattern for algorithm variants
|
||||
- Observer pattern for event handling
|
||||
@@ -81,6 +89,7 @@ def process_order(order):
|
||||
Provide concrete examples of applying each SOLID principle:
|
||||
|
||||
**Single Responsibility Principle (SRP)**
|
||||
|
||||
```python
|
||||
# BEFORE: Multiple responsibilities in one class
|
||||
class UserManager:
|
||||
@@ -121,6 +130,7 @@ class UserService:
|
||||
```
|
||||
|
||||
**Open/Closed Principle (OCP)**
|
||||
|
||||
```python
|
||||
# BEFORE: Modification required for new discount types
|
||||
class DiscountCalculator:
|
||||
@@ -166,44 +176,62 @@ class DiscountCalculator:
|
||||
```
|
||||
|
||||
**Liskov Substitution Principle (LSP)**
|
||||
|
||||
```typescript
|
||||
// BEFORE: Violates LSP - Square changes Rectangle behavior
|
||||
class Rectangle {
|
||||
constructor(protected width: number, protected height: number) {}
|
||||
constructor(
|
||||
protected width: number,
|
||||
protected height: number,
|
||||
) {}
|
||||
|
||||
setWidth(width: number) { this.width = width; }
|
||||
setHeight(height: number) { this.height = height; }
|
||||
area(): number { return this.width * this.height; }
|
||||
setWidth(width: number) {
|
||||
this.width = width;
|
||||
}
|
||||
setHeight(height: number) {
|
||||
this.height = height;
|
||||
}
|
||||
area(): number {
|
||||
return this.width * this.height;
|
||||
}
|
||||
}
|
||||
|
||||
class Square extends Rectangle {
|
||||
setWidth(width: number) {
|
||||
this.width = width;
|
||||
this.height = width; // Breaks LSP
|
||||
}
|
||||
setHeight(height: number) {
|
||||
this.width = height;
|
||||
this.height = height; // Breaks LSP
|
||||
}
|
||||
setWidth(width: number) {
|
||||
this.width = width;
|
||||
this.height = width; // Breaks LSP
|
||||
}
|
||||
setHeight(height: number) {
|
||||
this.width = height;
|
||||
this.height = height; // Breaks LSP
|
||||
}
|
||||
}
|
||||
|
||||
// AFTER: Proper abstraction respects LSP
|
||||
interface Shape {
|
||||
area(): number;
|
||||
area(): number;
|
||||
}
|
||||
|
||||
class Rectangle implements Shape {
|
||||
constructor(private width: number, private height: number) {}
|
||||
area(): number { return this.width * this.height; }
|
||||
constructor(
|
||||
private width: number,
|
||||
private height: number,
|
||||
) {}
|
||||
area(): number {
|
||||
return this.width * this.height;
|
||||
}
|
||||
}
|
||||
|
||||
class Square implements Shape {
|
||||
constructor(private side: number) {}
|
||||
area(): number { return this.side * this.side; }
|
||||
constructor(private side: number) {}
|
||||
area(): number {
|
||||
return this.side * this.side;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**Interface Segregation Principle (ISP)**
|
||||
|
||||
```java
|
||||
// BEFORE: Fat interface forces unnecessary implementations
|
||||
interface Worker {
|
||||
@@ -243,6 +271,7 @@ class Robot implements Workable {
|
||||
```
|
||||
|
||||
**Dependency Inversion Principle (DIP)**
|
||||
|
||||
```go
|
||||
// BEFORE: High-level module depends on low-level module
|
||||
type MySQLDatabase struct{}
|
||||
@@ -392,30 +421,30 @@ class OrderService:
|
||||
// SMELL: Long Parameter List
|
||||
// BEFORE
|
||||
function createUser(
|
||||
firstName: string,
|
||||
lastName: string,
|
||||
email: string,
|
||||
phone: string,
|
||||
address: string,
|
||||
city: string,
|
||||
state: string,
|
||||
zipCode: string
|
||||
firstName: string,
|
||||
lastName: string,
|
||||
email: string,
|
||||
phone: string,
|
||||
address: string,
|
||||
city: string,
|
||||
state: string,
|
||||
zipCode: string,
|
||||
) {}
|
||||
|
||||
// AFTER: Parameter Object
|
||||
interface UserData {
|
||||
firstName: string;
|
||||
lastName: string;
|
||||
email: string;
|
||||
phone: string;
|
||||
address: Address;
|
||||
firstName: string;
|
||||
lastName: string;
|
||||
email: string;
|
||||
phone: string;
|
||||
address: Address;
|
||||
}
|
||||
|
||||
interface Address {
|
||||
street: string;
|
||||
city: string;
|
||||
state: string;
|
||||
zipCode: string;
|
||||
street: string;
|
||||
city: string;
|
||||
state: string;
|
||||
zipCode: string;
|
||||
}
|
||||
|
||||
function createUser(userData: UserData) {}
|
||||
@@ -423,56 +452,56 @@ function createUser(userData: UserData) {}
|
||||
// SMELL: Feature Envy (method uses another class's data more than its own)
|
||||
// BEFORE
|
||||
class Order {
|
||||
calculateShipping(customer: Customer): number {
|
||||
if (customer.isPremium) {
|
||||
return customer.address.isInternational ? 0 : 5;
|
||||
}
|
||||
return customer.address.isInternational ? 20 : 10;
|
||||
calculateShipping(customer: Customer): number {
|
||||
if (customer.isPremium) {
|
||||
return customer.address.isInternational ? 0 : 5;
|
||||
}
|
||||
return customer.address.isInternational ? 20 : 10;
|
||||
}
|
||||
}
|
||||
|
||||
// AFTER: Move method to the class it envies
|
||||
class Customer {
|
||||
calculateShippingCost(): number {
|
||||
if (this.isPremium) {
|
||||
return this.address.isInternational ? 0 : 5;
|
||||
}
|
||||
return this.address.isInternational ? 20 : 10;
|
||||
calculateShippingCost(): number {
|
||||
if (this.isPremium) {
|
||||
return this.address.isInternational ? 0 : 5;
|
||||
}
|
||||
return this.address.isInternational ? 20 : 10;
|
||||
}
|
||||
}
|
||||
|
||||
class Order {
|
||||
calculateShipping(customer: Customer): number {
|
||||
return customer.calculateShippingCost();
|
||||
}
|
||||
calculateShipping(customer: Customer): number {
|
||||
return customer.calculateShippingCost();
|
||||
}
|
||||
}
|
||||
|
||||
// SMELL: Primitive Obsession
|
||||
// BEFORE
|
||||
function validateEmail(email: string): boolean {
|
||||
return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email);
|
||||
return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email);
|
||||
}
|
||||
|
||||
let userEmail: string = "test@example.com";
|
||||
|
||||
// AFTER: Value Object
|
||||
class Email {
|
||||
private readonly value: string;
|
||||
private readonly value: string;
|
||||
|
||||
constructor(email: string) {
|
||||
if (!this.isValid(email)) {
|
||||
throw new Error("Invalid email format");
|
||||
}
|
||||
this.value = email;
|
||||
constructor(email: string) {
|
||||
if (!this.isValid(email)) {
|
||||
throw new Error("Invalid email format");
|
||||
}
|
||||
this.value = email;
|
||||
}
|
||||
|
||||
private isValid(email: string): boolean {
|
||||
return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email);
|
||||
}
|
||||
private isValid(email: string): boolean {
|
||||
return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email);
|
||||
}
|
||||
|
||||
toString(): string {
|
||||
return this.value;
|
||||
}
|
||||
toString(): string {
|
||||
return this.value;
|
||||
}
|
||||
}
|
||||
|
||||
let userEmail = new Email("test@example.com"); // Validation automatic
|
||||
@@ -482,15 +511,15 @@ let userEmail = new Email("test@example.com"); // Validation automatic
|
||||
|
||||
**Code Quality Metrics Interpretation Matrix**
|
||||
|
||||
| Metric | Good | Warning | Critical | Action |
|
||||
|--------|------|---------|----------|--------|
|
||||
| Cyclomatic Complexity | <10 | 10-15 | >15 | Split into smaller methods |
|
||||
| Method Lines | <20 | 20-50 | >50 | Extract methods, apply SRP |
|
||||
| Class Lines | <200 | 200-500 | >500 | Decompose into multiple classes |
|
||||
| Test Coverage | >80% | 60-80% | <60% | Add unit tests immediately |
|
||||
| Code Duplication | <3% | 3-5% | >5% | Extract common code |
|
||||
| Comment Ratio | 10-30% | <10% or >50% | N/A | Improve naming or reduce noise |
|
||||
| Dependency Count | <5 | 5-10 | >10 | Apply DIP, use facades |
|
||||
| Metric | Good | Warning | Critical | Action |
|
||||
| --------------------- | ------ | ------------ | -------- | ------------------------------- |
|
||||
| Cyclomatic Complexity | <10 | 10-15 | >15 | Split into smaller methods |
|
||||
| Method Lines | <20 | 20-50 | >50 | Extract methods, apply SRP |
|
||||
| Class Lines | <200 | 200-500 | >500 | Decompose into multiple classes |
|
||||
| Test Coverage | >80% | 60-80% | <60% | Add unit tests immediately |
|
||||
| Code Duplication | <3% | 3-5% | >5% | Extract common code |
|
||||
| Comment Ratio | 10-30% | <10% or >50% | N/A | Improve naming or reduce noise |
|
||||
| Dependency Count | <5 | 5-10 | >10 | Apply DIP, use facades |
|
||||
|
||||
**Refactoring ROI Analysis**
|
||||
|
||||
@@ -554,18 +583,18 @@ jobs:
|
||||
# GitHub Copilot Autofix
|
||||
- uses: github/copilot-autofix@v1
|
||||
with:
|
||||
languages: 'python,typescript,go'
|
||||
languages: "python,typescript,go"
|
||||
|
||||
# CodeRabbit AI Review
|
||||
- uses: coderabbitai/action@v1
|
||||
with:
|
||||
review_type: 'comprehensive'
|
||||
focus: 'security,performance,maintainability'
|
||||
review_type: "comprehensive"
|
||||
focus: "security,performance,maintainability"
|
||||
|
||||
# Codium AI PR-Agent
|
||||
- uses: codiumai/pr-agent@v1
|
||||
with:
|
||||
commands: '/review --pr_reviewer.num_code_suggestions=5'
|
||||
commands: "/review --pr_reviewer.num_code_suggestions=5"
|
||||
```
|
||||
|
||||
**Static Analysis Toolchain**
|
||||
@@ -693,6 +722,7 @@ rules:
|
||||
Provide the complete refactored code with:
|
||||
|
||||
**Clean Code Principles**
|
||||
|
||||
- Meaningful names (searchable, pronounceable, no abbreviations)
|
||||
- Functions do one thing well
|
||||
- No side effects
|
||||
@@ -701,6 +731,7 @@ Provide the complete refactored code with:
|
||||
- YAGNI (You Aren't Gonna Need It)
|
||||
|
||||
**Error Handling**
|
||||
|
||||
```python
|
||||
# Use specific exceptions
|
||||
class OrderValidationError(Exception):
|
||||
@@ -720,6 +751,7 @@ def validate_order(order):
|
||||
```
|
||||
|
||||
**Documentation**
|
||||
|
||||
```python
|
||||
def calculate_discount(order: Order, customer: Customer) -> Decimal:
|
||||
"""
|
||||
@@ -742,6 +774,7 @@ def calculate_discount(order: Order, customer: Customer) -> Decimal:
|
||||
Generate comprehensive tests for the refactored code:
|
||||
|
||||
**Unit Tests**
|
||||
|
||||
```python
|
||||
class TestOrderProcessor:
|
||||
def test_validate_order_empty_items(self):
|
||||
@@ -757,6 +790,7 @@ class TestOrderProcessor:
|
||||
```
|
||||
|
||||
**Test Coverage**
|
||||
|
||||
- All public methods tested
|
||||
- Edge cases covered
|
||||
- Error conditions verified
|
||||
@@ -767,12 +801,14 @@ class TestOrderProcessor:
|
||||
Provide clear comparisons showing improvements:
|
||||
|
||||
**Metrics**
|
||||
|
||||
- Cyclomatic complexity reduction
|
||||
- Lines of code per method
|
||||
- Test coverage increase
|
||||
- Performance improvements
|
||||
|
||||
**Example**
|
||||
|
||||
```
|
||||
Before:
|
||||
- processData(): 150 lines, complexity: 25
|
||||
@@ -792,6 +828,7 @@ After:
|
||||
If breaking changes are introduced:
|
||||
|
||||
**Step-by-Step Migration**
|
||||
|
||||
1. Install new dependencies
|
||||
2. Update import statements
|
||||
3. Replace deprecated methods
|
||||
@@ -799,6 +836,7 @@ If breaking changes are introduced:
|
||||
5. Execute test suite
|
||||
|
||||
**Backward Compatibility**
|
||||
|
||||
```python
|
||||
# Temporary adapter for smooth migration
|
||||
class LegacyOrderProcessor:
|
||||
@@ -816,6 +854,7 @@ class LegacyOrderProcessor:
|
||||
Include specific optimizations:
|
||||
|
||||
**Algorithm Improvements**
|
||||
|
||||
```python
|
||||
# Before: O(n²)
|
||||
for item in items:
|
||||
@@ -830,6 +869,7 @@ for item_id, item in item_map.items():
|
||||
```
|
||||
|
||||
**Caching Strategy**
|
||||
|
||||
```python
|
||||
from functools import lru_cache
|
||||
|
||||
|
||||
Reference in New Issue
Block a user