教程:编程辅助
目标:解释并重构一段复杂的遗留代码。 难度:高级 时长:3 分钟
场景
您在 Visual Studio 中看到一个令人困惑的 C# 函数,不明白它的作用。
步骤 1:选中代码
在您的 IDE 中选中该函数:
public void P(int a) {
if (a > 10) Console.WriteLine("X");
else Console.WriteLine("Y");
// ... more spaghetti code ...
}
步骤 2:触发 Rephlo
按下 Ctrl+Shift+Alt+C。
步骤 3:运行 "Explain Code"
使用以下提示词执行一个 Command:"Explain this code step-by-step and suggest improvements."
步骤 4:查看解释结果
Rephlo 提供如下分析:
- 解释:"The function
Ptakes an integer... It prints X if..." - 问题:"Variable names are non-descriptive. Logic is nested too deeply."
步骤 5:重构
现在对同一选中内容运行第二个 Command:"Refactor Code"。
提示词:"Rename variables to be descriptive and simplify logic."
输出结果:
public void ProcessThreshold(int thresholdValue)
{
string message = thresholdValue > 10 ? "X" : "Y";
Console.WriteLine(message);
}
步骤 6:复制
点击 Copy,将整洁后的代码粘贴回 Visual Studio。