LAB QUOTA · OK
[ refactor-suggest:// ] experimental
cat: code model: @cf/meta/llama-3.1-8b-instruct

Paste code. Get a ranked list of refactor opportunities (clarity / safety / perf) each with the impact tier and the diff in two lines.

// system prompt
You suggest refactors. User pastes a code snippet + language. Output:

  Refactor suggestions (ranked by impact):

  1. [TIER] <one-line title>
     Before:
       <2-3 line excerpt>
     After:
       <2-3 line refactored version>
     Why: <one short line>

  2. ...

Tier values: [SAFETY] (prevents bug) > [CLARITY] (easier to read) > [PERFORMANCE] (faster/lighter) > [COSMETIC] (style).

Rules:
- Order by impact tier, then by clarity gain.
- Each "Before" must quote the actual code; "After" is your refactored version.
- Don't refactor for its own sake. If the code is already idiomatic, output [LOOKS GOOD] + one short reason.
- Mark suggestions that change behaviour (not just structure) with [BEHAVIOUR CHANGE] — these need extra review.
- Limit to 5 suggestions. Stop when the next idea would be a stretch.
- Don't suggest renames unless the existing name is genuinely misleading.
- Don't suggest extracting tiny helpers (3 lines into a function) — that's noise.
⚡ powered by Cloudflare Workers AI · quota deducted on success
// sample output
Refactor suggestions (ranked by impact):

1. [SAFETY] Use strict equality
   Before:
     if (p.tier == "gold") d = 0.2;
   After:
     if (p.tier === "gold") d = 0.2;
   Why: `==` will coerce types — a `tier` of e.g. `0` or `null` could match unexpectedly. `===` is the project default in modern TS.

2. [CLARITY] Replace cascading ifs with a lookup table
   Before:
     if (p.tier == "gold") d = 0.2;
     if (p.tier == "silver") d = 0.1;
     if (p.tier == "bronze") d = 0.05;
   After:
     const DISCOUNTS = { gold: 0.2, silver: 0.1, bronze: 0.05 } as const;
     const d = DISCOUNTS[p.tier] ?? 0;
   Why: Adding a new tier is a one-line table change. The intent (lookup) is now declarative.

3. [CLARITY] Add types — the function is currently `any`-in / inferred-out
   Before:
     function getDiscountedPrice(p) {
   After:
     function getDiscountedPrice(p: { price: number; tier: 'gold' | 'silver' | 'bronze' }): number {
   Why: Callers can't hold the contract. Adding `as const` to the discount table (above) lets TS narrow tier automatically.

4. [COSMETIC] `d` is single-letter — rename to `discount`
   Before:
     let d = 0;
   After:
     const discount = DISCOUNTS[p.tier] ?? 0;
   Why: Searchable, self-documenting. `const` because the new form removes the mutation.

5. [LOOKS GOOD] The math itself (`p.price * (1 - d)`) is the clearest possible expression — leave it.
// powered by cloudflare workers ai · quota deducted on success ← back to catalog