Meta Programming

Post on 15-Dec-2014

86 views 1 download

Tags:

description

 

Transcript of Meta Programming

Mårten Rånge

@marten_range

Model driven MetaProgrammingUsing Visual Studio & T4

Let’s talk about code

DNRYDo Not Repeat Yourself

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)}

Repeating myself

AnException Exception

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)}

Repeating myself

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

void OnRaisePropertyChanged (string propertyName);}

Repeating myself

int MyProperty

Repeating myself

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

void OnRaisePropertyChanged (string propertyName);}

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

}

Repeating myself

double PreviewWidth

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

}

Deliver maximum business value for minimal cost

Code redundancy increase cost

Longer development timesInconsistent behaviorCostlier maintenance

Stops us from doing the right thing

Tools to reduce redundancy

LoopsMethodsPolymorphismGenericsHigher-order functions

Sometimes they don’t help

MetaProgramming

MetaProgramming is code that codes

T4 is MetaProgramming

Demo.001Getting started with T4

T4 – ASP/PHP for code

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

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)}

Exceptions revisited

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

Exceptions revisited

C# lacks constructor forwardingA weakness in the language

Demo.002Generating exception classes

Exceptions revisited

MetaProgramming can combat code redundancy caused by weaknesses in the language

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

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

Additional resources

Visual Studio Pro2008, 2010 or 2012

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

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

Best practicesThe rule of two

Separate model & template

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

Demo.003Generating Dependency Properties with T4

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

T4 – ASP/PHP for code

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

T4 saved meMetaProgramming for me

Saves timeImproves qualityPuts the fun back into (meta)programming

Mårten Rånge

@marten_range