Unit 4 / 12

Code Review, Refactoring, and Technical Debt

Gains:

  • Ability to use AI as a second eye in code review for readability, logic and security
  • Ability to plan refactoring steps with AI support without disrupting complex code behavior
  • Ability to verify AI's review and edit recommendations with testing and version control comparison

In software engineering, code is read much more than it is written. A line of code is written once, but is read, modified and built upon dozens of times over the course of months. That's why code review (reviewing someone else's or your own code for logic, readability and security) and refactoring (improving the structure of the code without changing its behavior) are at the heart of engineering. AI becomes a powerful “second eye” for these two tasks: it quickly suggests readability, points out overlooked logic and security issues, and breaks a large refactoring into smaller safe steps. But there is a critical rule: refactoring should not change behavior, and the only thing that guarantees this is testing.

In this unit, we'll see how to use AI in a structured way for code review, how to fix complex code without breaking its behavior, and how to manage technical debt (quick but costly code decisions).

Concepts: Technical debt: Code decisions made today for speed that make maintenance difficult in the future. Code smell: Patterns that are not errors themselves but indicate problems (too long functions, repetitive code). Regression: When a change breaks something that was previously working.

Using AI in Structured Code Review

When time is limited, it is necessary to focus on the highest risk issues. The automatic formatter handles formatting issues such as indentation and spacing; You must devote human attention to logic, security, and edge case behavior. When having the AI ​​review, ask for a prioritized list, not a plain barrage of reviews.

  1. Give the scope. What code, what to do, in what context it works.
  2. Specify the priority axis. Accuracy and security first, readability second.
  3. Ask for concrete correction. “Why the problem” and “recommended fix” for each finding.
  4. You verify the findings. AI also produces false positives; Verify each finding against code and testing.

Structured review prompt: "Examine the following function like a senior engineer. List the findings in order of importance and mark them with these tags: [CRITICAL] logic/security, [MEDIUM] edge case/performance, [LOW] readability/name. For each finding: why ask, concrete fix suggestion. DO NOT SKIP formatting/indentation issues, the automated tool will handle it. Code: [code]"

Security-focused review prompt: "Review this code for security purposes only: lack of input validation, risk of injection, lack of authorization control, leak of confidential information, insecure defaults. Add an example attack scenario to each finding. If there is no security issue, clearly state 'I did not find any critical security issues'. Code: [code]"

Caution: Just because the AI says "no problem" is not proof that there is no problem. AI can produce false negatives; can bypass a real security issue. AI review supplements, not replaces, human review and security testing. In security-critical code, the competent engineer has the final say.

Test-Preserved Refactoring

The golden rule of refactoring: test first, change later. Before fixing the code, there should be tests that lock the current behavior so you know immediately if the change breaks something. Don't break the order when having the AI ​​refactoring.

  1. Put current behavior to the test. Otherwise, have the AI ​​produce a “characterization test” (test that captures current behavior as it is).
  2. Fix it in small steps. Testing must remain green at every step.
  3. Run it after each step. Catch regression early.

Safe refactoring plan prompt: "The following 60-line function does too much and is hard to read. I want to refactor it WITHOUT changing its behavior. First: list what test cases I need to lock down the current behavior. Then: break the refactoring into small steps, each of which can be executed while the tests are green. Don't write the code yet, give the plan first. Code: [code]"

Weak Prompt / Strong Prompt

WEAK: "Make this code better." (Result: unclear what to improve; AI makes arbitrary changes, can change behavior silently.) STRONG: "Refactor this payment calculation function for readability. CONSTRAINT: behavior must remain exactly the same, return values must not change. Split the long function into meaningful utility functions, increasing the magic numbers to named constants. List changes item by item and explain WHY each item does not change behavior. Code: [code]"

The powerful prompt clearly states the "behavior must remain exactly the same" constraint and what needs to be improved. Without this constraint, AI can change logic in the name of “improvement” and produce a silent regression.

Managing Technical Debt

Approach

In the short term

in the long run

ignoring debt

fast progress

Maintenance paralysis, team slowing down

rewrite everything

Standing feature development

Uncertain return, high risk

Measured, test-protected refactoring

minor slowdown

Sustainable speed

The healthiest way is the third: make the debt visible (track it in a list), start where it hurts the most, and test-proof each fix. AI is a good help in identifying and prioritizing debt items, but which debt to pay is a business decision.

Mini Cases

Case 1 — Silent regression. A developer tells the AI ​​to “simplify this function”; The AI ​​translates a condition incorrectly and the return calculation is broken. Since there is no testing, the error occurs after 3 weeks with a customer complaint. The team does the same job by first writing a characterization test and catches the error with a red test on the first run.

Case 2 — Useful second eye. In a code review, AI realizes that user authorization is only checked in the interface and not on the server. This is a unauthorized access vulnerability. Engineer adds server-side authorization checking; AI inspection prevents an actual security incident.

Case 3 — False positive. AI says "this variable is never used, delete it"; However, it is used indirectly through a variable reflection mechanism. If the engineer did not verify the suggestion against the test, it would be deleted and a runtime error would occur. Every AI finding must be confirmed before implementation.

Common mistakes

  • Refactoring without testing. There is nothing left to ensure that the behavior is preserved.
  • Applying AI findings without validating them. False positives and false negatives both happen.
  • Wasting human time on format problems. Focusing on tasks that can be solved by automated tools overshadows the real risks.
  • Taking the answer "No problem" as a guarantee. AI can bypass vulnerability; human review is required.
  • Trying to pay off the entire debt at once. Major rewrites are risky; Steps that are measured and protected by testing are preferred.

In summary

Code review and refactoring determines the longevity of the code. AI is a powerful second eye and plan generator: providing prioritized findings, security scenarios, and small-step refactoring plans. But refactoring shouldn't change behavior, and only testing guarantees this. Validate every AI finding against code and testing; Don't take the "no problem" answer as evidence. Make technical debt visible and pay it off in measured, test-protected steps.

Application task

Take a 40-70 line, somewhat complex function you have (or have the AI generate). First follow the structured review prompt and sort the findings as [CRITICAL]/[MEDIUM]/[LOW]; Manually verify at least one finding against the code. Then, with the safe refactoring plan prompt, first generate and run the characterization tests, then apply the refactoring in small steps and verify that the tests remain green at each step.

checklist

  • [ ] I structured the review with priority tags (critical/medium/low).
  • [ ] I have verified at least one AI finding against the code/test.
  • [ ] I tested the current behavior before refactoring.
  • [ ] I made the changes in small steps and ran tests at each step.
  • [ ] I specified the "Behavior must remain the same" constraint in the prompt.
  • [ ] I have confirmed that security findings require human confirmation.