What are the advantages and disadvantages of code generation?

There may be different types of code generation. In RoR, for example, Rails can create skeletons for models, controllers, etc. But the developer must complete these skeletons.

Now several times there are projects in which many basic artifacts are fully generated according to a set of definitions or models.

I'm mostly interested in learning about the advantages and disadvantages of this type of code generation.

+4
source share
3 answers

The main advantage is that it does the job for you, its repeatability and that the code is likely to work (it depends, of course, if the person who wrote the generator knew what they were doing). It can remove a lot of necessary time, performing tasks with black code. For example, should you really write objects that are nothing more than containers for data from a database, or is it better for some program to automatically create them for you?

The big disadvantage is that it forces you to write code that is compatible with the generated code. In most cases, this is not a problem, but it can be a real problem when someone comes up to you and says, β€œHey, can we make X?” and this conflicts with the generated code. If the generator is good, it will allow you to change the functionality, but it almost always increases the complexity of the generated code, etc. This complexity has a price. This is harder to understand, and it may be less efficient than the code you write yourself. This, of course, depends on the situation.

+6
source

The main problem with this programming style is that it pollutes the presentation of your project. This no longer allows you to practice DRY. It is useful to have a clean separation between what is automatically generated and what is written by man. Most systems, especially file systems, do not support this separation. In systems that have good introspection capabilities (for example, small images), creating a structure of dynamic objects by walking by definition / model is preferable.

In illusion-based programming (as practiced by large companies and government agencies) this is very useful because it allows you to create impressive stacks of documentation and demonstrate impressive execution performance, measured in lines of code over a human month. There, your most important skill is, of course, the time of your disappearance action.

+1
source

I think the most important thing to keep in mind is: WHY do you want to generate the source code. This, for example, is because you are more fluent in UML than any programming language, and therefore want to generate object-oriented classes from this graphical model?

Is it because you specified a schema definition in any language ( SQL DDL , for example: jOOQ , XSD , for example JAXB code generation ) and want to generate a model from this?

The advantage of code generation is always that you express something only once (as in DRY , as pointed out by Stephan ). This is a very good practice, which is deeply immersed in extreme programming (among other processes). When you hold DRY things, you do not risk that the model is different from the glue code. On the other hand, you can explode your glue code because it will exactly match its base model. Typically, you have one class / type / object for an RDMBS table or for an XML element.

If, however, you use code generation because it is more convenient for you to use a modeling language (as in MDA or model architecture ), you may run the risk that the generated code is not good enough (lack of details) or too complicated (lack of simplicity), because which, for example, UML is not suitable for detailed problem solving.

In any case: code generation can be very useful if the generated code can be used by AS-IS and does not need to be configured. Once you start customizing the generated code, it can become a nightmare for maintenance.

+1
source

Source: https://habr.com/ru/post/1333194/


All Articles