GlossaryAI code generation
AI code generation
AI code generation is the use of language models to write, edit and explain software from natural-language requests and surrounding code. It ranges from completing the next line in an editor to agents that change many files, run tests and propose a finished change.
How it works
Code models are language models trained on large amounts of source code and text. Given a request and context, such as open files, function signatures and error messages, they generate code that fits. A 2024 survey of language models for code generation covers how training data, model design and techniques such as retrieval of related code shape results.
Use falls into three modes. Completion suggests code as the engineer types. Chat answers questions and writes functions on request. Coding agents work on a task end to end: they read the repository, edit files, run commands and tests, and iterate on failures. A 2024 paper showed that the design of the interface between the agent and the computer, such as how it searches, views and edits files, strongly affects how well a coding agent solves real software issues.
The generated code is a draft. It compiles or fails, passes tests or fails, and it carries whatever bugs, outdated API calls or security flaws the model produced.
AI code generation vs code templates
Templates and code generators produce fixed code from a defined input, such as register maps or protocol definitions, and give the same output every time. AI code generation handles open-ended requests and messy context, but its output varies and needs review. Many firmware teams use both: deterministic generators for hardware definitions, AI for the logic around them.
Why it matters
It is the most common way engineers use AI day to day, for firmware, test scripts, data tools and automation, such as a script that parses functional test logs into pass and fail records or a bring-up routine that reads each sensor over I2C. It speeds up routine work and unfamiliar languages. It also shifts effort toward review, since code that looks right can be wrong in ways tests miss.
How to get it right
- Give the model the relevant code, interfaces and constraints, not only the request.
- Run generated code through the same tests, linters and review as human code.
- Check hardware details, such as register addresses and timing, against the datasheet.
- Follow secure development practices, such as those in NIST's Secure Software Development Framework.
- Keep changes small enough to review line by line.
AI code generation in robotics and drone hardware
Firmware for motor drivers, sensor interfaces and communication buses depends on exact register settings and timing. AI drafts that code quickly, but a wrong bit in a configuration register can damage hardware. Teams pair generated firmware with tests on real boards and real hardware before any release. Test code suits it well: a generated script for a test fixture or end-of-line test is easy to check by running it on a known good board and a known bad one.