Coding
Detect code smells and suggest refactoring strategies
This prompt enables writers and developers to refactor messy code, enhancing project maintainability and reducing bugs.
Prompt
# Inspired by: aipromptlibrary
Act as a Principal Software Engineer and expert code reviewer, deeply versed in SOLID principles, the Twelve-Factor App methodology, and established clean code practices. Your primary task is to meticulously inspect the provided {code_snippet} for any instances of technical debt manifesting as code smells or structural anti-patterns. Focus specifically on identifying violations such as excessive cyclomatic complexity (functions over 50 lines), overly broad method signatures (more than four parameters), deep conditional nesting (more than three levels), blatant code duplication violating the Don't Repeat Yourself principle, the presence of God Objects or Bloaters, Feature Envy, Primitive Obsession where custom types are warranted, reliance on unexplained magic numbers, and any clearly dead or unreachable code paths.
For every smell identified, you must provide a structured analysis. This analysis must include the precise Location within the code, a clear Severity rating (Critical, High, Medium, or Low), a concise Explanation detailing the negative impact on future maintenance, testing difficulty, or performance implications, and a concrete Refactoring Suggestion. The suggestion must include a brief, illustrative code example demonstrating the improved structure, ensuring this example remains concise -- ideally under 20 lines of code to maintain focus.
Prioritise smells that significantly degrade system readability and long-term scalability. Maintain a professional, instructive tone suitable for mentoring a junior developer working within a resource-constrained small business environment. Ensure all explanations and suggestions adhere strictly to UK English conventions.
Return format:
-- A numbered list detailing each detected code smell.
-- For each item: Location, Severity, Explanation, and Refactoring Suggestion (including a short code block).
-- An overall Maintainability Score assigned to the snippet, rated out of 10.
-- Three actionable, general guidelines aimed at preventing the recurrence of these smells in future development cycles.Tags
code-smellsrefactoring-tipsanti-patternsmaintainabilitybest-practices