Prompt Engineering: Code Review by Prompt
A code review prompt asks the model to check code against a checklist. The checklist says what good looks like.
A review against a short checklist
The prompt gives the function and three checks.
Review this Python function against this checklist. One, it returns "unknown" for an unknown order number. Two, it uses only the shop orders 1042, 1043 and 1044. Three, it returns a string.
def order_status(order_number):
orders = {1042: "shipped", 1043: "processing", 1044: "delivered"}
return orders.get(order_number, "unknown")
List each checklist item and say pass or fail.A review that finds a problem
The prompt gives a function with a missing case and the same checklist.
Review this Python function against this checklist. One, it returns "unknown" for an unknown order number. Two, it uses only the shop orders 1042, 1043 and 1044. Three, it returns a string.
def order_status(order_number):
orders = {1042: "shipped", 1043: "processing", 1044: "delivered"}
return orders[order_number]
List each checklist item and say pass or fail.A review of a second function
The prompt gives a refund check function and a checklist for it.
Review this Python function against this checklist. One, it returns true only for an unused item within 30 days. Two, it returns false for a used item. Three, it returns a true or false value.
def refund_ok(days_ago, used):
if used:
return False
return days_ago <= 30
List each checklist item and say pass or fail.Example
Review this Python function against this checklist. One, it returns "unknown" for an unknown order number. Two, it uses only the shop orders 1042, 1043 and 1044. Three, it returns a string. Four, it handles a string order number as well as a number order number.
def order_status(order_number):
orders = {1042: "shipped", 1043: "processing", 1044: "delivered"}
return orders.get(order_number, "unknown")
List each checklist item and say pass or fail.Key points
- A code review is a check of code against a list of rules.
- The prompt gives the model the code and the checklist. The model checks each item in turn.
- A checklist item must be checkable. A vague item gives a vague review.
- A review names each problem and where it sits in the code.
- A review that finds nothing is still useful. It says the code passed the checklist.
This example runs the review prompt against the local model and shows the review it gave.
Note
The model reviews the code it is shown. It cannot see code the prompt leaves out.
Try it Yourself
Edit the code, then press Run.
Running is simulated · Rust Playground
Exercise
What does a code review prompt give the model?