Skip to main content

Tutorial: Coding Assistance

Goal: Explain and refactor a complex piece of legacy code. Level: Advanced Time: 3 minutes

┌─────────────────────────────────────────────────────────────────────────┐
│ CODE ASSISTANCE WORKFLOW │
├─────────────────────────────────────────────────────────────────────────┤
│ │
│ ┌──────────────────────────────────────────────────────────────────┐ │
│ │ │ │
│ │ STEP 1: SELECT CODE STEP 2: EXPLAIN │ │
│ │ ┌─────────────────────┐ ┌─────────────────────────────┐ │ │
│ │ │ public void P(int a)│ │ EXPLANATION: │ │ │
│ │ │ { │ │ │ │ │
│ │ │ if (a > 10) │ ──▶ │ "The function P takes an │ │ │
│ │ │ Console... │ │ integer and prints X if │ │ │
│ │ │ } │ │ value exceeds 10..." │ │ │
│ │ │ [SELECTED] │ │ │ │ │
│ │ └─────────────────────┘ │ ISSUES FOUND: │ │ │
│ │ │ • Non-descriptive names │ │ │
│ │ │ • Deeply nested logic │ │ │
│ │ └─────────────────────────────┘ │ │
│ │ │ │ │
│ │ ▼ │ │
│ │ STEP 3: REFACTOR STEP 4: COPY TO IDE │ │
│ │ ┌─────────────────────┐ ┌─────────────────────────────┐ │ │
│ │ │ Command: │ │ public void ProcessThresh.. │ │ │
│ │ │ "Refactor Code" │ │ { │ │ │
│ │ │ │ ──▶ │ string msg = threshold.. │ │ │
│ │ │ "Rename variables, │ │ Console.WriteLine(msg); │ │ │
│ │ │ simplify logic" │ │ } │ │ │
│ │ │ │ │ │ │ │
│ │ │ [Run] │ │ [Copy] [Insert] │ │ │
│ │ └─────────────────────┘ └─────────────────────────────┘ │ │
│ │ │ │
│ └──────────────────────────────────────────────────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────────────────┘

Scenario

You are looking at a confusing C# function in Visual Studio and don't understand what it does.

Step 1: Select Code

Highlight the function in your IDE:

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

Step 2: Trigger Rephlo

Press Ctrl+Shift+Alt+C.

Step 3: Run "Explain Code"

Execute a command with the prompt: "Explain this code step-by-step and suggest improvements."

Step 4: Review Explanation

Rephlo provides a breakdown:

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

Step 5: Refactor

Now run a second command on the same selection: "Refactor Code".

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

Output:

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

Step 6: Copy

Click Copy and paste the clean code back into Visual Studio.