Petya Petrova - Fotolia
Overview of the problems automatic code generation can solve
Explore the ins and outs, pros and cons, and dos and don'ts of automatic code generation.
Automatic code generation capabilities continue to evolve within programming languages, IDEs and tools that work at compile time. This coding technique has proliferated because it can reduce mundane programming grunt work, and developers have found that it improves turnaround times and accuracy.
Let's examine automatic code generation, some tools for developers to consider and potential use cases in an enterprise setting.
Benefits and headaches
Fast turnaround times and accuracy are key advantages of automatic code generation. There's less customization, but automatic code generation tools allow developers to create code faster, said Eric McGee, senior network engineer at TRG Datacenters. Since the tools handle code generation from project descriptions, developers can instead focus on reducing code mistakes or working on other projects.
These tools can create problems. "Autogenerated code usually becomes a hindrance for developers who want to tweak it later on," McGee cautioned. Teams should plan to restrict these tools to only certain parts of the SDLC, such as where they can act as facilitators in smaller, less complex situations.
Different code generation types
Before organizations introduce automatic code generation, project leaders need to explicitly lay out what they mean by this development concept. Doing so will improve discussions across teams, said Jonathan Bartlett, senior research and development engineer at Specialized Bicycle Components, a sporting goods retailer headquartered in Morgan Hill, Calif.
Automatic code generation likely means one of three things to an organization:
Reflection support. Reflections in dynamic languages such as Ruby can automatically generate code within a running program. Reflection support works in a similar fashion to a smart search and automation process, which inspects the code within the language and acts in response.
Automatic code generation. Developers can use external tools required for statically typed languages -- such as Go -- to automatically generate source code.
IDEs. Visual Studio and similar IDEs allow developers to build logical structure declaratively using XML. This can then be translated into code.
The IDE scenario falls somewhere between reflection support and external code generation. It's cleaner because of standardized code generation and the wholly managed development environment. However, this angle's potential benefits are more limited.
Automatic code generation use cases
Teams need to consider the tradeoffs between reusable blocks of code to accomplish a task and automated code generation. Any kind of repetitive coding can slow development progress and introduce mistakes. Developers can mitigate these flaws with functions or by code generation. "These two options are actually more similar than people generally realize," Bartlett said, as they both eliminate manual coding work.
Code generation can automate API development in one system to match another, Bartlett said. For example, it's often simpler to use a code generation tool to write a code handler than to do so manually, because the tool automatically analyzes the database.
With automatic code generation, it's also easier to keep an API in sync with the database, since the tool can identify errors at compile time -- rather than as part of a separate testing step. Automatic code generation can also simplify code migration as companies adopt newer technology stacks.
"Our R&D team needs to reimplement the Bonita user portal every other year or so to stay up to date with web technologies and continue to offer our end users the best user experience," said Nicolas Chabanoles, CTO at Bonitasoft, an open source business process management tools provider.
A few years ago, the company moved to model-driven development and automatic code generation for the portal. As a result, Bonitasoft now saves several months of development time each time it updates the portal.
"It's only a matter of changing the code generator, while the essential information is kept in the model," Chabanoles said.
Common enterprise workflows
Steve Speicher, senior manager for product management at Red Hat, commonly sees three kinds of code generation workflows used by enterprise developers:
- creating starting templates
- identifying and updating dependencies
- generating program artifacts
At the beginning of many projects, developers might navigate a library of patterns or blueprints, select what's appropriate, fill in a few fields and the tools automatically create an application template. While this example is often associated with new applications, teams can also use it when they want to add health checks, monitoring or other capabilities. The biggest challenges relate to reducing the blueprints to only what the team needs and organizing them in an effective way.
Dependency-scanning tools automate the identification of dependencies with known vulnerabilities, which makes it easier to respond when a team discovers new security risks. In this case, the code generation automates the process of provisioning a quick fix for applications that use the dependency.
The future of auto code generation tools
One area that Bartlett looks forward to is tools that can convert diagrams and charts into code. Although there is some work in this area, he believes tool creators should pay more attention on how to flag changes that would affect the workflow between manual code automation and automated code generation. Ideally, this would make it easier to show different program flow views for business users and technical users so each team can understand how it relates to their requirements.
Speicher expects to see toolmakers more widely use AI to improve the workflows between domain-specific languages and coding tools to help developers quickly build out and maintain application code. The combination of better AI and new domain-specific languages could make it easier to detect problems, recommend fixes and repair the applications, services or dependencies that exist within an application domain.
Application code needs to be kept in sync with various supporting artifacts, such as API documentation, SDK and tests. Automatic code generation tools can help automate this process to ensure changes are propagated across artifacts when updated code is pushed into production. For example, software documentation tools such as Doxygen, GhostDoc, Javadoc and Docurium can create basic documentation from annotated source code.
Extending the power of code
Many teams turn to compile time tools to automatically generate code after developers have written the main code. For example, Google has protocol buffers to serialize structured data with diverse languages and platforms.
"At Google, we use protocol buffer for almost everything, from database schemas to API service endpoints," said Elvis Sun, a Google engineer who is also CTO and co-founder of Antaa, a wish-list app. His team at Antaa uses a built value tool to generate the builder classes required for their preferred programming style.
In Sun's opinion, it's important to distinguish between IDE code generation and compile time generation using tools like protocol buffer and built value. He found that a team still needs to code review, test and maintain IDE-generated code. He recommends using IDE code generation for lesser tasks, because it works better to generate code at compile time each time a system builds and executes code.
As a result, the code relies on well-tested libraries that are automatically updated. This reduces the manual work when changes are required.
Challenges with automation
While automated code generation removes a lot of tedious tasks from the hands of developers, there are possible perils.
Teams need to be aware that a continuous integration (CI) pipeline generates code the same way each time. Problems can occur, Sun said, when team members have local versions of code generation libraries, which results in different code generating from the same starting point. To solve this problem, standardize common automatic coding software versions and set up a CI pipeline that uses standardized code formatters, linters and lots of tests to provide a single source of truth.
Automated code generation can make programming faster in such a way that it's now harder to see potential ramifications, Bartlett said. The breaking points in programs can move drastically. "It causes minor changes to potentially have far-reaching consequences," Bartlett said. It also means a team will need to maintain, test and document the tools for metaprogramming, which involves dynamically programming tools that generate the actual code, including IDE and compile time code generation tools.
Ongoing evolution of software projects can be problematic in tools, such as Visual Studio, that make it difficult to identify generated code. Bartlett has found it useful to use a naming scheme for automatically created code, so that the generated code is easily recognized even in a file listing. However, sometimes there is crossover. Generated code sometimes needs manual modification, in which case it is no longer "generated," and requires developers to maintain it manually.