FAQ on code generation (Translation)

Source: Internet
Author: User
Tags abstract definition
I finally finished it before work and skipped some unimportant places. I feel that I have not understood the translation in many places, but I have gained more understanding in the translation process. Sorry, but please give me some advice.
Wish you a happy New Year
For the original article, see Code Generation FAQ.

Note: the translation in this article is only used for learning without any permission. Do not use it for commercial purposes.

What is code generation?

Code generation is a technology that uses programs to generate code. These programs include scripts of a very small help nature, and business logic models that create a large number of complete applications. There is no fixed pattern for code generation applications. You can use the command line or GUI to create code in one or more programming languages and create code multiple times. There is no fixed input or output. The common feature of code generation is that the output of the generator is code, which can be compiled manually.

 

What are the benefits of code generation?

The benefits of code generation can be summarized into four aspects:

1.Quality: The quality of the code generated by the code generator is determined by the quality of the code and template used to generate the target code. When the quality of these codes and templates is improved, the quality of the re-generated code is also improved based on this.

2.Consistency: The code generated by the code generator has strong consistency. variable naming, method naming, and class naming are all created in the same way. These make the generated code easy to use and hierarchical.

3.Productivity:When the code generator runs for the first time, it is easy to recognize the advantages of the code generation technology. You start by entering some input required in the design of the output Code. At the same time, you get the Code implemented by your design. In any case, true efficiency begins when you run the generator to create design-based code. How many times have you seen that the demand for a project has not changed? This is the ability to generate code based on changing requirements.

4.Abstract:Some generators create code based on the abstract model of the target code. For example, you can create an SQL Schema and data access Layer Code to define tables, fields, relationships, and queries in the database from xml. The value of this abstraction lies in the ability of generators to relocate code on other platforms. This means that your business logic and rules are portable. You do not have to write code for different platforms.

 

Why are there two views on code generation?

One positive view is that code generation provides a lot of benefits in the short term. "God" often understands the benefits of code generation to the problems they face in projects. Just like 'a-ha' when the object orientation comes. This can easily lead to an idea of "finding a silver bullet", but it was not long before that, and there were errors in different directions. Code generation should be used in appropriate places.

On the contrary, some engineers were stabbed by code generation. They either use generators, or use generated code, which is gradually corrupted, or the generators they develop are not actually applied. For whatever reason, some engineers do not like code generation.

 

What is the workflow of code generation?

The fundamental software development process is: write code-compile-test. Write down some code, compile it (if needed), and test it. Repeat this cycle if necessary. The code generation process depends on active or passive code generation. You can stop editing the Code created by the active code generator, or you can edit a specific part to make it look like it is generated. The passive code generator creates the code at a time, and then you can write the core content yourself.

The workflow of passive code generation is to run the generator and follow the process of writing code, compiling, and testing for the generated code.

For an active code generator, run the generator first, then compile and test the output. At this point, if you find problems in the generated code, you can modify the template or change the input of the generator to regenerate. If the problem occurs in the handwritten code section, follow the write-compile-test process.

 

What are active and passive generators?

You do not need to modify the code created by the active generator or modify the specific markup parts that remain secure during the re-generation cycle. The active generator aims to generate the same output for different inputs.

The passive generator creates code at a time and then submits it to you for maintenance so that the code can run for a long time.

Passive generators are efficient at the beginning. But the benefits of active generation-code maintenance and bug modification in the template are lost in passive generation.

 

What is generated using a generator?

Code generators can be used in many different types of application software development. Database Access and user interfaces are commonly used.

Most of them are used for database access, because the data access code is highly repetitive. In addition, the data access layer provides a standard for the portability of technical platforms and the benefits of portable platforms.

The user interface code is also highly repetitive, so it also becomes the common goal of the generator. In addition, a user interface generator can also provide multiple implementations, such as generating fat clients and thin clients for the same definition.

 

What is under the generator?

Like many technologies, code generation has both sides. While implementing similar tools, you should be sure you have mastered these points.

1.Documentation and Training:You need the document of the Code Generator to explain how the generator works and how to maintain it. You also need to teach other persons in the project team the value of the generator and how to use it.

2.Maintenance:Adding a generator to your workflow is to add a new tool to your application environment, which means that you will release and maintain the tool within its lifecycle.

3.Complexity:Depending on the complexity of the project and generator itself, you get a tool for generating a baroque design. Maybe this is a challenging problem. It can be said that the best generator is often very simple. It can simplify the work of a few days into several hours of macros. Therefore, complexity is not a must for generators.

It is important to understand when the generator should be used and when it should not be used. Code Generation cannot replace object-oriented design.

What types of generators are there?

