Advantages and Disadvantages of code generation: you can write code code

The current state of the code generation

The current state of the code generation is ubiquitous (Spring 2019).
Today, code generation occurs in each layer of the software stack,

  • Including Java libraries (such as swagger CodeGen),
  • The latest cross-compiler / compiler (such as Babel for JavaScript applications)
  • And generating a full stack (such as Starter StackGen (tm))

REST API surge in the number of resulting species in the past decade for a variety of programming languages and development environments API Client Creator variety.
Like some kind of huge numbers Turducken, like, REST API and generated client seems to be able to connect anything to anything.
So let's look at turkeys, ducks and chickens code generation management involved ......

1. Code rewrite

A widespread form of automatic coding is to rewrite the code - used to convert the syntax of a language version to another language, and sometimes even completely converted to another language.
For example, languages such as Scala and the like actually will rewrite your code compatible with Java code.
The famous strange Lombok project basically rewritten in Java code as you type, generating phantom method, so you can focus on the good.
Like Babel, any good JavaScript developer can prove "compiler" is how your senior ES2017 is compatible rewrite has been used for 10 years, and the legacy browser JavaScript, so you can write modern code without having to worry about the behavior of the browser.

Let the computer do their dirty work? ? sweet!

2. Based on the template generation

React ecosystems create the application framework templates (including the React's "create-react-app" CLI command) based on aspects particularly active.
Throughout the software industry, other template-based code generation are also common. HTML templates and popular new static site generator usually use the popular handlebar mustache or template engine to use template parsing.
In the server template engine (eg Velocity) and a handlebar mustache and implement server-based, spit out the source file pre-rendered (such as HTML and more and more JavaScript).

3. Run Dynamic Code

The template is not the only method code generation.
By introspection, such as Java and Java Reflection API and other languages as well as Ruby Metaprogramming, run the code can be "self-aware", once your code self-aware, to build truly dynamic application of real options will expand.
In Starter, we use Java to build a StackGen generator engine, which means we can use the convenient JavaPoet library to build dynamic Java class files, including dynamically at runtime synthesis function. Interestingly, these classes can be compiled by the class loader in memory, without having to be written to disk.
They say that Java is not a dynamic language!
This type of dynamically generated code is exciting because it not only saves us time and manual coding of money, but also provides the opportunity to create the response behavior of the application at runtime, without human intervention (possibly based on AI decisions) to make our applications, and digital experiences more attractive and practical than ever.

But the generated code sucks!

Unfortunately, the full code generator in the history of the software industry, without code development tools, sample WYSIWYG front-end generator, and never seemed troubled by the demise of the old platform spit universal encryption source code Terror ( I'm looking for or should we say Drumbeat 2000 on your DreamWeaver Ultradev?!).
When the machine is full of obscure technical and confusing variable names, who want to deal with the code machine spit out? The former generated code is just frustrating and sad, but you want to create anything at all.

Fortunately, times have changed

In 2019, best practices, tools, and software development has evolved into humans and machines can really meet in the middle point. Such as APIcur.io, StackGen and Microsoft PowerApps like code generator through development, we can overcome the limitations of the past, while all the best features of code generation technology combined with modern development tools.

Code generation advantage

  • No manual fewer errors encoded =
  • Achieve larger, more complex patterns may save a lot of time
  • Fast and simple application to market the PoC
  • Stable infrastructure means less time wasted
  • CI / CD-friendly, may be added to the code generation step as any conduit or CI development workflow
  • Generating an error end process eliminates multiple source files and file types
  • New features can be integrated into the basic template to build the cross-large code bases in a single implementation
  • This is not to say that the code generation without its drawbacks.

Disadvantage code generation

  • Not all applications will benefit from code generation
  • Compared to manually select each code libraries, and coding style pattern, code generation is more inflexible
  • Changes to the underlying templates will be extended to all generated files, so changes must be highly compatible and fully tested
  • Re-generate the presence of risk coverage developer code and should be without prejudice to any existing code in the case - generated code must be carefully isolated and developer code in the code library
  • Code generation will add some complexity - for example, to really understand and cooperate with the generator, and the reasons you need to know the code generation - beyond the code base in addition to general questions
  • When writing code to write other code mode of thinking may become loose, especially when writing build the application to other applications

LCDP and innovation to the brink

LCDP (low-source development platform) as a new method for the latest forms of "codeless development" as well as bridging the advanced user / jr attracted attention. Developers gap.
With the growth in demand for new features and new system, developers become stretched, many users are overloaded Excel spreadsheet bound, or because of all levels of developer resources are scarce and there is no required software functionality.
Consumers SaaS solutions can do a lot of one-time task, but a bunch of disorganized Web applications and services are not reliable to meet many basic needs.
On the other hand, for many organizations, expensive enterprise systems is not an option.
In addition to saving development costs, authorized "citizen developers" build their own solutions, where appropriate, is the main driving force LCDP value proposition.
Code generator located directly intermediate solutions solution domain.

OpenAPI end use and generation StackGen

StackGen holistic approach to generate code - based OpenAPI / Swagger mode.
Development started from the beginning, focusing on the use of excellent OpenAPI / Swagger Design API.
Then, the design criteria allows you to generate robust stack with predictable behavior, and use any custom logic and front-end design here its precise expansion.
To achieve this flexibility, StackGen using three kinds of code generation step and three different implementations.
Three main components of the system used to generate a variety of techniques: Swagger CodeGen, MyBatis DAO generated and mapping, as well as the upcoming StackGen PRO, ReactJS distal generated.
Swagger CodeGen and StackGen React React generate code that uses content from Mustache Mustache template file.

  • Mustache
  • JavaPoet
  • Swagger CodeGen
  • MyBatis Generator

The advantage of this method is that it can be inserted, and allows us to generate at run time and before compiling.
By generating a high-quality source of developer-friendly format, modern code generator in the modern highly scalable and robust reference code base, while the efficiency of the full stack developer multiplication.

About the Author

John McMahon (John McMahon) is the CEO of Starter Inc., the company founder Extentech Inc, and over the years is the developer StackGen, Sheetster, OpenXLS, ExtenXLS and many software projects.

Original translation

https://medium.com/bigdecimal/code-that-codes-pros-and-cons-of-code-generators-15b2e571281a

Guess you like

Origin www.cnblogs.com/victor2302/p/11809944.html