Angular JS Services
Most of us have hit at least once in a lifetime situation where similar pieces of code have been written and rewritten pieces that differ only in some details. Have you ever wondered – while you were copy-paste from one class to another, and maybe then some 50 classes – why do this repetitive, error-prone and over boring when it could easily be performed a car? The machines are best suited for such tasks, but why avoid them? This article will be presented a program that generates code based on an XML input file and some flexible templates.
Code generation sounds more like a requirement for NASA engineers than for a group of programmers from Cluj working in the outsourcing industry. Great was our surprise when, one day welcome and long summer customer notify us to glad a new topic: a tool to generate code – and not just any, but one that generates code for multiple platforms and different programming languages, using an XML file as input and some templates.
To better understand the problem, to take an example. Let’s imagine that we have made an application that requires managing a warehouse that stores database using different types of electronic devices and appliances. This is a classic application database that can contain multiple formins and a lot of access to data type classes – DAL (Database Access Layer). If this warehouse is small and contains only a few types of products when writing data access classes does not seem to be a big problem. But like us to store all types of devices from mobile phones to monitors, routers, DVD players and create tables for each category? Writing all data access classes, even if they differ only slightly from one another, yet there seems to be a problem. Only this time solving the task would involve an allocation of time.
A small change or a new requirement brought the application may generate changes in all classes, which the mechanism of copy-paste from one class to another would lead to unexpected errors. In a scenario of this type, use a code generator we would enormously ease the work: it would generate all required classes, in addition to some further changes would only involve changes in template files. Thus, code refactoring would be quick and easy. A well-written code generator and flexible can be a helpful tool in any project.
The architecture of a code generator
A code generator is divided into two parts: the part that reads and analyzes the input file and the template files that analyzes and generates output files. Pass first component data of the second following a well-defined format. Therefore, any change to this format change requires both parties.
Writing a code generator is not an easy task; it takes more effort, but most often proves to be a good investment. Through this article, I tried to expose the main challenges that arise in the implementation of such applications in a manner as easy understanding. In our case, the decision to invest in deepening flex time – bison proved to be excellent. Because there we found reliable solutions, we have focused mostly on defining language. Flex – bison is a solution that satisfied our requirements, having a tidy documentation.
Reading the file input file must contain all information necessary for filling templates. They must be arranged in a form as structured and easily analyzed by a car. In this article, I’ll use an XML file. We illustrate a case where the generated code will be a C++ library based on STL. The input file must contain as much information about categories () to be mapped to classes/tables, fields and properties associated with each (). It’s recommended structure shown in the XML file, as it will ease writing template files.
Working with generating parser is an advantage should be highlighted: in the case of major changes or minor structural terms or the introduction of new requirements in existing code using the generation of code will be modified just a few files. This would benefit any programmer. Code generation using the code generator is really special.