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

What's hot (20)

PDF
Introduction to python
Marian Marinov
 
PPTX
Chap1 array
raksharao
 
PPTX
FUNCTIONS IN PYTHON. CBSE +2 COMPUTER SCIENCE
Venugopalavarma Raja
 
PDF
FUNCTIONS IN PYTHON, CLASS 12 COMPUTER SCIENCE
Venugopalavarma Raja
 
PPTX
Introduction to julia
岳華 杜
 
PDF
Python for Dummies
Leonardo Jimenez
 
PPTX
Python
Sameeksha Verma
 
PDF
Talk Code
Agiliq Solutions
 
PPTX
Introduction to Python and TensorFlow
Bayu Aldi Yansyah
 
PDF
Python Modules, Packages and Libraries
Venugopalavarma Raja
 
PPT
Chain Rule
calculusgroup3
 
PPTX
Unit 3
GOWSIKRAJAP
 
PDF
The Language for future-julia
岳華 杜
 
PDF
Beginning Python
Agiliq Solutions
 
PDF
Declarative Thinking, Declarative Practice
Kevlin Henney
 
PDF
c programming
Arun Umrao
 
PDF
Erlang assembly
rstudnicki
 
PPTX
Python Modules and Libraries
Venugopalavarma Raja
 
PDF
Joose @jsconf
malteubl
 
PDF
Java script introducation & basics
H K
 
Introduction to python
Marian Marinov
 
Chap1 array
raksharao
 
FUNCTIONS IN PYTHON. CBSE +2 COMPUTER SCIENCE
Venugopalavarma Raja
 
FUNCTIONS IN PYTHON, CLASS 12 COMPUTER SCIENCE
Venugopalavarma Raja
 
Introduction to julia
岳華 杜
 
Python for Dummies
Leonardo Jimenez
 
Talk Code
Agiliq Solutions
 
Introduction to Python and TensorFlow
Bayu Aldi Yansyah
 
Python Modules, Packages and Libraries
Venugopalavarma Raja
 
Chain Rule
calculusgroup3
 
Unit 3
GOWSIKRAJAP
 
The Language for future-julia
岳華 杜
 
Beginning Python
Agiliq Solutions
 
Declarative Thinking, Declarative Practice
Kevlin Henney
 
c programming
Arun Umrao
 
Erlang assembly
rstudnicki
 
Python Modules and Libraries
Venugopalavarma Raja
 
Joose @jsconf
malteubl
 
Java script introducation & basics
H K
 

Similar to Pragmatic metaprogramming (20)

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

More from Mårten Rånge (10)

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

Recently uploaded (20)

PDF
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
PDF
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PDF
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
PDF
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PPTX
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
PDF
What Makes Contify’s News API Stand Out: Key Features at a Glance
Contify
 
PDF
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
PDF
From Code to Challenge: Crafting Skill-Based Games That Engage and Reward
aiyshauae
 
PDF
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
PDF
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
PDF
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
PPTX
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
PDF
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
PDF
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
PPTX
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
PPTX
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
PDF
HubSpot Main Hub: A Unified Growth Platform
Jaswinder Singh
 
PDF
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
What Makes Contify’s News API Stand Out: Key Features at a Glance
Contify
 
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
From Code to Challenge: Crafting Skill-Based Games That Engage and Reward
aiyshauae
 
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
HubSpot Main Hub: A Unified Growth Platform
Jaswinder Singh
 
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 

Pragmatic metaprogramming