29
Power Tools for the Modern Programmer Regular Expressions, Reflection, Code Generation

Developer power tools

Embed Size (px)

Citation preview

Page 1: Developer power tools

Power Tools for the Modern ProgrammerRegular Expressions, Reflection, Code Generation

Page 2: Developer power tools

Introduction • Leverage advanced practices to build

adaptive business solutions• Boost your own productivity by reducing

repetitive tedious code• Improve quality and consistency by

reducing repetitive error prone code

Page 3: Developer power tools

What We Will Cover

• Power Tools

• Sample Application

• Further Ideas

• Sources for More Information

Page 4: Developer power tools

Power Tools

• Regular expressions Take string manipulation to the next level

• Reflection Interrogate and manipulate code metadata at run time

• Code generation Make the computer write its own code

• Design patterns Leverage best practices in your designs

Page 5: Developer power tools

Overview - Reflection

• Create adaptive solutions

• Simplify code complexity

• Ease maintenance

• Ease deployment

• Solve configuration management issues

Page 6: Developer power tools

Overview Regular Expressions

• Regular expressions are to string as math is to numbers

• Simplify and optimize string manipulation

• Use editing tools to simplify creating regular expressions

• Document! Document! Document!

Page 7: Developer power tools

Overview Code Generation

• Obtain language independence with the CodeDom

• Use meta data to drive the code generation • Use meta data to provide code documentation• Standardize repetitive tasks with consistent

implementation

Page 8: Developer power tools

Overview Patterns

• Leverage emerging best practices

• Leverage existing documentation

• Don’t go over board

The KISS Principle still applies

Page 9: Developer power tools

Sample Problem

• Parse fixed length record file

• Parse with multiple purposes at different times (Load in a database, display on a document, display in a web page, etc)

• Need to parse multiple file with different formats

Page 10: Developer power tools

Sample Format

• Customer order tracking file

• Customer Record

• Order Record

• Order Item Record

• Address Record

Page 11: Developer power tools

Customer

• CST Record Identifer

• 23 Characters Customer ID

• 25 Characters First Name

• 25 Characters Last Name

• 3 Characters Age

Page 12: Developer power tools

Order Record

• ODR Record Identifier

• 8 Characters Order Date

• 8 Characters Required Date

• 8 Characters Shipped Date

Page 13: Developer power tools

Order Item Record

• ITM Record Identifier

• 25 Characters Product ID

• 5 Characters Quantity

• 5 Characters Unit Price

• 4 Characters Discount

Page 14: Developer power tools

Address Record

• ADR Record Identifier• 60 Characters Street Address• 75 Characters City• 2 Characters State• 5 Characters Zip Code• 4 Characters Zip Plus 4

Page 15: Developer power tools

Very Easy Regular Expression

“CST(?<CustomerID>.{23}(?<FirstName>.{25})(?<LastName>.{25})(?<Age>.{3})”

Page 16: Developer power tools

Regular Expression Notes

• Verify that the input string starts as expected

• Allow any character for the specified number of times

• Use named explicit capture groups

Page 17: Developer power tools

Defining Record Objects

• Define a Constructor expecting a string to parse

• Define read only strongly typed property for each element in the record

Page 18: Developer power tools

Building the Record Object

• Metadata storage

• Generation Process

• Provide documentation

Page 19: Developer power tools

Metadata

Document

PK DocumentID

DocumentName

Record

PK RecordID

FK1 DocumentIDRecordNameRecordPrefixComment

RecordElement

PK RecordElementID

FK1 RecordIDRecordElementNameDataTypeCharacterLengthSequenceComment

Page 20: Developer power tools

Generation Process

• CodeNamespace• CodeTypeDeclaration• CodeConstructor• CodeMemberProperty• CodeMemberMethod• CodeVariableDeclaration• CodeAssignmentStatement

Page 21: Developer power tools

Documentation Process

• Simple reports

• Documentation is accurate because it drives program execution

Page 22: Developer power tools

Reflection to Reduce Complexity

• One measure of complexity is the number of paths through the code

• Multiple paths may lead to redundant code• Multiple paths may lead to confusing flow control• Multiple paths may lead to higher maintenance

requirements

Page 23: Developer power tools

Identifying Record Objects (Hard Coded)

• Separate case statement for each record type• Brittle dependency on the file format• More complex formats lead to more complex

parsing• Parsing a different format requires a different

parser

Page 24: Developer power tools

private void HardCodedParseLine (string inputLine){

string prefix = inputLine.Substring (0,3);object parsedObject = null;switch (prefix){ case "CST": {

parsedObject = new Customer (inputLine);break;

} case "ODR": {

parsedObject = new Order (inputLine);break;

} . . .}DisplayObjectDetails (parsedObject, tvwResults);

}

Page 25: Developer power tools

Identifying Record Objects (Reflective)

• Single path through the code

• Code complexity stays constant even as file format complexity increases

• Same parser can handle parsing files with different formats

Page 26: Developer power tools

private void ReflectiveParseLine (string inputLine){

string prefix = inputLine.Substring (0,3);Type parseType = (Type)mTypes[prefix];Object [] parameters = {inputLine};if (parseType != null){

object parseObject = Activator.CreateInstance (parseType, parameters);Common.Helpers.DisplayObjectDetails

(parseObject, tvwResults);}

}

Page 27: Developer power tools

Visitor Pattern

• Create enhanced flexibility with events• Define an event that will be raised when a record

object is identified• Calling objects “visit” each discovered object by

subscribing to the event• Separation of responsibilities

– Parser knows how to identify record objects but not what to do with them

– Visitors know what to do with the record objects but not how to find them

Page 28: Developer power tools

Further Ideas

• Load the Record Object types through reflection or from a config file instead of hard coding them into hash table

• Use reflection to identify the method to be called from the event handler specific to each record object type

• Generate and compile the record objects at run time for the ultimate in flexibility

Page 29: Developer power tools

Sources for More Information

• Reflection– http://www.oreillynet.com/pub/au/1073

• Regular Expressions– http://www.ultrapico.com/Expresso.htm– http://weblogs.asp.net/rosherove/articles/6863.aspx

• Code Generation– http://www.ondotnet.com/pub/a/dotnet/2003/02/03/codedom

.html