Refactor and clean up code following best practices
Cleans and refactors code to improve readability, maintainability, and follow best practices.
/plugin marketplace add citadelgrad/scott-claude-code/plugin install scott-claude-code@scott-claude-codeclaude-sonnet-4-5misc/Clean up and refactor the following code to improve readability, maintainability, and follow best practices.
$ARGUMENTS
Naming
Functions
DRY (Don't Repeat Yourself)
Complexity
Type Safety (where applicable)
any/untyped data structuresGeneral Patterns (adapt to your language)
# Python example: Use context managers
with open('file.txt') as f:
content = f.read()
# Python example: Use comprehensions
filtered = [x for x in items if x.active]
// JavaScript/TypeScript: Use optional chaining
const value = obj?.prop?.nested
// Use nullish coalescing
const result = value ?? defaultValue
// Use destructuring
const { name, email } = user
// Use template literals
const message = `Hello, ${name}!`
// Rust: Use pattern matching
match value {
Some(x) => println!("{}", x),
None => println!("No value"),
}
// Use Result for error handling
let result = operation()?;
Framework-Specific Patterns
React/Component Frameworks
Backend Frameworks
Extract Function
# Before: Long function
def process():
# 50 lines of code
# After: Broken into smaller functions
def validate():
pass
def transform():
pass
def save():
pass
def process():
validate()
data = transform()
save(data)
Replace Conditional with Polymorphism/Strategy
# Before: Multiple conditionals
if type == 'A':
return process_a()
elif type == 'B':
return process_b()
# After: Strategy pattern
processors = {
'A': process_a,
'B': process_b
}
return processors[type]()
Introduce Parameter Object
# Before: Many parameters
def create(name, email, age, address):
pass
# After: Parameter object
class UserData:
def __init__(self, name, email, age, address):
self.name = name
self.email = email
self.age = age
self.address = address
def create(user_data: UserData):
pass
Remove Dead Code
Improve Error Handling
# Before: Generic catch
try:
do_something()
except:
print("Error")
# After: Specific error handling
try:
do_something()
except ValidationError as e:
handle_validation(e)
except Exception as e:
logger.error('Unexpected error', exc_info=e)
raise
Consistent Formatting
Better Comments
Server vs Client Components (Next.js/React Server Components)
Proper Data Fetching (adapt to your framework)
Focus on practical improvements that make code more maintainable without over-engineering. Balance clean code with pragmatism, following the conventions and best practices of your chosen language and framework.