📜 ⬆️ ⬇️

Where code generation is used

It struck me that so many people, in the comments to the previous article, express the view that code generation is the wrong technique. Let's look at some examples of how it is used now.

World wide web


Code generation is actively used on the Internet. Currently, the standard for providing information for Web sites is HTML. But decomposing static HTML pages on the site, we will not get a dynamic site that meets modern requirements, so site developers write software (site engines), the purpose of which is to generate HTML code for the user's browser. Examples of systems that help programmers in this difficult task: PHP, Perl, ASP.NET, Java, RubyOnRails, etc.

Moreover, not only server technologies generate HTML code, but client scripts (in particular JavaScript) often do the same.

Visual editors in development environments


Beginning with Microsoft Visual Basic, the visual editors of Windows forms went. It was necessary to transfer the button to the form and the careful generator would write its properties and location in a special file. First, the code was generated in special markup languages ​​(Visual Basic, Delphi), but then they began to generate forms in the same language in which the programmer writes. Later, the same approach was used in programming Web applications. Now it is difficult to imagine a serious programming environment without visual editors, which are kodogenerators.
')

Java, .NET and similar platforms


Platforms such as Java and .NET generate low-level, high-level code. In .NET, this technique is actively used for multilingualism. Many high-level languages ​​are generated in one low-level language, which is undoubtedly convenient. Moreover, the situation in the WWW is somewhat similar now: many different languages ​​generate HTML. And the results are similar: cross-platform.

MVC frameworks


Recently, MVC (Model-View-Controller) frameworks have become very popular. Integral components of these frameworks are code generator scripts. Some scripts generate Models based on DB, others generate Controllers and Views based on Models, etc. Without code generation, working in such frameworks would be pretty routine. Examples of MVC frameworks: RubyOnRails, CakePHP.

LINQ


LINQ (Language Integrated Query) appeared in C # version 3. This is quite a handy addition to C #, which saves the programmer from the mass of routine work. The general approach (LinqToObject) uses the generation of anonymous methods and classes, and the LinqToSql approach uses the generation of SQL code for Microsoft SQL Server. A LINQ implementation is being prepared for Oracle and many other data sources. The idea is excellent - implementations for specific data types are generated from a single high-level language.

I will give a simple example of use:

var onlineUsers = from user in DB.Users where user.LastActivity> DateTime.Now.AddMinutes (-5) orderby user.LastActivity descending select user;


CodeSmith


CodeSmith is a professional code generator based on templates. The result of the work is not tied to any specific programming language. For example, the distribution includes templates for generating C # code. VB.NET and T-SQL. The program consists of two parts: free Code Smith Explorer and paid Code Smith Studio. With the help of Explorer, code generation is performed, and Studio is a medium for developing your own templates.

These are the examples that came to mind first. You yourself can add to the list.

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


All Articles