meta programming

38
Mårten Rånge @marten_range

Upload: marten-range

Post on 15-Dec-2014

88 views

Category:

Technology


1 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Meta Programming

Mårten Rånge

@marten_range

Page 2: Meta Programming

Model driven MetaProgrammingUsing Visual Studio & T4

Page 3: Meta Programming

Let’s talk about code

Page 4: Meta Programming

DNRYDo Not Repeat Yourself

Page 5: Meta Programming

Repeating myself

[Serializable]public class AnException : Exception { public AnException (); public AnException (string message); public AnException (string message, Exception exc); protected AnException ( SerializationInfo info, StreamingContext context);

[SecurityPermission( SecurityAction.Demand, SerializationFormatter = true)] public override void GetObjectData ( SerializationInfo info, StreamingContext context)}

Page 6: Meta Programming

Repeating myself

AnException Exception

Page 7: Meta Programming

Repeating myself

[Serializable]public class AnException : Exception { public AnException (); public AnException (string message); public AnException (string message, Exception exc); protected AnException ( SerializationInfo info, StreamingContext context);

[SecurityPermission( SecurityAction.Demand, SerializationFormatter = true)] public override void GetObjectData ( SerializationInfo info, StreamingContext context)}

Page 8: Meta Programming

Repeating myself

public class MyViewModel : INotifyPropertyChanged{ public int MyProperty { get { return m_myProperty; } set { m_myProperty = value OnRaisePropertyChanged ("MyProperty") } }

void OnRaisePropertyChanged (string propertyName);}

Page 9: Meta Programming

Repeating myself

int MyProperty

Page 10: Meta Programming

Repeating myself

public class MyViewModel : INotifyPropertyChanged{ public int MyProperty { get { return m_myProperty; } set { m_myProperty = value OnRaisePropertyChanged ("MyProperty") } }

void OnRaisePropertyChanged (string propertyName);}

Page 11: Meta Programming

Repeating myself

public static readonly DependencyProperty PreviewWidthProperty = DependencyProperty.Register ( "PreviewWidth", typeof (double), typeof (AccordionPanel), new FrameworkPropertyMetadata (32.0));

public double PreviewWidth{

get { return (double)GetValue (PreviewWidthProperty); }

set { if (PreviewWidth != value)

SetValue (PreviewWidthProperty, value); }

}

Page 12: Meta Programming

Repeating myself

double PreviewWidth

Page 13: Meta Programming

Repeating myself

public static readonly DependencyProperty PreviewWidthProperty = DependencyProperty.Register ( "PreviewWidth", typeof (double), typeof (AccordionPanel), new FrameworkPropertyMetadata (32.0));

public double PreviewWidth{

get { return (double)GetValue (PreviewWidthProperty); }

set { if (PreviewWidth != value)

SetValue (PreviewWidthProperty, value); }

}

Page 14: Meta Programming

Deliver maximum business value for minimal cost

Page 15: Meta Programming

Code redundancy increase cost

Longer development timesInconsistent behaviorCostlier maintenance

Stops us from doing the right thing

Page 16: Meta Programming

Tools to reduce redundancy

LoopsMethodsPolymorphismGenericsHigher-order functions

Page 17: Meta Programming

Sometimes they don’t help

Page 18: Meta Programming

MetaProgramming

Page 19: Meta Programming

MetaProgramming is code that codes

Page 20: Meta Programming

T4 is MetaProgramming

Page 21: Meta Programming

Demo.001Getting started with T4

Page 22: Meta Programming

T4 – ASP/PHP for code

class X{<# for (var iter = 0; iter < 10; ++iter) { #> public int X<#=iter#> = <#=iter#>;<# } #>}

Page 23: Meta Programming

Exceptions revisited

[Serializable]public class AnException : Exception { public AnException (); public AnException (string message); public AnException (string message, Exception exc); protected AnException ( SerializationInfo info, StreamingContext context);

[SecurityPermission( SecurityAction.Demand, SerializationFormatter = true)] public override void GetObjectData ( SerializationInfo info, StreamingContext context)}

Page 24: Meta Programming

Exceptions revisited

Exception sample from“The CORRECT Way to Code a Custom Exception Class”bit.ly/10TzZAh@dseelinger

Page 25: Meta Programming

Exceptions revisited

C# lacks constructor forwardingA weakness in the language

Page 26: Meta Programming

Demo.002Generating exception classes

Page 27: Meta Programming

Exceptions revisited

MetaProgramming can combat code redundancy caused by weaknesses in the language

Page 28: Meta Programming

Autobiographical examples

MC68000 – Self modifying codeC/C++ - Higher order macrosC++ - Partial template specialization.NET – System.Reflection.Emit.NET – System.Linq.ExpressionsT-SQL – SELECT * FROM SYS.tables

Visual Studio – T4

Page 29: Meta Programming

Benefits of T4

Simple conceptASP/PHP for code

Easy to debugThe generated code can be debugged just as easy as hand written code

FlexibleT4 can generate any text artifact

C#VBC++

Doesn’t affect deployment

Page 30: Meta Programming

Additional resources

Visual Studio Pro2008, 2010 or 2012

T4 Addinhttp://t4-editor.tangible-engineering.com/

Good T4 Bloghttp://www.olegsych.com/

Page 31: Meta Programming

Best practicesThe rule of two

Page 32: Meta Programming

Separate model & template

Page 33: Meta Programming

Separate generated & crafted code

partial class X{ partial void Partial_Y (int q, ref int a); public int Y (int q) { var a = 0; Partial_Y(q, ref a); return a; }}

Page 34: Meta Programming

Demo.003Generating Dependency Properties with T4

Page 35: Meta Programming

DependencyProperties (WPF)

Controls requires DependencyPropertiesDependencyProperty pattern Redundant code

Becomes inconsistentError proneNaming conventionsTakes a long time to writeIs mind-numbingly boring to write

T4 can help

Page 36: Meta Programming

T4 – ASP/PHP for code

class X{<# for (var iter = 0; iter < 10; ++iter) { #> public int X<#=iter#> = <#=iter#>;<# } #>}

Page 37: Meta Programming

T4 saved meMetaProgramming for me

Saves timeImproves qualityPuts the fun back into (meta)programming

Page 38: Meta Programming

Mårten Rånge

@marten_range