Code Generation: Cutting Through Development Routine
An article from a DevOps engineer at IT holding T1 explains how to implement code generation at various stages of the development lifecycle, from simple snippets to runtime generation. The author covers using concatenation, templating with Jinja2, and syntax trees (AST and CST) for modifying existing code, and discusses the future with LLMs and the Model Context Protocol (MCP). Practical advice is given on starting with code formatters and using tools like OpenAPI-generator and Cookiecutter.
The article, authored by Ivan, a DevOps engineer at IT holding T1, emphasizes that code generation is essential for ensuring code matches a specified schema like OpenAPI, preventing drift between schema and implementation. The author outlines levels of automation: IDE snippets, development-time generation, generation for common modules (API clients, message schemas), build-time generation (embedding backend URLs, license keys), deployment-time generation, and runtime generation (used in no-code/low-code environments, e.g., PyTest). Methods for creating generators are detailed: simple concatenation of strings, templating with Jinja2 for multi-language support, and using Abstract Syntax Trees (AST) in Python to refactor existing code, though AST loses comments and formatting. Concrete Syntax Trees (CST) via libcst preserve comments and style, enabling safe refactoring. The article also discusses the potential of LLMs and the Model Context Protocol (MCP) to reduce hallucinations by giving models tools to interact with the codebase, citing agents like 'oh my pi'. Practical advice includes starting with a code formatter, using OpenAPI-generator for service contracts, and Cookiecutter for microservice templates, concluding that code generation scales and cleans codebases but requires proper tool selection.
- Abbreviations
- IDE = Integrated Development Environment — интегрированная среда разработки
- API = Application Programming Interface — программный интерфейс приложения
- CLI = Command-Line Interface — интерфейс командной строки
- AST = Abstract Syntax Tree — абстрактное синтаксическое дерево
- CST = Concrete Syntax Tree — конкретное синтаксическое дерево
- LLM = Large Language Model — большая языковая модель
- MCP = Model Context Protocol — протокол контекста модели
- LSP = Language Server Protocol — протокол языкового сервера
Source: Habr — хаб ИИ —
original