It is difficult to classify applications, especially the diversity of code generators. One way to differentiate is to observe the input and output of the generator. At the input end, you can use code as the input, or an abstraction depicted by a design. At the output end, you can create input based on some conditions or design new Code partially implemented. Or the fully-implemented code is designed and the requirement is not extended.

Based on such input and output, we can get some generator models:

1.Code munger:Code munger reads the Code as the input, and creates a new Code as the output. The new Code can be partially or completely dependent on the generator design, Xdoclet, the example of Code munger is to create Beans that support EJB through embedded annotations in JavaDoc Code.

2.Inline code booster:This model reads code as input and creates new code based on the input code. However, some of the Code is extended based on the original code design. The embedded SQL language, such as Pro * C, is an example of the Inline code booster. SQL is written in C code. The generator creates C code by extending the SQL in the C code that implements commands and queries.

3.Hybrid Code Generator:This model uses code as the input, and then creates new code based on the input code, but returns the output code to the input file. The wizard is often implemented as a hybrid code generator. Special comments embedded in the Code to define the scope for the generator to add new code.

4.Local Code Generator:The local code generator uses an abstract definition as the input, and then creates the output code. You can use subclass to complete the design.

5.Layer generator:This type of generator creates a complete output code set from an abstract design. This code means that "as-is" is used without any extension. This is currently the most common form of open-source and commercial generators.

 

Which developers are most concerned about code generation?

There are many concerns about new technologies and tools such as developer object code generation. Fear, worry, and doubt. There are also some reasonable concerns to blame on current or previous technical experience. We hope to pay more attention to the following aspects:

1.Too many code generation tasks are required.: Many code Generators on the market are Tier Generators, which means they are designed to create a large amount of code from a code abstraction model. To generate so much code, you need to be forced to learn many tools and output code. However, this is not the only generator. In addition to some small aspects, many small generators can provide the same benefits. You can create your own generator to meet your requirements. Because most of the more widely commercialized generators with targets do not mean smaller generators with specific targets have higher availability.

2.Code Generation cannot work on the basis of existing code: This is indeed a problem. The layer generator tends to generate new code for the project, mainly because it is easier to hit the target. Writing code in the existing framework means large-scale modification. Templates used to create code adapt to them to meet existing standards. There is a tool, such as Codify, that can work well with existing code and new code.

3.Poor quality of output code: The generated code maintains the consistency level of quality. This level can be from bad to good, depending on the quality of the Code and the template used to create the code. In ideal conditions, the quality is good, but if not, there is still the benefit of consistency. The quality of handwritten code tends to be different during the project, but the overall quality is improved. But this is often not the case. Qiu Gill was attacked by a woman: "Qiu Gill! You are an alcoholic !". He replied, "Maybe I am an alcoholic, but you are ugly. I will be awake tomorrow, but you are still ugly ". This also occurs on the code generator. The code generator can be edited to create consistent and good code. However, inconsistent code has been written manually.

4.Code generation will eat up my work: If your work involves writing a lot of code. Try the automatic process. You may be right. But many of us enter this industry because they love creating interesting programs that can help people. Code Generation helps you create programs faster. Therefore, code generation brings many opportunities for many people.

5.Code Generation is too complex:The generator focuses on the complexity of the Code to be created and the complexity of the generated relationship. They can be complex, but the output of the generator is often easier to debug and maintain than the function-based code in a flexible Runtime Library. Although this is true, you must pay attention to the complexity of the generator, whether during development or deployment .. The principle of "you aren't going to need it" (YAGNI) must be strictly used for generator design and implementation.

6.You cannot generateXTechnology:Any text file can be generated. So there is nothing impossible. One problem is to create an input-output comparison between the generator and the handwritten code. For example, a generator can be generated by itself, but I assume it is a complex project.

7.Generators are a good starting point, but they never maintain:This is a very reasonable phase method. I have seen many times that the generated code has been manually modified. The comments of "do not edit" still exist in the Code, which has been ignored for a long time. Indeed, this can only be solved through long-term training on how to maintain the generated code. However, there are also some practical points, that is, the generated code is never retained in the code management system. Code Generation is an active part of the creation process. Ensure that the Code has never been manually modified, and the generator is frequently maintained or discarded.

 

Code Generation andCASEWhat is the relationship between tools?

The case tool was exaggerated as "the end of programmers" in late 1980s and early 1990s ". Analysts can use visualization tools to define business logic, and the builder can create all program code. In this regard, these voices are more like MDA, compiling UML to code for popular platforms such as J2EE and. NET. The boundaries of code generation and the core of the CASE tool fall in the same place. Use a generator to create an application from an abstract definition.

The difference between code generation is that it is a bottom-up method to reduce the coding workload. Developers write their own tools to make their work easier. The CASE tools are self-built, and they attempt to use tools to replace trained developers. This is the most fundamental difference between the two in some aspects.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.