- Home
- Skills
- Code Quality & Review
- Craftsman: Clean Code & Refactoring
Works with the AI Agents you already use
Craftsman: Clean Code & Refactoring
Autonomous code refactoring specialist that flattens nested branching, eliminates hidden O(n²) bottlenecks, and reduces cognitive bloat with guaranteed 100% behavioral invariance.
$9
Craftsman: Clean Code & Refactoring
Example session with this skill installed
Act as Craftsman. Audit src/services/subscription.ts and refactor the most convoluted function using guard clauses while preserving exact behavior.
- Read your context and instructions
- Compiled the craftsman
Summary
Refactored processUserSubscription in src/services/subscription.ts. Flattened a 5-level nested if ladder into declarative guard clauses, validated preconditions up front, and surfaced the happy path at the root level.
Complexity Impact
| Metric | Before | After | Delta |
|---|---|---|---|
| Max Nesting Depth | 5 levels | 2 levels | -3 levels |
| Branching Points | 11 points | 6 points | -5 points |
| Function Lines | 42 lines | 31 lines | -11 lines |
| Test Suite | 24 passing | 24 passing | 100% Invariant |
// src/services/subscription.ts
export function processUserSubscription(
user: User,
plan: PlanTier,
paymentMethod: PaymentMethod | null,
options?: SubscriptionOptions
): SubscriptionResult {
if (!user) {
throw new ValidationError('USER_REQUIRED');
}
if (!user.isActive || user.isSuspended) {
throw new UserAccountError('USER_INACTIVE_OR_SUSPENDED');
}
if (plan.status !== 'active' || plan.price < 0) {
throw new PlanError('INVALID_PLAN_TIER');
}
if (!paymentMethod || !paymentMethod.isValid) {
throw new PaymentError('INVALID_PAYMENT_METHOD');
}
if (user.hasActivePlan && !options?.forceUpgrade) {
throw new SubscriptionError('USER_ALREADY_SUBSCRIBED');
}
const discountRate = options?.discount || 0;
const chargeAmount = plan.price * (1 - discountRate);
const transaction = executePayment(user, chargeAmount, paymentMethod);
if (!transaction.success) {
throw new PaymentError('PAYMENT_FAILED', transaction.reason);
}
const sub = recordSubscription(user.id, plan.id, transaction.id);
sendConfirmationEmail(user.email, sub.id);
return { success: true, subscriptionId: sub.id };
}
🛡️ Behavioral Invariance Verification
- Verified identical return values across valid and error boundary conditions
- Preserved error types (
ValidationError,UserAccountError,PlanError,PaymentError,SubscriptionError) - Preserved temporal ordering of
executePayment->recordSubscription->sendConfirmationEmail - Ran native test suite: 24/24 tests passing green
🎯 Next Steps
- Commit the change with Conventional Commits:
refactor(subscription): flatten nested validation branches using guard clauses.
Connects securely to your tools. The creator never sees your data.
About this skill
Stop letting nested if/else pyramids, hidden quadratic loops, and cognitive complexity slow your engineering team down.
Craftsman is an autonomous clean-code refactoring skill built on first principles and the Zero-Anchor philosophy. Unlike superficial linters or style formatters that just change whitespace, Craftsman restructures code architecture:
- 🛡️ Guaranteed Behavioral Invariance: Zero breaking changes. Preserves exact return values, error types, and temporal ordering of side effects.
- 📐 Early Exits & Guard Clauses: Flattens deeply indented conditional pyramids into readable, linear execution pipelines.
- ⚡ Algorithmic Hotspot Detection: Detects nested loops (O(n²)+) and hidden linear lookups inside loops (
.find(),.includes(),x in list), refactoring them into O(1) set/map operations. - 🧹 Tames Declarative Bloat: Decomposes runaway method-chaining pipelines (
.map().filter().reduce().sort()) into readable, strongly typed stages. - 🔬 Bundled Static Analyzer: Includes
analyze_complexity.py, a zero-dependency Python tool that calculates nesting depth, branching points, and cognitive friction before and after refactoring. - 🌐 Universal & Language-Agnostic: Works seamlessly across TypeScript, JavaScript, Python, Go, Rust, Dart, and Java.
What's Inside the Skill:
SKILL.md— Actionable instructions with trigger-based detection.scripts/analyze_complexity.py— Standalone AST & indentation complexity analyzer.examples/— Concrete before/after walkthroughs and standardized deliverable templates.
Find more skills like this on the Claude Skills Marketplace, the Codex Skills Marketplace and the Gemini Skills Marketplace, or browse Claude Skills for Code Quality & Review.
Requires Python 3 for the static analyzer. Relies on existing project test suites and typecheckers for final verification.
How to use
- 1
Connect your agent to the Agensi MCP
Copy the prompt and paste it into Claude, ChatGPT, Cursor or any agent that supports MCP. It sets up the connection for you. Manual setup
- 2
Sign in to Agensi
Your agent shows a sign-in link. Sign in and click Allow.
- 3
Ask for what you need
Your agent loads this skill and uses it on your task. It always asks before buying a skill.
Reviews
No reviews yet
Be one of the first to try it. Every listed skill goes through the trust checks below.
Security scanned
Checked by our 8-point scan before listing
Fresh listing
Recently published to Agensi
Money-back guarantee
Doesn't do what its page promises? You get your money back.
Trust & safety
Security scan
Security scan passed 7 days ago
- Software
Python
Git