返回提示词目录第 1160 页

用标准库替换手写工具代码

梳理项目中重复实现的工具逻辑,比较标准库与现有依赖的边界差异,并逐项测试替换。

完整提示词

<context>
Hand-rolled versions of solved problems are common and costly: date arithmetic that breaks at daylight saving changes and month ends, CSV parsing that splits on commas and fails on quoted fields, URL building by string concatenation without encoding, retry loops without backoff or jitter, deep copy by JSON round trip that drops dates and undefined, UUIDs from `Math.random`, padding, chunking, grouping and debounce helpers, and email or path handling that misses cases the platform API covers. The standard library or a dependency already in the lockfile usually has a tested version.

Replacement is a behaviour change at the edges, and sometimes the hand-rolled version is the correct one. The library may differ on: invalid input (throw versus return null), time zones and locale, rounding mode (banker's rounding versus half up), sort stability, Unicode handling, quoting and escaping, and performance on large inputs. Some home-grown code exists for a reason (a bug in the library version the project pins, a deliberate leniency clients rely on, a hot path). Adding a new dependency to replace twenty lines is often worse than keeping them: it brings supply-chain risk, size and upgrades. Prefer the standard library, then dependencies already present, and propose a new dependency only when the hand-rolled code is clearly buggy and large.
</context>

<task>
Find and replace hand-rolled code in {{scope}}.

1. Search the scope for reimplementations: helper names and patterns such as date math, parsing, formatting, encoding, retries, copying, id generation, collection helpers. Check the runtime version so you only propose APIs it has.
2. For each candidate list: location, what it does, the standard-library or existing-dependency replacement, and known edge-case differences (invalid input, time zones, rounding, Unicode, quoting, ordering). Check git history or comments for a reason it was written by hand. Mark candidates that need a new dependency separately.
3. Write tests that pin the current behaviour on edge cases before replacing, and run them against the replacement. Where behaviour differs, show the difference and ask whether the old behaviour was a bug or a requirement.
4. Replace one candidate at a time, deleting the helper when it has no remaining callers, and run the tests{{#test_command}} with `{{test_command}}`{{/test_command}}.
5. Show the diff and list candidates you left alone with the reason.
</task>

<constraints>
- No behaviour difference ships without the user's decision; where the hand-rolled code is buggy, say so and treat the fix as an intended change.
- Do not add a new dependency without approval; state its size, maintenance status as far as you can tell from the repo, and what it replaces.
- Only use APIs available in the project's runtime and language version.
{{> guardrails/scope-discipline}}
{{> guardrails/verify-before-done}}
</constraints>

<output_format>
## Candidates
Table: location, what it does, replacement, new dependency (yes or no).
## Edge-case differences
Per candidate: input, old result, new result, decision needed.
## Diff
The change.
## Check
Tests run and results, and candidates left alone with reasons.
</output_format>

可替换内容

  • {{scope}}
  • {{#test_command}}
  • {{test_command}}
  • {{/test_command}}
  • {{> guardrails/scope-discipline}}
  • {{> guardrails/verify-before-done}}

分类与标签

编程开发、代码重构、标准库、依赖治理

前往 AI 图片创作