Code generators

Copy/pasting or re-using a piece of previously written code is an inevitable habit in programming. Code generators are invented to assist you achieving the same purpose. Code generators can help you to write same pattern of coding for a class, form or even a custom data file. While benefits of using code generators are countless like every other solution it has its own downsides. Nevertheless if it is used for appropriate task it can reduces from time of development and bugs of code.

Code generator in simple word produces some pieces of code using a given template and variables’ value which is provided at the time of using it. For example consider you want to write a repository for a data type or let’s say a class. Knowing that all repositories have methods for update, add, delete and save, you can write a code generator template to produce the interface for the repository. Without repository you probably copy and paste a previous written repository interface class and change the name of its class name. Thus by using a code generator you avoid copying and pasting which has its own downsides.

The first benefit of using code generators is speed in development.  Two or three clicks or issuing a command and producing the code without any further need of change are much faster than copying and pasting. In addition to the speed in development, the code generator’s produced-code is less prone to errors because you are not leaning in human efforts to fill the required gaps. For example consider an Ajax MVC form which is for “Create” operation of a model and produced both with copy and pasting method and also a code generator. This form needs an update target id which can simply be the parent division. If we lean on a human attention to have in mind changing the parent div’s class name, there is a high probability that the person fails to do that and if in a situation the source and the target form both are rendered in a page after submit they update same div which is not desired. Code generators can prevent such problems when you can simply provide a name for the form and tell the code generator to use the form name for the parent div name.

 Code generators are not an alternate for other concepts of programming. I’ve seen many people that excessive usage of code generators made their codes unmaintainable. For example repository implantations most of the time are the same and by that I mean the logic of add, update, delete and save are much the same. This resemblance triggers the OOP “generic” concept in mind. However I’ve seen people who use code generators to produce the repositories and at the end there are tons of repositories with much the same code. At the first glance this will do the same as a generic class does but the amount of code in a generic class is extremely less and it is more maintainable because if a change in the logic of one method is to be done, in code generator case several classes have to be modified.

Code generation is a new concept in programming which can replace old-school copy and pasting method with an engineering approach. Although there may be some disadvantages in excessive usage of code generators, appropriate corporation of code generator can reduce from time of development and the code which is produced is more robust and accurate. 

Read 1313 times Last modified on Sunday, 19 April 2015 10:36
Rate this item
0
(0 votes)
About Author
Leave a comment

Make sure you enter the (*) required information where indicated. HTML code is not allowed.

Advanced Programming Concepts
News Letter

Subscribe our Email News Letter to get Instant Update at anytime