Developer power tools

406 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
406
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
2
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Developer power tools

  1. 1. Power Tools for the Modern Programmer Regular Expressions, Reflection, Code Generation
  2. 2. Introduction <ul><li>Leverage advanced practices to build adaptive business solutions </li></ul><ul><li>Boost your own productivity by reducing repetitive tedious code </li></ul><ul><li>Improve quality and consistency by reducing repetitive error prone code </li></ul>
  3. 3. What We Will Cover <ul><li>Power Tools </li></ul><ul><li>Sample Application </li></ul><ul><li>Further Ideas </li></ul><ul><li>Sources for More Information </li></ul>
  4. 4. Power Tools <ul><li>Regular expressions Take string manipulation to the next level </li></ul><ul><li>Reflection Interrogate and manipulate code metadata at run time </li></ul><ul><li>Code generation Make the computer write its own code </li></ul><ul><li>Design patterns Leverage best practices in your designs </li></ul>
  5. 5. Overview - Reflection <ul><li>Create adaptive solutions </li></ul><ul><li>Simplify code complexity </li></ul><ul><li>Ease maintenance </li></ul><ul><li>Ease deployment </li></ul><ul><li>Solve configuration management issues </li></ul>
  6. 6. Overview Regular Expressions <ul><li>Regular expressions are to string as math is to numbers </li></ul><ul><li>Simplify and optimize string manipulation </li></ul><ul><li>Use editing tools to simplify creating regular expressions </li></ul><ul><li>Document! Document! Document! </li></ul>
  7. 7. Overview Code Generation <ul><li>Obtain language independence with the CodeDom </li></ul><ul><li>Use meta data to drive the code generation </li></ul><ul><li>Use meta data to provide code documentation </li></ul><ul><li>Standardize repetitive tasks with consistent implementation </li></ul>
  8. 8. Overview Patterns <ul><li>Leverage emerging best practices </li></ul><ul><li>Leverage existing documentation </li></ul><ul><li>Don’t go over board </li></ul><ul><li>The KISS Principle still applies </li></ul>
  9. 9. Sample Problem <ul><li>Parse fixed length record file </li></ul><ul><li>Parse with multiple purposes at different times (Load in a database, display on a document, display in a web page, etc) </li></ul><ul><li>Need to parse multiple file with different formats </li></ul>
  10. 10. Sample Format <ul><li>Customer order tracking file </li></ul><ul><li>Customer Record </li></ul><ul><li>Order Record </li></ul><ul><li>Order Item Record </li></ul><ul><li>Address Record </li></ul>
  11. 11. Customer <ul><li>CST Record Identifer </li></ul><ul><li>23 Characters Customer ID </li></ul><ul><li>25 Characters First Name </li></ul><ul><li>25 Characters Last Name </li></ul><ul><li>3 Characters Age </li></ul>
  12. 12. Order Record <ul><li>ODR Record Identifier </li></ul><ul><li>8 Characters Order Date </li></ul><ul><li>8 Characters Required Date </li></ul><ul><li>8 Characters Shipped Date </li></ul>
  13. 13. Order Item Record <ul><li>ITM Record Identifier </li></ul><ul><li>25 Characters Product ID </li></ul><ul><li>5 Characters Quantity </li></ul><ul><li>5 Characters Unit Price </li></ul><ul><li>4 Characters Discount </li></ul>
  14. 14. Address Record <ul><li>ADR Record Identifier </li></ul><ul><li>60 Characters Street Address </li></ul><ul><li>75 Characters City </li></ul><ul><li>2 Characters State </li></ul><ul><li>5 Characters Zip Code </li></ul><ul><li>4 Characters Zip Plus 4 </li></ul>
  15. 15. Very Easy Regular Expression <ul><li>“ CST(?<CustomerID>.{23} </li></ul><ul><li>(?<FirstName>.{25}) </li></ul><ul><li>(?<LastName>.{25}) </li></ul><ul><li>(?<Age>.{3})” </li></ul>
  16. 16. Regular Expression Notes <ul><li>Verify that the input string starts as expected </li></ul><ul><li>Allow any character for the specified number of times </li></ul><ul><li>Use named explicit capture groups </li></ul>
  17. 17. Defining Record Objects <ul><li>Define a Constructor expecting a string to parse </li></ul><ul><li>Define read only strongly typed property for each element in the record </li></ul>
  18. 18. Building the Record Object <ul><li>Metadata storage </li></ul><ul><li>Generation Process </li></ul><ul><li>Provide documentation </li></ul>
  19. 19. Metadata
  20. 20. Generation Process <ul><li>CodeNamespace </li></ul><ul><li>CodeTypeDeclaration </li></ul><ul><li>CodeConstructor </li></ul><ul><li>CodeMemberProperty </li></ul><ul><li>CodeMemberMethod </li></ul><ul><li>CodeVariableDeclaration </li></ul><ul><li>CodeAssignmentStatement </li></ul>
  21. 21. Documentation Process <ul><li>Simple reports </li></ul><ul><li>Documentation is accurate because it drives program execution </li></ul>
  22. 22. Reflection to Reduce Complexity <ul><li>One measure of complexity is the number of paths through the code </li></ul><ul><li>Multiple paths may lead to redundant code </li></ul><ul><li>Multiple paths may lead to confusing flow control </li></ul><ul><li>Multiple paths may lead to higher maintenance requirements </li></ul>
  23. 23. Identifying Record Objects (Hard Coded) <ul><li>Separate case statement for each record type </li></ul><ul><li>Brittle dependency on the file format </li></ul><ul><li>More complex formats lead to more complex parsing </li></ul><ul><li>Parsing a different format requires a different parser </li></ul>
  24. 24. <ul><li>private void HardCodedParseLine (string inputLine) </li></ul><ul><li>{ </li></ul><ul><li>string prefix = inputLine.Substring (0,3); </li></ul><ul><li>object parsedObject = null; </li></ul><ul><li>switch (prefix) </li></ul><ul><li>{ </li></ul><ul><li> case &quot;CST&quot;: </li></ul><ul><li> { </li></ul><ul><li>parsedObject = new Customer (inputLine); </li></ul><ul><li>break; </li></ul><ul><li> } </li></ul><ul><li> case &quot;ODR&quot;: </li></ul><ul><li> { </li></ul><ul><li>parsedObject = new Order (inputLine); </li></ul><ul><li>break; </li></ul><ul><li> } </li></ul><ul><li> . . . </li></ul><ul><li>} </li></ul><ul><li>DisplayObjectDetails (parsedObject, tvwResults); </li></ul><ul><li>} </li></ul>
  25. 25. Identifying Record Objects (Reflective) <ul><li>Single path through the code </li></ul><ul><li>Code complexity stays constant even as file format complexity increases </li></ul><ul><li>Same parser can handle parsing files with different formats </li></ul>
  26. 26. <ul><li>private void ReflectiveParseLine (string inputLine) </li></ul><ul><li>{ </li></ul><ul><li>string prefix = inputLine.Substring (0,3); </li></ul><ul><li>Type parseType = (Type)mTypes[prefix]; </li></ul><ul><li>Object [] parameters = {inputLine}; </li></ul><ul><li>if (parseType != null) </li></ul><ul><li>{ </li></ul><ul><li>object parseObject = Activator.CreateInstance (parseType, parameters); </li></ul><ul><li>Common.Helpers.DisplayObjectDetails </li></ul><ul><li>(parseObject, tvwResults); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  27. 27. Visitor Pattern <ul><li>Create enhanced flexibility with events </li></ul><ul><li>Define an event that will be raised when a record object is identified </li></ul><ul><li>Calling objects “visit” each discovered object by subscribing to the event </li></ul><ul><li>Separation of responsibilities </li></ul><ul><ul><li>Parser knows how to identify record objects but not what to do with them </li></ul></ul><ul><ul><li>Visitors know what to do with the record objects but not how to find them </li></ul></ul>
  28. 28. Further Ideas <ul><li>Load the Record Object types through reflection or from a config file instead of hard coding them into hash table </li></ul><ul><li>Use reflection to identify the method to be called from the event handler specific to each record object type </li></ul><ul><li>Generate and compile the record objects at run time for the ultimate in flexibility </li></ul>
  29. 29. Sources for More Information <ul><li>Reflection </li></ul><ul><ul><li>http://www.oreillynet.com/pub/au/1073 </li></ul></ul><ul><li>Regular Expressions </li></ul><ul><ul><li>http://www.ultrapico.com/Expresso.htm </li></ul></ul><ul><ul><li>http://weblogs.asp.net/rosherove/articles/6863.aspx </li></ul></ul><ul><li>Code Generation </li></ul><ul><ul><li>http://www.ondotnet.com/pub/a/dotnet/2003/02/03/codedom.html </li></ul></ul>

×