📜 ⬆️ ⬇️

Foreword

I do not know why, but so little attention is paid to this crucial technology. I want to improve the situation somewhat, so this is the first article in the “Code Generation” cycle. When considering this topic PHP language and MySQL database will be used, but code generation by itself is possible in any language and using any database, just in PHP it will be easier for me to explain some important points. I will also pay attention to the state of affairs in other systems and languages.

This article is devoted to one question: what problems are present in modern programming.


What are the problems now?


In order to build complex systems with code reuse, the PLO (object-oriented approach) was invented. This approach is used for a long time and brings good results.
')
What is inherent in OOP? Turn to Wikipedia.

Data abstraction
Objects constitute a simplified, idealized description of real entities of the domain. If the corresponding models are adequate to the problem being solved, then it is much more convenient to work with them than with a low-level description of all possible properties and reactions of the object.

Encapsulation
Encapsulation is a principle according to which any class should be considered as a black box - a class user should see and use only the interface part of the class (that is, the list of the declared properties and methods of the class) and not delve into its internal implementation. Therefore, it is customary to encapsulate data in a class in such a way that access to it by reading or writing is not carried out directly, but by using methods. The principle of encapsulation (theoretically) allows minimizing the number of connections between classes and, accordingly, simplifying the independent implementation and modification of classes.

Inheritance
Inheritance is the ability to generate one class from another while preserving all the properties and methods of the ancestor class (the progenitor, sometimes called the superclass) and adding, if necessary, new properties and methods. A set of classes related by inheritance is called a hierarchy. Inheritance is intended to display a real-world property such as hierarchy.

Polymorphism
Polymorphism is a phenomenon in which the same program code (polymorphic code) is executed differently depending on the object of which class is used when calling this code. Polymorphism is provided by the fact that in the descendant class the implementation of the method of the ancestor class is modified with the obligatory preservation of the method signature. This ensures that the ancestor class interface remains unchanged and allows you to bind the name of the method in the code with different classes - from the object of which class the call is made, from that class the method with the given name is taken. Such a mechanism is called dynamic (or late) binding - in contrast to the static (early) binding performed at the compilation stage of the program.


But are such approaches now justifying themselves?

What we see now:

1. Multilevel inheritance.

In modern complex systems (especially .NET, Java), many classes have many levels of inheritance and properties, together with methods, are summarized in the final classes. On the one hand, this is good: a gradual increase in complexity from primitive classes to specific components. But on the other hand, a programmer who uses a finite class uses no more than 10-20% of its capabilities in each particular case (for example, in ASP.NET GridView has hundreds of properties, many of which are not needed in the project at all). But such a large number of methods and properties bears the complexity. First, for the programmer. When he types the name of an object and presses a point, huge lists are opened, among which it is difficult to find what is needed. Secondly, it all takes memory and processor time.

2. Multi-level abstractions.

When designing modern systems, multi-level abstractions are used. At the top level - the essence of the real world. And the closer to the bottom, the farther from the entities of the real world to the entities of the performing environment. This approach is good, but again carries complexity. It turns out a lot of classes. A programmer who works with such systems should keep in mind a lot of information when building and maintaining this system. Moreover, to coordinate abstractions, it is often necessary to use non-optimal methods.

3. Routine work.

Whatever the system, it is often the routine monotonous work that a programmer must perform. For example, the creation of uniform pages, where each next page differs from the previous one only in the source or presentation of data. In such situations “copy-and-paste” is often used. But if a definite change needs to be made to the mass of the same monotonous pages, then the routine work again appears. But nobody will be able to build abstract models for such minor reasons, because classes need to be designed.

4. Libraries.

It is difficult to imagine a project that would not use any class / function library. The programmer is not only forced to study these libraries, but also has to follow their rules and abstractions, which again adds complexity to the project and forces the programmer to keep in mind a large amount of information.

Total


Modern approaches give rise to excessive complexity and routine. And complexity is the enemy of any project. The simpler the project, the better it is.

In the following articles we will look at how code generation can solve such problems.

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


All Articles