html5-img
1 / 25

Code Generation

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

jonny
Download Presentation

Code Generation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Code Generation Presented by: Ashish Poddar Guided by: Prof. J. Fawcett Based on: Coder to Developer (Chapter 8) by Mike GunderLoy

  2. 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

  3. 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 !

  4. 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 !

  5. Smart Solution • Make a small throwaway code or use existing code generators to generate the code ! • Code Smith can be really handy

  6. 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

  7. 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; }

  8. 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 !

  9. 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; }

  10. Mixed code generator • Special comments might specify delegate code that needs to be created and added to the file • WinForm Application’s InitializeComponent ()

  11. 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

  12. Tier generator • UML products that integrate with your IDE • Rational Rose • Computer Aided Software Engineering (CASE) Tools

  13. 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 !

  14. 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

  15. 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)

  16. 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)

  17. 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

  18. 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

  19. 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...

  20. … 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 !

  21. 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?

  22. 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?

  23. Economics – Effort saved Vs Extra Effort required • Complexity of the tool • How efficiently the tool can be used for desired quality

  24. 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

  25. Thank You Ashish Poddar

More Related