SlideShare a Scribd company logo
Pragmatic MetaProgramming
Mårten Rånge
SWETUGG 10:50
Mårten Rånge
@marten_range
Pragmatic MetaProgramming
DNRY
Do Not Repeat Yourself
Loops are great
x += 1;
x += 2;
x += 3;
x += 4;
x += 5;
x += 6;
x += 7;
x += 8;
x += 9;
x += 10;
Loops are great
for (var iter = 1; iter < 11; ++iter)
{
x += iter;
}
Functions are great
var z1 = t*(y1 – x1) + x1
var z2 = t*(y2 – x2) + x2
var z3 = t*(y3 – x3) + x3
Functions are great
double Lerp (this double t, double x, double y)
{
return t*(y – x) + x;
}
var z1 = t.Lerp (x1, y1);
var z2 = t.Lerp (x2, y2);
var z3 = t.Lerp (x3, y3);
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
exception AnException of int*string
Repeating myself
public class MyViewModel : INotifyPropertyChanged
{
int m_myProperty = 0;
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
{
int m_myProperty = 0;
public int MyProperty
{
get { return m_myProperty; }
set
{
m_myProperty = value
OnRaisePropertyChanged ("MyProperty")
}
}
void OnRaisePropertyChanged (string propertyName);
}
Repeating myself
class Customer
{
public long Id ;
public string FirstName;
public string LastName ;
}
public Customer ReadCustomer (IDataReader dr)
{
return new Customer
{
Id = dr.GetInt64 (0),
FirstName = dr.GetString (1),
LastName = dr.GetString (2),
};
}
Repeating myself
class Customer
{
public long Id ;
public string FirstName;
public string LastName ;
}
public Customer ReadCustomer (IDataReader dr)
{
return new Customer
{
Id = dr.GetInt64 (0),
FirstName = dr.GetString (1),
LastName = dr.GetString (2),
};
}
Repeating myself
Customer
long Id
string FirstName
string LastName
Repeating myself
class Customer
{
public long Id ;
public string FirstName;
public string LastName ;
}
public Customer ReadCustomer (IDataReader dr)
{
return new Customer
{
Id = dr.GetInt64 (0),
FirstName = dr.GetString (1),
LastName = dr.GetString (2),
};
}
Code duplication increases maintenance cost
Looking for answers
 Reflection
 LINQ Expression Trees
 Dynamic IL
Adding complexity
 Trading compile-time errors for run-time errors
 Hard to get enough test coverage
 Hard to understand
 No exit strategy
 Risky to change
 Limited
Pragmatic metaprogramming
public Customer ReadCustomer (IDataReader dr)
{
return new Customer
{
Id = dr.GetInt64 (0),
FirstName = dr.GetString (1),
LastName = dr.GetString (2),
};
}
Adding complexity
 Trading compile-time errors for run-time errors
 Hard to get enough test coverage
 Hard to understand
 No exit strategy
 Risky to change
 Limited
Complex code increases maintenance cost
Pragmatic metaprogramming
Pragmatic metaprogramming
Looking for answers
Powerful
Simple concept
Language agnostic
Exit strategy
Lightweight
T4
class Example
{
public int X0 = 0;
public int X1 = 1;
public int X2 = 2;
public int X3 = 3;
public int X4 = 4;
public int X5 = 5;
public int X6 = 6;
public int X7 = 7;
public int X8 = 8;
public int X9 = 9;
}
T4
class Example
{
<# for (var iter = 0; iter < 10; ++iter) { #>
public int X<#=iter#> = <#=iter#>;
<# } #>
}
T4
class Example
{
public int X0 = 0;
public int X1 = 1;
public int X2 = 2;
public int X3 = 3;
public int X4 = 4;
public int X5 = 5;
public int X6 = 6;
public int X7 = 7;
public int X8 = 8;
public int X9 = 9;
}
T4
Demo.001
Getting started with T4
Betty Holberton
Merge Sort Generator (1951)
Repeating myself
public class MyViewModel : INotifyPropertyChanged
{
int m_myProperty = 0;
public int MyProperty
{
get { return m_myProperty; }
set
{
m_myProperty = value
OnRaisePropertyChanged ("MyProperty")
}
}
void OnRaisePropertyChanged (string propertyName);
}
Repeating myself
public class MyViewModel : INotifyPropertyChanged
{
int m_myProperty = 0;
public int MyProperty
{
get { return m_myProperty; }
set
{
m_myProperty = value
OnRaisePropertyChanged ()
}
}
void OnRaisePropertyChanged ([CallerMemberName]string name = null);
}
Repeating myself
int MyProperty
Repeating myself
public class MyViewModel : INotifyPropertyChanged
{
int m_myProperty = 0;
public int MyProperty
{
get { return m_myProperty; }
set
{
m_myProperty = value
OnRaisePropertyChanged ("MyProperty")
}
}
void OnRaisePropertyChanged (string propertyName);
}
Demo.002
Generating view model classes
T4
Powerful
Simple concept
Language agnostic
Exit strategy
Lightweight
Carnelian
class Example
{
<# for (var iter = 0; iter < 10; ++iter) { #>
public int X<#=iter#> = <#=iter#>;
<# } #>
}
T4
class Example
{
@@> for iter in 0..10
public int X@@=iter=@@ = @@=iter=@@;
@@> end
}
Carnelian
Demo.003
Carnelian
Carnelian
Powerful
Simple concept
Language agnostic
Exit strategy
Lightweight
Start small
class Example
{
<# for (var iter = 0; iter < 10; ++iter) { #>
public int X<#=iter#> = <#=iter#>;
<# } #>
}
T4 is ASP/PHP for code
T4 saved me
Mårten Rånge
@marten_range
T4
T4 Addin
http://t4-editor.tangible-engineering.com/
T4 Blog
http://www.olegsych.com/
T4Include
http://github.com/mrange/t4Include/
”The CORRECT Way to Code a Custom Exception Class”
http://bit.ly/1AsJ0UH
Vad tyckte du om sessionen?
svara på vägen ut

More Related Content

PDF
The Ring programming language version 1.7 book - Part 35 of 196
PDF
Python Functions (PyAtl Beginners Night)
PDF
Functions in python
PDF
Python dictionary : past, present, future
PPTX
Refactoring
PDF
Python Puzzlers
PPTX
Metaprogramming in julia
PDF
Python Puzzlers - 2016 Edition
The Ring programming language version 1.7 book - Part 35 of 196
Python Functions (PyAtl Beginners Night)
Functions in python
Python dictionary : past, present, future
Refactoring
Python Puzzlers
Metaprogramming in julia
Python Puzzlers - 2016 Edition

What's hot (20)

PDF
Introduction to python
PPTX
Chap1 array
PPTX
FUNCTIONS IN PYTHON. CBSE +2 COMPUTER SCIENCE
PDF
FUNCTIONS IN PYTHON, CLASS 12 COMPUTER SCIENCE
PPTX
Introduction to julia
PDF
Python for Dummies
PPTX
PDF
Talk Code
PPTX
Introduction to Python and TensorFlow
PDF
Python Modules, Packages and Libraries
PPT
Chain Rule
PPTX
Unit 3
PDF
The Language for future-julia
PDF
Beginning Python
PDF
Declarative Thinking, Declarative Practice
PDF
c programming
PDF
Erlang assembly
PPTX
Python Modules and Libraries
PDF
Joose @jsconf
PDF
Java script introducation & basics
 
Introduction to python
Chap1 array
FUNCTIONS IN PYTHON. CBSE +2 COMPUTER SCIENCE
FUNCTIONS IN PYTHON, CLASS 12 COMPUTER SCIENCE
Introduction to julia
Python for Dummies
Talk Code
Introduction to Python and TensorFlow
Python Modules, Packages and Libraries
Chain Rule
Unit 3
The Language for future-julia
Beginning Python
Declarative Thinking, Declarative Practice
c programming
Erlang assembly
Python Modules and Libraries
Joose @jsconf
Java script introducation & basics
 
Ad

Similar to Pragmatic metaprogramming (20)

PPTX
New C# features
PDF
TypeScript Introduction
PDF
Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)
PPSX
Tuga IT 2017 - What's new in C# 7
PPT
Oop objects_classes
PPSX
C# 6.0 - April 2014 preview
PDF
Functional Programming You Already Know - Kevlin Henney - Codemotion Rome 2015
PDF
Haskell 101
PDF
TDC2016SP - Trilha Programação Funcional
PPT
شرح مقرر البرمجة 2 لغة جافا - الوحدة الرابعة
PDF
Clean code
PPTX
Chapter 7 functions (c)
PDF
Les nouveautés de C# 6
PPTX
Laziness, trampolines, monoids and other functional amenities: this is not yo...
PPTX
TDC2016POA | Trilha .NET - C# como você nunca viu: conceitos avançados de pro...
PDF
Keep getting a null pointer exception for some odd reasonim creati.pdf
PPSX
What's New In C# 7
PPTX
Lowering in C#: What really happens with your code?, from NDC Oslo 2019
PDF
Java VS Python
PPTX
F# Eye For The C# Guy - Seattle 2013
New C# features
TypeScript Introduction
Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)
Tuga IT 2017 - What's new in C# 7
Oop objects_classes
C# 6.0 - April 2014 preview
Functional Programming You Already Know - Kevlin Henney - Codemotion Rome 2015
Haskell 101
TDC2016SP - Trilha Programação Funcional
شرح مقرر البرمجة 2 لغة جافا - الوحدة الرابعة
Clean code
Chapter 7 functions (c)
Les nouveautés de C# 6
Laziness, trampolines, monoids and other functional amenities: this is not yo...
TDC2016POA | Trilha .NET - C# como você nunca viu: conceitos avançados de pro...
Keep getting a null pointer exception for some odd reasonim creati.pdf
What's New In C# 7
Lowering in C#: What really happens with your code?, from NDC Oslo 2019
Java VS Python
F# Eye For The C# Guy - Seattle 2013
Ad

