Skip to content
BENIBIX Learn

Code Generation and Completion

Level 5 · Prompts for code

How AI code completion and code generation are not the same. How to prompt each one, and why code that runs on the first try still has to be read.

Course contentsLesson 13 of 16

After this lesson you can

  • Tell code completion from code generation
  • Write a code request with inputs, outputs and tests
  • Read suggested code before you keep it

11 mincovers 3 glossary entries

Worth reading first: Model Selection and Testing

How AI suggests and writes code

AI code generation writes program code from a plain request. AI code completion suggests the next part of the code as you type, right in the editor. In a 2022 test by GitHub, 95 developers wrote the same small program. Those using Copilot finished 55% faster, in about 71 minutes against 161.

Completion is the quieter of the two. It reads the file and the code around your cursor, then offers anything from the rest of a line to a whole function. You press a key to take it, or keep typing to skip it. It suggests, and it never runs anything.

Generation starts from words. You describe what the code should do, and the model writes it: one function, a test or a small app. The prompt rules from the rest of this course apply. Say the language, the inputs, the outputs and what should happen when something goes wrong.

For completion, the prompt is the code itself. A clear function name, a short comment saying what it does and the types of its inputs all steer the guess. A function called f with no comment gets a guess. One called is_weekend, taking a date, gets the right two lines.

Generated code often runs on the first try. That is not the same as being right. It can miss a rare case, use an old way of doing things or call a library function that does not exist. Running it once proves it runs once.

Ask for tests with the code. A prompt that says write the function and five tests, including an empty input, gives you a way to check the work. Read the tests first. If they test the wrong thing, the code will pass the wrong thing.

Read every line you keep. Suggestions look right more often than they are right, and the speed makes it easy to stop reading. The time saved on typing is only saved if you do not spend it later hunting for the bug.

text
Write a Python function is_palindrome(word) that returns True
if the word reads the same both ways, ignoring capitals.
Add five tests, including an empty string and "Anna".
A code request names the language, the input and the output. It also asks for the tests.
The grey lines are only a suggestion. One key takes them, and typing on skips them.

Worked example

Emma adds a weekend check

Emma types the first line of an is_weekend function, and completion offers two more lines. She presses Tab to take them.

Then she asks a chat model for five tests. The Sunday test fails, because the suggestion counted the days of the week the wrong way. Two minutes of reading would have caught it.

The words for this lesson3

Check yourself

Is AI-written code right if it runs on the first try?

Finished this lesson?

Marking it complete also marks its 3 glossary entries learned, which updates your roadmap and your saved page. 0 are already marked.