返回提示库

代码改写

A high-signal refactoring prompt for rewriting existing code with constraints, tests, and clear change tracking.

审校时间: 2026-03-18

Use this when you already have working or partially working code and want the model to improve readability, performance, safety, or maintainability without changing behavior by accident.

Prompt 模板

You are a senior {language} engineer.

Goal:
Rewrite the code below for {goal} without changing the intended behavior unless explicitly requested.

Context:
- Runtime/framework: {runtime}
- Constraints: {constraints}
- Must preserve: {must_preserve}
- Style preferences: {style_preferences}

Code:
```{language}
{code}
```

Return exactly:
1. A short diagnosis of the main issues.
2. The rewritten code in one fenced block.
3. A concise change log.
4. Follow-up tests or risks to check.

在正式工作流中使用前,请先替换模板变量并补充你的约束条件。

适用场景

  • Refactoring duplicated logic in a component or service
  • Adding comments and naming cleanup before a handoff
  • Optimizing a hot path while preserving public behavior

期望输出

  • Issue diagnosis before the rewrite
  • Single final code block
  • Clear explanation of what changed
  • Risks or tests still worth checking

可替换变量

language

Primary language of the code

Example: TypeScript

goal

What kind of rewrite you need

Example: readability and error handling

runtime

Execution context or framework

Example: Next.js 16 server component

constraints

Hard constraints that should not be violated

Example: Do not add new dependencies

must_preserve

Behavior or API that must stay stable

Example: Existing function signature and output shape

style_preferences

Formatting or style conventions

Example: Prefer early returns and short helper functions

优化建议

  • Tell the model what must not change before you ask for improvements.
  • Name the runtime and framework so the rewrite matches your environment.
  • Ask for a test checklist if you plan to apply the patch directly.

示例用法

Refactor a React component with repeated fetch-state handling, but keep props and rendered behavior unchanged.

预期效果

The model consolidates repeated branches, keeps the same prop contract, and gives you a short regression checklist.

参考来源