refactor-replace-conditional-with-polymorphism

refactor-replace-conditional-with-polymorphism is a skill for Claude Code, Codex from helmedeiros/clean-code-skills. It costs 61 tokens per session (820 once invoked), scanned A, original, MIT.

Instructions for replacing long if-else or switch chains with separate classes that each handle one type or category. This design is called polymorphism: the object's type selects the behavior.

In plain words
What is it for?
Refactoring repeated type-based conditionals, status checks, category branches, and switch statements into separate implementations.
Why use it?
It makes branching business rules easier to extend and avoids changing one large conditional whenever a new type is added.

Skill for Claude CodeCodex

Part of the clean-code-skills plugin — 21 skills, 2 commands shipped together

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add skills/helmedeiros/clean-code-skills/refactor-replace-conditional-with-polymorphism
Any agent
npx skills add helmedeiros/clean-code-skills --skill refactor-replace-conditional-with-polymorphism
Clone the repo
git clone --depth 1 https://github.com/helmedeiros/clean-code-skills

Made for: Claude Code, Codex.

Or install clean-code-skills, the plugin that ships this one along with the rest of its 21 skills, 2 commands.

Wrote this? Show the measurements

A badge with what this costs and how it scanned, read live from this page, so it follows the numbers instead of freezing them. Markdown for a README, HTML for a documentation site or a project page.

agentmods badge for refactor-replace-conditional-with-polymorphism

README.md
[![agentmods](https://agentmods.dev/badge/skills/helmedeiros/clean-code-skills/refactor-replace-conditional-with-polymorphism.svg)](https://agentmods.dev/skills/helmedeiros/clean-code-skills/refactor-replace-conditional-with-polymorphism)
Your own site
<a href="https://agentmods.dev/skills/helmedeiros/clean-code-skills/refactor-replace-conditional-with-polymorphism"><img src="https://agentmods.dev/badge/skills/helmedeiros/clean-code-skills/refactor-replace-conditional-with-polymorphism.svg" alt="Measured on agentmods" height="20"></a>
Per session 61 Skills are progressive disclosure: only the name and description are preloaded; the body loads when the skill is used.
When invoked 820 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5 $0.00061 $0.00820
Opus 5 $0.00030 $0.00410
Sonnet 5 $0.00012 $0.00164
Haiku 4.5 $0.00006 $0.00082

Measured 4d ago against content hash db8a874d8d4e, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

refactor-replace-conditional-with-polymorphism scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 4d ago.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

skills/refactor-replace-conditional-with-polymorphism/SKILL.md · 92 lines

How it starts

The opening of the file, as written. The whole thing — 92 lines — stays where its author put it; the contents beside it link to each section on GitHub.

Refactoring: Replace Conditional with Polymorphism

Replace complex conditional logic that branches on type or category with polymorphic dispatch, distributing each branch into its own class with a shared interface.

When This Skill Applies

  • A switch or if-else chain branches on an object's type, status, or category
  • The same conditional structure is duplicated across multiple methods
  • Adding a new type requires modifying existing conditionals
  • The user wants to apply the Open-Closed Principle to branching logic
  • Business rules vary by type and are tangled together

Core Principle

When a conditional selects behavior based on type, replace the conditional with polymorphism. Each type gets its own class that implements a shared interface. The conditional disappears — the correct behavior is selected by the object's type at runtime. This makes the system open for extension (add a new type) and closed for modification (existing types are untouched).

Workflow

Step 1: Identify the Conditional

Find switch statements or if-else chains that branch on type, status, category, or kind. Look for the same branching pattern repeated in multiple methods — this is a strong signal.

Step 2: Define the Interface

Extract the common method signature from the conditional branches. This becomes the shared interface or abstract method that all variants will implement.

Step 3: Create Subclasses or Implementations

For each branch, create a class that implements the interface. Move the branch's logic into the corresponding class. Each class encapsulates the behavior for one variant.

Step 4: Replace the Conditional with Dispatch

Replace the original conditional with a call to the interface method on the appropriate object. Use a factory or registry to select the correct implementation at construction time.

Step 5: Verify

Run the test suite. Add a test for each variant to confirm the polymorphic behavior matches the original conditional logic.

Detection / Indicators

  • Switch statements with more than 3 cases
  • If-else chains with instanceof, type checks, or string comparisons
  • The same switch structure appears in multiple methods
  • Adding a new type requires editing multiple files
  • Comments like "if it's a Premium user, do X; if Basic, do Y"

Read the full file on GitHub · 92 lines

Files

What ships with it

1 file beside SKILL.md in the same directory: the scripts, references and assets a skill reads on demand. Not counted in the per-session cost; read them before you install if any of them is executable.

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. 4d ago First seen · 92 lines · 61 tokens per session scan A db8a874d8d4e

Subscribe to this mod's changes

refactor-replace-conditional-with-polymorphism is a skill published in the GitHub repository helmedeiros/clean-code-skills (2 stars, last pushed 6mo ago), licensed MIT. It adds 61 tokens to every session and 820 once invoked, about $0.0003 per session on Opus 5. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-31.

Related

Other skills, from other repositories

fp-brief

First-principles briefing from technical documents. Use when: understanding why decisions were made, onboarding to feature reasoning, reviewing decision chains, explaining doc from first principles. Not for: PM/CTO summary (use project-brief), pre-doc analysis (use feasibility-study), code explanation (use…

sd0xdev/sd0x-harness · 77 tokens

codex-explain

Explain complex code via Codex MCP. Use when: understanding complex logic, tracing data flow, onboarding to unfamiliar code. Not for: code review (use codex-code-review), exploration (use code-explore). Output: structured explanation at chosen depth.

sd0xdev/sd0x-harness · 56 tokens

sharingan

Replicate knowledge from any source as sd0x-dev-flow skill definition. Use when: copying skills from repos, adapting patterns from articles/papers/code, converting knowledge to skill format. Not for: research without skill output (use deep-research), creating skills from scratch (use skill-creator), project onboarding…

sd0xdev/sd0x-harness · 87 tokens

creating-explainers

Use when creating an interactive explainer - a single self-contained HTML page with hand-built Canvas figures. Handles source-file explainers, topic-driven research explainers, and mixed intake where files provide the spine and research adds support. Trigger phrases include "make an explainer", "turn this paper into…

analyticalmonk/explain-this · 98 tokens

explaining-codebases

Use when creating an interactive explainer about a codebase, repository, or source files. Handles onboarding overviews, architecture maps, and deep-dives on real implementation paths. Trigger phrases include "explain this codebase", "interactive guide to this repo", "walk through how X works in the code", or…

analyticalmonk/explain-this · 84 tokens

openehr-assistant

This skill should be used when a conversation touches openEHR outside a task owned by a dedicated skill — e.g. "what is an archetype?", "how do openEHR templates work?", "which composition category fits this data?", "find me a guide on X", "where do I start with openEHR modeling?" — or names openEHR concepts (ADL…

Cadasto/openehr-assistant-plugin · 182 tokens