Simplify overly complex Python code. Use when user asks to simplify, refactor, clean up, make more readable, reduce complexity, improve code quality, find code smells, detect duplicates, or analyze coupling in Python code. Triggers on requests like "simplify this code", "this is too complex", "make this more readable", "refactor this", "clean this up", "find issues", "analyze this codebase", or when reviewing code that exhibits complexity anti-patterns. For Django-specific analysis, use the dja…
Use when user asks to simplify, refactor, clean up, make more readable, reduce complexity, improve code quality, find code smells, detect duplicates, or analyze coupling in Python code.
Triggers on requests like "simplify this code", "this is too complex", "make this more readable", "refactor this", "clean this up", "find issues", "analyze this codebase", or when reviewing code that exhibits complexity anti-patterns.
For Django-specific analysis, use the django-simplifier skill instead.
Similar popular skills
Related neighbors and high-traction skills in the same topics — useful to compare before installing.
# Before: Complex inline condition
if user.age >= 18 and user.country in ALLOWED and not user.banned:
# After: Named condition
is_eligible = user.age >= 18 and user.country in ALLOWED and not user.banned
if is_eligible:
Early Returns
# Before: Deep nesting
def process(data):
if data:
if data.valid:
if data.ready:
return compute(data)
return None
# After: Guard clauses
def process(data):
if not data or not data.valid or not data.ready:
return None
return compute(data)
Comprehensions
# Before: Manual loop
result = []
for item in items:
if item.active:
result.append(item.name)
# After: List comprehension
result = [item.name for item in items if item.active]
Dictionary Techniques
# Before: Verbose key checking
if key in d:
value = d[key]
else:
value = default
# After: get() with default
value = d.get(key, default)
# Before: Manual grouping
groups = {}
for item in items:
if item.category not in groups:
groups[item.category] = []
groups[item.category].append(item)
# After: defaultdict
from collections import defaultdict
groups = defaultdict(list)
for item in items:
groups[item.category].append(item)
Context Managers
# Before: Manual cleanup
f = open('file.txt')
try:
data = f.read()
finally:
f.close()
# After: with statement
with open('file.txt') as f:
data = f.read()
Over-Engineering Anti-Patterns
Pattern
Problem
Solution
Single-impl interface
Abstract class with one subclass
Merge or wait for need
Unnecessary factory
Factory that creates one type
Direct instantiation
Premature strategy
Strategy pattern with one strategy
Simple function
Thin wrapper
Class that just delegates
Use wrapped class directly
Speculative generality
Code for "future needs"
Delete it (YAGNI)
Deep inheritance
4+ levels of inheritance
Composition over inheritance
Code Smells Quick Reference
Smell
Detection
Fix
Mutable default
def f(x=[])
Use None, create inside
Bare except
except:
except Exception:
God class
15+ methods, 10+ attrs
Split into focused classes
Long function
50+ lines
Extract helper functions
Deep nesting
4+ levels
Early returns, extract
Feature envy
Method uses other class more
Move method
Magic numbers
Unexplained numeric literals
Named constants
Script Reference
Script
What It Detects
analyze_complexity.py
Cyclomatic complexity, cognitive complexity, nesting depth, function length, parameter count, class size
findcodesmells.py
Mutable defaults, bare excepts, magic numbers, type comparisons, god classes, data classes, boolean blindness