Chuyển tới nội dung chính

Hướng dẫn: Hỗ trợ lập trình

Mục tiêu: Giải thích và tái cấu trúc (refactor) một đoạn mã cũ (legacy) phức tạp. Cấp độ: Nâng cao Thời gian: 3 phút

Tình huống

Bạn đang nhìn vào một hàm C# gây khó hiểu trong Visual Studio và không hiểu nó làm gì.

Bước 1: Chọn mã

Bôi đen (highlight) hàm đó trong IDE của bạn:

public void P(int a) {
if (a > 10) Console.WriteLine("X");
else Console.WriteLine("Y");
// ... more spaghetti code ...
}

Bước 2: Kích hoạt Rephlo

Nhấn Ctrl+Shift+Alt+C.

Bước 3: Chạy "Explain Code"

Thực thi một lệnh với prompt: "Explain this code step-by-step and suggest improvements."

Bước 4: Xem giải thích

Rephlo đưa ra một bản phân tích:

  1. Explanation: "The function P takes an integer... It prints X if..."
  2. Issues: "Variable names are non-descriptive. Logic is nested too deeply."

Bước 5: Refactor

Giờ hãy chạy một lệnh thứ hai trên cùng vùng chọn: "Refactor Code".

Prompt: "Rename variables to be descriptive and simplify logic."

Kết quả:

public void ProcessThreshold(int thresholdValue)
{
string message = thresholdValue > 10 ? "X" : "Y";
Console.WriteLine(message);
}

Bước 6: Copy

Nhấp Copy và dán đoạn mã sạch trở lại vào Visual Studio.