Give the bug an address
A useful coding request has four pieces: the relevant code, an input that fails, the result you expected and the result you got. “My app is broken” makes the assistant guess. A reproducible example tells it where to look.
Consider this JavaScript function. The intended result is the arithmetic mean of a non-empty array of finite numbers, with null for an empty array:
function average(values) {
return values.reduce((total, value) => total + value) / values.length;
}
console.log(average([2, 4, 6])); // 4
console.log(average([])); // throws instead of returning nullThe first call works, which makes the bug easy to miss. The empty-array case fails because reduce has no initial value and no first element to use as one.
Ask for an explanation before a rewrite
This function should return the arithmetic mean.
Inputs are arrays containing finite numbers.
For an empty array it must return null.
The [2, 4, 6] case works, but [] throws.
Explain the cause and propose the smallest fix.
Include tests for an empty array, one value and negative values.The input contract matters. If the array could contain strings or NaN, the task would also need a validation policy. Here, the values are already finite numbers, so the repair can stay focused.
Ask the assistant to state its assumption rather than quietly widening the task. A small function does not need a framework, a dependency or an unrelated refactor to solve this bug.
Make the smallest change that satisfies the contract
function average(values) {
if (values.length === 0) return null;
const total = values.reduce((sum, value) => sum + value, 0);
return total / values.length;
}The guard gives the empty case an explicit result. The initial zero makes summation work even when there is no first value. The function still returns the arithmetic mean for the inputs in its contract.
There is no claim here that the function accepts arbitrary input. Extremely large numbers can exceed JavaScript’s numeric range, and non-number values require separate handling. Define those requirements before extending the implementation.
Run checks that can prove the fix wrong
Save the function and these assertions in a file named average.mjs, then run it with Node.js. This example uses the built-in assertion module.
import assert from 'node:assert/strict';
function average(values) {
if (values.length === 0) return null;
return values.reduce((sum, value) => sum + value, 0) / values.length;
}
assert.equal(average([]), null);
assert.equal(average([7]), 7);
assert.equal(average([2, 4, 6]), 4);
assert.equal(average([-4, 0, 4]), 0);
console.log('All checks passed.');node average.mjsThese checks cover the reported failure and several different input shapes. They do not establish every possible property of the function, but they are more useful than checking only the example that already worked.
Carry the same method into a real project
When the bug is larger, reduce it until you can show the failure clearly. Include the relevant version information when behavior depends on a library or runtime. Share an error message with credentials removed. Explain what changed immediately before the failure.
For a repository change, ask for a patch that follows the existing structure. Review what it touches, run the relevant checks and verify the behavior yourself. Do not paste API keys or private customer data into an example just because an error mentions them.
DarkGPT’s file limits vary by plan, and longer context can use more credits. A small relevant excerpt often gives a better starting point than uploading everything. Use follow-up questions to inspect the proposed fix: “Which assumption could make this wrong?” is a useful one.
A starting point for your next chat
Try this prompt.
Use this promptHelp me debug a small function. Ask me for the code, a failing input and the expected output. Explain the cause, propose the smallest fix and give runnable tests. Do not rewrite unrelated code.
Opens a new chat with the prompt filled in. You choose when to send it.