More from Mårten Rånge (10)

PPTX
Know your FOSS obligations
PPTX
Ray Marching Explained
PPTX
Better performance through Superscalarity
PPTX
Property Based Tesing
PPTX
Monad - a functional design pattern
PPTX
Formlets
PPTX
Concurrency - responsiveness in .NET
PPTX
Meta Programming
PPTX
Concurrency scalability
PPTX
Concurrency
Know your FOSS obligations
Ray Marching Explained
Better performance through Superscalarity
Property Based Tesing
Monad - a functional design pattern
Formlets
Concurrency - responsiveness in .NET
Meta Programming
Concurrency scalability
Concurrency

Recently uploaded (20)

PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Empathic Computing: Creating Shared Understanding
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
Big Data Technologies - Introduction.pptx
PDF
cuic standard and advanced reporting.pdf
PDF
KodekX | Application Modernization Development
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
Machine learning based COVID-19 study performance prediction
PDF
Approach and Philosophy of On baking technology
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
Encapsulation theory and applications.pdf
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Encapsulation_ Review paper, used for researhc scholars
20250228 LYD VKU AI Blended-Learning.pptx
Empathic Computing: Creating Shared Understanding
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Dropbox Q2 2025 Financial Results & Investor Presentation
Spectral efficient network and resource selection model in 5G networks
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Big Data Technologies - Introduction.pptx
cuic standard and advanced reporting.pdf
KodekX | Application Modernization Development
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Machine learning based COVID-19 study performance prediction
Approach and Philosophy of On baking technology
Building Integrated photovoltaic BIPV_UPV.pdf
Mobile App Security Testing_ A Comprehensive Guide.pdf
Digital-Transformation-Roadmap-for-Companies.pptx
Advanced methodologies resolving dimensionality complications for autism neur...
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Encapsulation theory and applications.pdf
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy

Pragmatic metaprogramming