Designing and Optimizing Prompt Templates for AI Code Review (Lesson 16)

This article explains how to build a TypeScript PromptBuilder that supports variable placeholders, demonstrates three concrete AI code‑review prompt templates, and shows how these templates can be integrated into a code‑review workflow to produce structured, high‑quality feedback.

Ubiquitous Tech
Ubiquitous Tech
Ubiquitous Tech
Designing and Optimizing Prompt Templates for AI Code Review (Lesson 16)

In enterprise AI code‑review scenarios, simple prompts are insufficient because code changes, business impact, and review requirements vary widely. Effective prompts should include dynamic variables, explicit review goals, checklist compliance, change detection, and avoid returning irrelevant code information.

The article introduces a PromptBuilder class written in TypeScript. Its design focuses on two capabilities:

Template replacement : parses strings containing {{variable}} placeholders and allows developers to define these variables at runtime.

Prompt construction : combines a user‑provided template with a variable map to generate the final prompt.

Example implementation (simplified):

import { PromptBuilder } from './PromptBuilder';
const promptBuilder = new PromptBuilder("You are a highly skilled Java architect, please review the code change. Requirements: {{ruleList}}");
const systemPrompt = promptBuilder.build({
    ruleList: "1. Focus on business logic correctness, null‑pointer risks, etc."
});
console.log(systemPrompt);

The generated prompt can be passed to an LLM for code review. The article also shows how to place the PromptBuilder in an openai.ts module and import it where needed.

Three concrete prompt templates are provided:

Prompt 2

export const prompt2 = `
# Role
You are a senior **{{language}}** programming expert, responsible for reviewing code for performance, readability, security, logic errors, exception handling, and boundary conditions.
# Skills
- **Skill1**: Provide constructive and humorous feedback.
- **Skill2**: Review code from **GitLab** including full business context and **Git Diff** snippets. '+' lines are additions, '-' lines are deletions.
# Task
Analyze the **Git Diff** for performance issues, null‑pointer risks, magic numbers, logic defects, security risks, naming conventions, missing comments, exception handling, and boundary conditions.
# Output Requirements
1. Use **Markdown** format.
2. Bold section titles (e.g., **Code Score**, **Issues**).
3. Provide deep analysis and detailed modification suggestions.
4. Return structured variables: score, issues, suggestions, modified code snippets, highlights, purpose, and technical knowledge.
`;

Prompt 3

export const prompt3 = `
# Role
You are a senior **{{language}}** expert, proficient in code quality inspection.
# Skills
- **Skill1**: Offer constructive, humorous feedback.
- **Skill2**: Review naming, logging, parameter validation, exception handling, transaction control, etc.
- **Skill3**: Analyze concurrency, idempotency, security, unit testing, remote calls.
# Task
Check the following 18 items, e.g., comment standards, log level appropriateness, naming conventions, parameter validation, null‑check handling, exception handling, modularity, concurrency control, unit test coverage, code style, API compatibility, logic clarity, security risks, transaction control, idempotency, middleware usage, code smells, remote‑call considerations.
# Output
Return a Markdown table with columns **Rule Name**, **Review Description**, **Pass/Fail**.
`;

Result screenshots (included as images) demonstrate that the LLM follows the defined structure, producing markdown‑formatted feedback and tables that align with the checklist.

Finally, the article emphasizes that prompt engineering is a crucial skill in the AI era, summarises best‑practice guidelines (clear instructions, provide reference text, split tasks, give the model time to think, use external tools, test changes systematically) and lists several knowledge‑base resources for further study.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

TypeScriptLLMsoftware quality
Ubiquitous Tech
Written by

Ubiquitous Tech

A ubiquitous public account for pirate enthusiasts, regularly sharing curated experiences, tech learning, and growth insights. Currently publishing articles on AI RAG customer service, AI MCP technology, and open-source design. Personal free Knowledge Planet: Awakening New World Programmer.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.