| name | language-optimization |
| description | Optimize code for readability, performance, maintainability, and security across Python. Use when asked to improve code quality, optimize performance, add type safety, or refactor for idioms |
Language Optimization
Optimize code across languages following universal principles and language-specific idioms.
Workflow
Think through optimization systematically:
- Analyze: Identify issues - lint errors, type errors, performance bottlenecks, security gaps
- Profile first: Measure before optimizing performance (never optimize without data)
- Apply fixes: Follow language-specific standards from
instructions/
- Verify: All tests pass, metrics improved, no regressions
Universal Principles
- KISS: Simple over clever; readability first
- YAGNI: Don't build before needed; no premature optimization
- DRY: Extract repeated logic; single source of truth
- Fail Fast: Validate early; specific error messages
- Security: No secrets in code; validate at boundaries
Optimization Targets (Priority Order)
- Correctness: Fix bugs, handle edge cases
- Type safety: Add/improve type annotations
- Readability: Clear names, reduce nesting, simplify logic
- Performance: Only after profiling identifies bottlenecks
- Security: Input validation, secret management, dependency audit
<language_specific>
Python
- Standards:
.github/instructions/python.instructions.md
- Always: type hints on public functions,
T | None not Optional[T]
- Tools:
ruff (lint+format), mypy (types), pytest (tests)
- Prefer: generators over lists,
pathlib over os.path, f-strings over .format()
</language_specific>
<performance_patterns>
Common Optimizations
Algorithm, Before=O(n^2) nested loops, After=O(n) hash map lookup
Caching, Before=Recompute every call, After=Memoize/cache result
Lazy eval, Before=Build full list, After=Generator/iterator
Batching, Before=N individual calls, After=Single batch operation
Built-ins, Before=Custom implementation, After=Standard library function
Performance Workflow
- Set baseline benchmark
- Profile to find bottleneck (not guess)
- Apply targeted optimization
- Measure improvement against baseline
- Document trade-off if complexity increased
</performance_patterns>
Python: Add type safety
def get_user(id, include_posts=False):
user = db.find(id)
if include_posts:
user['posts'] = db.posts(id)
return user
def get_user(user_id: int, *, include_posts: bool = False) -> User | None:
user = db.find(user_id)
if user is None:
return None
user.posts = db.posts(user_id)
Success Criteria
Optimization is complete when:
- All linter/type checks pass with zero warnings
- Test suite passes with no regressions
- Performance improved (if that was the goal, with measurements)
- Code follows language-specific idioms from
.github/instructions/