250 likes | 454 Views
Code Generation. Presented by: Ashish Poddar Guided by: Prof. J. Fawcett Based on: Coder to Developer (Chapter 8) by Mike GunderLoy. What is this about?. Reusing code
E N D
Code Generation Presented by: Ashish Poddar Guided by: Prof. J. Fawcett Based on: Coder to Developer (Chapter 8) by Mike GunderLoy
What is this about? • Reusing code • Riddhiman Ghosh few weeks back discussed Software matrix. The whole concept is far beyond the scope for discussion here, but part (minute though) of what he wanted to achieve was Software Salvage (which is lifting a significant block of existing system and inserting it in a newly developed system) • If such a block of code can be made more generalized then we convert it into yet better system as a Code generator
Example – Library Management • Database is designed to contain 10 Tables • We have 7 operations that needs to be done for each of the tables • Making the total implementation for 70 operations !
Solution • Implement the procedures for one of the tables • Copy the statements for other tables • Rename the fields • Make the changes everywhere ! • Compile a couple of times to debug logical errors • Then hope everything is fixed but be prepared for surprises !
Smart Solution • Make a small throwaway code or use existing code generators to generate the code ! • Code Smith can be really handy
How does it work? • Using a template code • Inline code expander • Mixed code generator • Partial class generator • Tier generator from abstract definitions • Full domain language
Template Code (generator code) public virtual <%= ClassName %> Copy() { <%= ClassName %> collection = new <%= ClassName %>(this._count); <% if (ItemValueType || IsString(ItemType)) { %> for (int i = 0; i < this._count; i++) collection._array[i] = GetByIndex(i); <% } else { %> for (int i = 0; i < this._count; i++) { <%= ItemType %> item = GetByIndex(i); if ((object) item != null) collection._array[i] = (<%= ItemType%>) item.<%= DeepCopyItem%>(); } <% } %> collection._tail = collection._count = this._count; collection._version = this._version; return collection; }
Template Code (generated code) public virtual intQueue Copy() { intQueue collection = new intQueue(this._count); for (int i = 0; i < this._count; i++) collection._array[i] = GetByIndex(i); collection._tail = collection._count = this._count; collection._version = this._version; return collection; } • Isn’t Compiler also a Template Code Generator !
Inline code expander • Embedded SQL generators which allow you to drop SQL statements into C or Java code • #define macros ! int main() { <SQL select * from users> return 0; } int main() { DBHandle *db_Handle = db_connect(); DBQueryData *db_data = db_db_query (db_Handle, “select * from users”); for (int record = 0; record < db_data->length; record++) {…} return 0; }
Mixed code generator • Special comments might specify delegate code that needs to be created and added to the file • WinForm Application’s InitializeComponent ()
Partial class generator • Reads abstract definitions from some source and builds a base class source code file to implement the definition • In Visual Studio code layout for a derived class from an Interface or an Abstract Class
Tier generator • UML products that integrate with your IDE • Rational Rose • Computer Aided Software Engineering (CASE) Tools
Full domain language • Complete programming language created just for your problem • General purpose way to specify code that should be created • E.g. programs for creating and simulating circuits can be best done by SKILL which is built just for the purpose • General purpose for any circuits, but special purpose in sense that only for electronic circuits !
Code Generation Uses • Database access code • User Interface code • Documentation !!! • Unit Test Generators • Web Services (Code from WSDL file) • DLL wrappers for legacy code • Configuration or initialization files • Scripting files • Installation files
What this means is… • If you can describe an output that you’d like to get in some readable language, you can likely build a code generator to take the description to the actual output (Of course depends on whether it’s more work to write the output or build the generator)
Code generation for .NET • Increasing popularity and visibility of code generation in software development • Extensible nature of VS .NET • System.CodeDom and System.CodeDom.Compiler (System.dll)
Everybody is generating code • Coder to Developer states number of .NET Code Generation Tools like: • AlachiSoft TierDeveloper – go from database development to full application, integrates with Visual studio • Iron speed Designer – building complete ASP.NET applications from database and a set of layout pages • M3rlin – Free and open source project, uses ASP.NET engine to generate code based on templates and XML
Solutions Design LLBLGenPro – Visual Basic .NET or C# code from SQL server or Oracle databases. Features a lot of places where you can fine tune the generated code • Workstate Codify – VS.NET addin for templated code generation. Unique among the code generators as it can inject little bits of generated code into the middle of a class • Software CodeCharge Studio – Provides flexible environment for generating web applications, hosted in its own IDE, letting you target ASP, JSP, PHP, Perl, ColdFusion, ASP.NET
All that glitters… • They work by letting you specify things about your application like tables, structures, class names, general layout of pages, … • Saves time and prevents repetitive programming • But...
… is not Gold ! • Binding commitment to these tools ! • Difficult to maintain with lots of unnecessary lines ! • Rigid to custom changes ! • Cannot use new version of language until its code generator is released !
So… should we use it? • How maintainable is the generated code from that tool? • Does it need the presence of the tool for its lifetime or can it exist without it? • If so, when you abandon the tool, how long will it take to replace the code written by another tool or by hand?
Quality of the generated code is optimal or not? • May be better than the code written by yourself depending on your own level of experience • How well commented is it, in case you had to fix it ! • Is the template to generate the code flexible?
Economics – Effort saved Vs Extra Effort required • Complexity of the tool • How efficiently the tool can be used for desired quality
Code Generation Summary • When faced with large quantities of repetitive code • Evaluate multiple code generation tools to find the one • Plan for moving beyond the tool if necessary • Balance the learning curve against the time saved
Thank You Ashish Poddar