Meta Programming

248 views
188 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

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

No notes for slide

Meta Programming

  1. 1. Mårten Rånge @marten_range
  2. 2. Model driven MetaProgramming Using Visual Studio & T4
  3. 3. Let’s talk about code
  4. 4. DNRY Do Not Repeat Yourself
  5. 5. 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) }
  6. 6. Repeating myself AnException Exception
  7. 7. 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) }
  8. 8. Repeating myself public class MyViewModel : INotifyPropertyChanged { public int MyProperty { get { return m_myProperty; } set { m_myProperty = value OnRaisePropertyChanged ("MyProperty") } } void OnRaisePropertyChanged (string propertyName); }
  9. 9. Repeating myself int MyProperty
  10. 10. Repeating myself public class MyViewModel : INotifyPropertyChanged { public int MyProperty { get { return m_myProperty; } set { m_myProperty = value OnRaisePropertyChanged ("MyProperty") } } void OnRaisePropertyChanged (string propertyName); }
  11. 11. 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); } }
  12. 12. Repeating myself double PreviewWidth
  13. 13. 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); } }
  14. 14. Deliver maximum business value for minimal cost
  15. 15. Code redundancy increase cost Longer development times Inconsistent behavior Costlier maintenance Stops us from doing the right thing
  16. 16. Tools to reduce redundancy Loops Methods Polymorphism Generics Higher-order functions
  17. 17. Sometimes they don’t help
  18. 18. MetaProgramming
  19. 19. MetaProgramming is code that codes
  20. 20. T4 is MetaProgramming
  21. 21. Demo.001 Getting started with T4
  22. 22. T4 – ASP/PHP for code class X { <# for (var iter = 0; iter < 10; ++iter) { #> public int X<#=iter#> = <#=iter#>; <# } #> }
  23. 23. 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) }
  24. 24. Exceptions revisited Exception sample from “The CORRECT Way to Code a Custom Exception Class” bit.ly/10TzZAh @dseelinger
  25. 25. Exceptions revisited C# lacks constructor forwarding A weakness in the language
  26. 26. Demo.002 Generating exception classes
  27. 27. Exceptions revisited MetaProgramming can combat code redundancy caused by weaknesses in the language
  28. 28. Autobiographical examples MC68000 – Self modifying code C/C++ - Higher order macros C++ - Partial template specialization .NET – System.Reflection.Emit .NET – System.Linq.Expressions T-SQL – SELECT * FROM SYS.tables Visual Studio – T4
  29. 29. Benefits of T4 Simple concept ASP/PHP for code Easy to debug The generated code can be debugged just as easy as hand written code Flexible T4 can generate any text artifact C# VB C++ Doesn’t affect deployment
  30. 30. Additional resources Visual Studio Pro 2008, 2010 or 2012 T4 Addin http://t4-editor.tangible-engineering.com/ Good T4 Blog http://www.olegsych.com/
  31. 31. Best practices The rule of two
  32. 32. Separate model & template
  33. 33. 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; } }
  34. 34. Demo.003 Generating Dependency Properties with T4
  35. 35. DependencyProperties (WPF) Controls requires DependencyProperties DependencyProperty pattern  Redundant code Becomes inconsistent Error prone Naming conventions Takes a long time to write Is mind-numbingly boring to write T4 can help
  36. 36. T4 – ASP/PHP for code class X { <# for (var iter = 0; iter < 10; ++iter) { #> public int X<#=iter#> = <#=iter#>; <# } #> }
  37. 37. T4 saved me MetaProgramming for me Saves time Improves quality Puts the fun back into (meta)programming
  38. 38. Mårten Rånge @marten_range

×