Loading...
Loading...
Detect common code smells and anti-patterns providing feedback on quality issues a senior developer would catch during review. Use when user opens/views code files, asks for code review or quality assessment, mentions code quality/refactoring/improvements, when files contain code smell patterns, or during code review discussions.
npx skill4agent add kjgarza/marketplace-claude detect-code-smells{
"file": "path/to/file.js",
"language": "javascript",
"overallScore": 7.5,
"smells": [
{
"type": "Long Method",
"severity": "warning",
"location": {
"line": 42,
"endLine": 95,
"function": "processUserData"
},
"description": "Function 'processUserData' is 53 lines long",
"suggestion": "Extract smaller functions for validation, transformation, and persistence",
"impact": "Harder to understand, test, and maintain",
"effort": "medium"
},
{
"type": "Complex Conditional",
"severity": "warning",
"location": {
"line": 67,
"column": 8
},
"description": "Nested conditional with 5 levels of nesting",
"suggestion": "Extract conditions into well-named boolean variables or separate functions",
"impact": "Difficult to understand logic flow",
"effort": "small"
}
],
"metrics": {
"linesOfCode": 234,
"averageComplexity": 4.2,
"maxComplexity": 12,
"duplicationPercentage": 8.5
},
"recommendations": [
"Extract 'processUserData' into smaller single-purpose functions",
"Replace complex conditional at line 67 with early returns",
"Consider extracting repeated validation logic into a helper function"
]
}user-service.jsCode Smell Detected: Long Method
Function 'handleUserRegistration' (lines 23-172) is 150 lines long.
Impact: This function is doing too many things, making it:
- Hard to understand at a glance
- Difficult to test thoroughly
- Prone to bugs when modified
- Challenging to reuse parts of its logic
Suggestion: Extract into smaller functions:
- validateRegistrationData() - lines 23-45
- checkEmailAvailability() - lines 46-68
- createUserAccount() - lines 69-98
- sendWelcomeEmail() - lines 99-130
- logRegistrationEvent() - lines 131-172
This follows the Single Responsibility Principle and makes each piece independently testable.Code Smell Detected: Complex Conditional
Lines 45-72 contain a deeply nested conditional (5 levels deep) with multiple boolean conditions.
Current pattern:
if (user) {
if (user.isActive) {
if (user.hasPermission('admin')) {
if (validateToken(user.token)) {
// ... more nesting
}
}
}
}
Refactoring suggestion using guard clauses:
if (!user) return null;
if (!user.isActive) return null;
if (!user.hasPermission('admin')) return null;
if (!validateToken(user.token)) return null;
// Now do the work without nesting
Benefits: Reduces cognitive load, clearer failure conditions, easier to test edge cases.Code Smell Detected: Duplicate Code
Found 3 nearly identical code blocks:
- Lines 34-42 (in fetchUserById)
- Lines 78-86 (in fetchUserByEmail)
- Lines 112-120 (in fetchUserByUsername)
All three blocks implement the same error handling and response formatting logic.
Suggestion: Extract common logic into a helper function:
function handleUserFetchResponse(result, errorMessage) {
if (!result) {
throw new Error(errorMessage);
}
return formatUserResponse(result);
}
Then simplify each call site:
const user = await db.query(...);
return handleUserFetchResponse(user, 'User not found');
Impact: Reduces maintenance burden, ensures consistent behavior, fewer bugs.suggest-performance-fixsecurity-pattern-check