Code generation uses a natural-language description, comment, docstring, or other input to produce code for a specified programming language. Large language models (LLMs) are especially suited to code because code contains clear structures and repeated patterns. section 1.7 section 6.6
Common uses include:
- Suggesting code as a programmer types, completing repetitive code, offering alternatives, and converting comments into implementations. section 1.7 section 6.6
- Making a quick first attempt at a function, although complex functions can still fail and require iteration. section 6.6
- Generating documentation such as comments, docstrings, and type hints, and transforming code or schemas into visual diagrams. section 6.6
- Supporting coding interviews, testing, and programmers who are not native English speakers. section 1.7
Most tools began as pair programmers that complemented human developers rather than replacing them. Newer agentic systems can take actions directly, while “vibe coding” relies on prompts for much or all of a project. Greater autonomy does not guarantee reliability: one assessment found an autonomous system spending days on dead ends that a human engineer might have recognized quickly. section 1.7 section 6.6
Generated code remains subject to security, transparency, licensing, accuracy, and privacy concerns. Proprietary code should not be pasted into an external application programming interface (API), and generated output needs human checking before it is trusted. section 1.7 section 6.6
A practical prompt
A developer can describe what a function should do in a comment or docstring and ask an LLM to implement it. The result may provide a useful starting point, but the developer must assess whether it is correct and suitable for the task. section 6.6
Figure 6.4 The docstring produced by ChatGPT correctly describes the function given and each input.
A generated docstring demonstrates how code-generation tools can describe a function and each of its inputs. section 6.6
















