SlideShare a Scribd company logo
1 of 29
Correctness through simplicity Ulvi K. Guliyev
“…domain of dependable software engineering boils down less to money and more to ethics. Its about your duty as a software engineer to ensure that system is of as higher quality as possible.”  Prof. Joseph Kiniry (Software Development Group)
How do we think about computation?
∞ ∞ void InsertNutritionalSubstance(List<Dish> dishes, ImperativeGeek bob) { _burps = new List<Burp>(); foreach(vardish in dishes){ foreach(varfoodItemindish.Items){ if(foodItem.IsTasty(bob)){ Burpburp; switch(foodItem.Kind){ caseFoodKind.Yummy: bob.ConsumeItem(foodItem, out burp); break; 					caseFoodKind.Chewy: var timeout = TimeSpan.FromSeconds(10); bob.Chew(foodItem, timeout); break; default: throw new Exception(“Yuk!”); 			} if(burp != null) 			_burps.Add(burp);	 		} else{ Environment.Spit(foodItem, bob); } 	} } foreach(var burp in _burps){ Environment.Release(burp); } } Branch Loop Loop Branch Side-effect Branch Branch Jump Branch Side-effect Branch Side-effect Meet accidental complexity Meet imperative Bob Loop Side-effect
How do we change thinking modality? We are computers Technology is secondary Intuition and ingenuity Structured roadmap
Roadmap to dependable software Targeted at imperative programmers Focus on reducing code complexity Step by step discovery Meant to alter the way we reason about computation
Step 1. Procedural decomposition
void InsertNutritionalSubstance(List<Dish> dishes, ImperativeGeek bob) { _burps= new List<Burp>(); foreach(vardish in dishes){ foreach(varfoodItemindish.Items){ if(foodItem.IsTasty(bob)){ Burpburp; switch(foodItem.Kind){ caseFoodKind.Yummy: bob.ConsumeItem(foodItem, out burp); break; 					caseFoodKind.Chewy: var timeout = TimeSpan.FromSeconds(10); bob.Chew(foodItem, timeout); break; default: throw new Exception(“Yuk!”); 				} if(burp != null) _burps.Add(burp);	 			} else Environment.Spit(foodItem, bob); 		} 	} foreach(var burp in_burps){ Environment.Release(burp); 	} } Operational demarcation
void InsertNutritionalSubstance(List<Dish> dishes, ImperativeGeek bob) { _burps= new List<Burp>(); foreach(vardish in dishes){ foreach(varfoodItemindish.Items){ if(foodItem.IsTasty(bob)){ Burpburp; switch(foodItem.Kind){ caseFoodKind.Yummy: bob.ConsumeItem(foodItem, out burp); break; 					caseFoodKind.Chewy: var timeout = TimeSpan.FromSeconds(10); bob.Chew(foodItem, timeout); break; default: throw new Exception(“Yuk!”); 				} if(burp != null) burps.Add(burp);	 			} else Environment.Spit(foodItem, bob); 		} } Burp(); } void Burp() { foreach(var burp in_burps){ Environment.Release(burp); 	} }
Step 2. Single responsibility
void InsertNutritionalSubstance(List<Dish> dishes, ImperativeGeek bob) { _burps= new List<Burp>(); foreach(vardish in dishes){ foreach(varfoodItemindish.Items){ if(foodItem.IsTasty(bob)){ Burpburp; switch(foodItem.Kind){ caseFoodKind.Yummy: bob.ConsumeItem(foodItem, out burp); break; 					caseFoodKind.Chewy: var timeout = TimeSpan.FromSeconds(10); bob.Chew(foodItem, timeout); break; default: throw new Exception(“Yuk!”); 				} if(burp != null) _burps.Add(burp);	 			} else{ Environment.Spit(foodItem, bob); } 		} } } void Burp() { foreach(var burp in_burps){ Environment.Release(burp); 	} } Responsibility demarcation
void InsertNutritionalSubstance(List<Dish> dishes, ImperativeGeek bob) { _burps= new List<Burp>(); foreach(vardish in dishes){ foreach(varfoodItemindish.Items){ if(foodItem.IsTasty(bob)){ Burpburp = ProcessFoodItem(foodItem, bob); if(burp != null) _burps.Add(burp);	 else Environment.Spit(foodItem, bob); 		} } } 	Burp(); } Burp ProcessFoodItem(FoodItemfoodItem, ImperativeGeekbob) { 	Burpburp = null; 	switch(foodItem.Kind) { caseFoodKind.Yummy: bob.ConsumeItem(foodItem, out burp); break; caseFoodKind.Chewy: var timeout = TimeSpan.FromSeconds(10); bob.Chew(foodItem, timeout); break; default: throw new Exception(“Yuk!”); } return burp;  }
Step 3. Semantic regression
Code beacons Patterns in code Sequences of imperative instructions Describe how to compute and not computation itself Denote hidden semantic intent
void InsertNutritionalSubstance(List<Dish> dishes, ImperativeGeek bob) { _burps= new List<Burp>(); foreach(vardish in dishes){ foreach(varfoodItemindish.Items){ if(foodItem.IsTasty(bob)){ Burpburp = ProcessFoodItem(foodItem, bob); if(burp != null) _burps.Add(burp);	 else Environment.Spit(foodItem, bob); 		} } } Burp(); } void Burp() { foreach(var burp in_burps){ Environment.Release(burp); 	} } Beacon Beacon Beacon
void InsertNutritionalSubstance(List<Dish> dishes, ImperativeGeek bob) { 	_burps = ProcessFood(dishes, bob); Burp(); } List<Burp>ProcessFood(List<Dish> dishes, ImperativeGeek bob) { return dishes.SelectMany(d => d.Items)             .Where(i => i.IsTasty(bob))             .Select(i => ProcessFoodItem(i, bob));  } void Burp() { foreach(var burp in _burps){ Environment.Release(burp); 	} }
Step 4. Controlled side-effects
Mutable type void InsertNutritionalSubstance(List<Dish> dishes, ImperativeGeek bob) { 	_burps = ProcessFood(dishes, bob); 	Burp(); } List<Burp>ProcessFood(List<Dish> dishes, ImperativeGeek bob) { return dishes.SelectMany(d => d.Items)             .Where(i => i.IsTasty(bob))             .Select(i => ProcessFoodItem(i, bob));  } void Burp() { foreach(var burp in_burps){ Environment.Release(burp); 	} } Mutable type Mutable type
void InsertNutritionalSubstance(IEnumerable<Dish> dishes, ImperativeGeek bob) { 	_burps = ProcessFood(dishes, bob); 	Burp(); } IEnumerable<Burp>ProcessFood(IEnumerable<Dish> dishes, ImperativeGeek bob) { return dishes.SelectMany(d => d.Items)             .Where(i => i.IsTasty(bob))             .Select(i => ProcessFoodItem(i, bob));  } void Burp() { foreach(var burp in_burps){ Environment.Release(burp); 	} }
Step 6. Functional composition
Non-compositional void InsertNutritionalSubstance(IEnumerable<Dish> dishes, ImperativeGeek bob) { 	_burps = ProcessFood(dishes, bob); 	Burp(); } IEnumerable<Burp>ProcessFood(IEnumerable<Dish> dishes, ImperativeGeek bob) { return dishes.SelectMany(d => d.Items)             .Where(i => i.IsTasty(bob))             .Select(i => ProcessFoodItem(i, bob));  } void Burp() { foreach(var burp in_burps){ Environment.Release(burp); 	} }
void InsertNutritionalSubstance(IEnumerable<Dish> dishes, ImperativeGeek bob) { Burp(ProcessFood(dishes, bob)); } IEnumerable<Burp>ProcessFood(IEnumerable<Dish> dishes, ImperativeGeek bob) { return dishes.SelectMany(d => d.Items)             .Where(i => i.IsTasty(bob))             .Select(i => ProcessFoodItem(i, bob));  } void Burp(IEnumerable<Burp> burps) { foreach(var burp inburps){ Environment.Release(burp); 	} } Composition
Step 7. Computational abstraction
IEnumerable<Burp>ProcessFood(IEnumerable<Dish> dishes, ImperativeGeek bob) { return dishes.SelectMany(d => d.Items)             .Where(i => i.IsTasty(bob))             .Select(i => ProcessFoodItem(i, bob));  } Burp ProcessFoodItem(FoodItemfoodItem, ImperativeGeek bob) { 	Burpburp = null; 	switch(foodItem.Kind) 	{ caseFoodKind.Yummy: bob.ConsumeItem(foodItem, out burp); break; 		caseFoodKind.Chewy: var timeout = TimeSpan.FromSeconds(10); bob.Chew(foodItem, timeout); break; default: throw new Exception(“Yuk!”); 	} return burp;  } void Burp(IEnumerable<Burp> burps) { foreach(var burp inburps){ Environment.Release(burp); 	} } Could be null
IEnumerable<Maybe<Burp>>ProcessFood(IEnumerable<Dish> dishes, ImperativeGeek bob) { return dishes.SelectMany(d => d.Items)             .Where(i => i.IsTasty(bob))             .Select(i => ProcessFoodItem(i, bob));  } Maybe<Burp>ProcessFoodItem(FoodItemfoodItem, ImperativeGeek bob) { 	Burpburp = null; 	switch(foodItem.Kind) 	{ caseFoodKind.Yummy: bob.ConsumeItem(foodItem, out burp); break; 		caseFoodKind.Chewy: var timeout = TimeSpan.FromSeconds(10); bob.Chew(foodItem, timeout); break; default: throw new Exception(“Yuk!”); 	} return burp != null ? burp.ToMaybe() : Maybe<Burp>.Nothing;  } void Burp(IEnumerable<<Maybe<Burp>> burps) { foreach(var burp inburps.Where(b => b.HasValue)){ Environment.Release(burp); 	} }
Step 8. Constrained state space
∞ ∞ IEnumerable<Maybe<Burp>>ProcessFood(IEnumerable<Dish> dishes, ImperativeGeek bob) { return dishes.SelectMany(d => d.Items)             .Where(i => i.IsTasty(bob))             .Select(i => ProcessFoodItem(i, bob));  } Maybe<Burp>ProcessFoodItem(FoodItemfoodItem, ImperativeGeek bob) { 	Burpburp = null; 	switch(foodItem.Kind) 	{ caseFoodKind.Yummy: bob.ConsumeItem(foodItem, out burp); break; 		caseFoodKind.Chewy: var timeout = TimeSpan.FromSeconds(10); bob.Chew(foodItem, timeout); break; default: throw new Exception(“Yuk!”); 	} return burp != null ? burp.ToMaybe() : Maybe<Burp>.Nothing;  } ∞ ∞
IEnumerable<Maybe<Burp>>ProcessFood(IEnumerable<Dish> dishes, ImperativeGeek bob) { Contract.Requires(dishes.Any()); Contract.Requires(bob.IsHungry); Contract.Ensures(bob.IsFull); return dishes.SelectMany(d => d.Items)             .Where(i => i.IsTasty(bob))             .Select(i => ProcessFoodItem(i, bob));  } Maybe<Burp>ProcessFoodItem(FoodItemfoodItem, ImperativeGeek bob) { Contract.Requires(foodItem.ExpiryDate < DateTime.Now); Contract.Requires(foodItem.IsWarm); Contract.Requires(bob.IsHungry); 	Burpburp = null; 	switch(foodItem.Kind) 	{ caseFoodKind.Yummy: bob.ConsumeItem(foodItem, out burp); break; 		caseFoodKind.Chewy: var timeout = TimeSpan.FromSeconds(10); bob.Chew(foodItem, timeout); break; default: throw new Exception(“Yuk!”); 	} return burp != null ? burp.ToMaybe() : Maybe<Burp>.Nothing;  }

More Related Content

Featured

Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 

Featured (20)

Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 

Correctness through simplicity

  • 2. “…domain of dependable software engineering boils down less to money and more to ethics. Its about your duty as a software engineer to ensure that system is of as higher quality as possible.” Prof. Joseph Kiniry (Software Development Group)
  • 3. How do we think about computation?
  • 4. ∞ ∞ void InsertNutritionalSubstance(List<Dish> dishes, ImperativeGeek bob) { _burps = new List<Burp>(); foreach(vardish in dishes){ foreach(varfoodItemindish.Items){ if(foodItem.IsTasty(bob)){ Burpburp; switch(foodItem.Kind){ caseFoodKind.Yummy: bob.ConsumeItem(foodItem, out burp); break; caseFoodKind.Chewy: var timeout = TimeSpan.FromSeconds(10); bob.Chew(foodItem, timeout); break; default: throw new Exception(“Yuk!”); } if(burp != null) _burps.Add(burp); } else{ Environment.Spit(foodItem, bob); } } } foreach(var burp in _burps){ Environment.Release(burp); } } Branch Loop Loop Branch Side-effect Branch Branch Jump Branch Side-effect Branch Side-effect Meet accidental complexity Meet imperative Bob Loop Side-effect
  • 5. How do we change thinking modality? We are computers Technology is secondary Intuition and ingenuity Structured roadmap
  • 6. Roadmap to dependable software Targeted at imperative programmers Focus on reducing code complexity Step by step discovery Meant to alter the way we reason about computation
  • 7.
  • 8. Step 1. Procedural decomposition
  • 9. void InsertNutritionalSubstance(List<Dish> dishes, ImperativeGeek bob) { _burps= new List<Burp>(); foreach(vardish in dishes){ foreach(varfoodItemindish.Items){ if(foodItem.IsTasty(bob)){ Burpburp; switch(foodItem.Kind){ caseFoodKind.Yummy: bob.ConsumeItem(foodItem, out burp); break; caseFoodKind.Chewy: var timeout = TimeSpan.FromSeconds(10); bob.Chew(foodItem, timeout); break; default: throw new Exception(“Yuk!”); } if(burp != null) _burps.Add(burp); } else Environment.Spit(foodItem, bob); } } foreach(var burp in_burps){ Environment.Release(burp); } } Operational demarcation
  • 10. void InsertNutritionalSubstance(List<Dish> dishes, ImperativeGeek bob) { _burps= new List<Burp>(); foreach(vardish in dishes){ foreach(varfoodItemindish.Items){ if(foodItem.IsTasty(bob)){ Burpburp; switch(foodItem.Kind){ caseFoodKind.Yummy: bob.ConsumeItem(foodItem, out burp); break; caseFoodKind.Chewy: var timeout = TimeSpan.FromSeconds(10); bob.Chew(foodItem, timeout); break; default: throw new Exception(“Yuk!”); } if(burp != null) burps.Add(burp); } else Environment.Spit(foodItem, bob); } } Burp(); } void Burp() { foreach(var burp in_burps){ Environment.Release(burp); } }
  • 11. Step 2. Single responsibility
  • 12. void InsertNutritionalSubstance(List<Dish> dishes, ImperativeGeek bob) { _burps= new List<Burp>(); foreach(vardish in dishes){ foreach(varfoodItemindish.Items){ if(foodItem.IsTasty(bob)){ Burpburp; switch(foodItem.Kind){ caseFoodKind.Yummy: bob.ConsumeItem(foodItem, out burp); break; caseFoodKind.Chewy: var timeout = TimeSpan.FromSeconds(10); bob.Chew(foodItem, timeout); break; default: throw new Exception(“Yuk!”); } if(burp != null) _burps.Add(burp); } else{ Environment.Spit(foodItem, bob); } } } } void Burp() { foreach(var burp in_burps){ Environment.Release(burp); } } Responsibility demarcation
  • 13. void InsertNutritionalSubstance(List<Dish> dishes, ImperativeGeek bob) { _burps= new List<Burp>(); foreach(vardish in dishes){ foreach(varfoodItemindish.Items){ if(foodItem.IsTasty(bob)){ Burpburp = ProcessFoodItem(foodItem, bob); if(burp != null) _burps.Add(burp); else Environment.Spit(foodItem, bob); } } } Burp(); } Burp ProcessFoodItem(FoodItemfoodItem, ImperativeGeekbob) { Burpburp = null; switch(foodItem.Kind) { caseFoodKind.Yummy: bob.ConsumeItem(foodItem, out burp); break; caseFoodKind.Chewy: var timeout = TimeSpan.FromSeconds(10); bob.Chew(foodItem, timeout); break; default: throw new Exception(“Yuk!”); } return burp; }
  • 14. Step 3. Semantic regression
  • 15. Code beacons Patterns in code Sequences of imperative instructions Describe how to compute and not computation itself Denote hidden semantic intent
  • 16. void InsertNutritionalSubstance(List<Dish> dishes, ImperativeGeek bob) { _burps= new List<Burp>(); foreach(vardish in dishes){ foreach(varfoodItemindish.Items){ if(foodItem.IsTasty(bob)){ Burpburp = ProcessFoodItem(foodItem, bob); if(burp != null) _burps.Add(burp); else Environment.Spit(foodItem, bob); } } } Burp(); } void Burp() { foreach(var burp in_burps){ Environment.Release(burp); } } Beacon Beacon Beacon
  • 17. void InsertNutritionalSubstance(List<Dish> dishes, ImperativeGeek bob) { _burps = ProcessFood(dishes, bob); Burp(); } List<Burp>ProcessFood(List<Dish> dishes, ImperativeGeek bob) { return dishes.SelectMany(d => d.Items) .Where(i => i.IsTasty(bob)) .Select(i => ProcessFoodItem(i, bob)); } void Burp() { foreach(var burp in _burps){ Environment.Release(burp); } }
  • 18. Step 4. Controlled side-effects
  • 19. Mutable type void InsertNutritionalSubstance(List<Dish> dishes, ImperativeGeek bob) { _burps = ProcessFood(dishes, bob); Burp(); } List<Burp>ProcessFood(List<Dish> dishes, ImperativeGeek bob) { return dishes.SelectMany(d => d.Items) .Where(i => i.IsTasty(bob)) .Select(i => ProcessFoodItem(i, bob)); } void Burp() { foreach(var burp in_burps){ Environment.Release(burp); } } Mutable type Mutable type
  • 20. void InsertNutritionalSubstance(IEnumerable<Dish> dishes, ImperativeGeek bob) { _burps = ProcessFood(dishes, bob); Burp(); } IEnumerable<Burp>ProcessFood(IEnumerable<Dish> dishes, ImperativeGeek bob) { return dishes.SelectMany(d => d.Items) .Where(i => i.IsTasty(bob)) .Select(i => ProcessFoodItem(i, bob)); } void Burp() { foreach(var burp in_burps){ Environment.Release(burp); } }
  • 21. Step 6. Functional composition
  • 22. Non-compositional void InsertNutritionalSubstance(IEnumerable<Dish> dishes, ImperativeGeek bob) { _burps = ProcessFood(dishes, bob); Burp(); } IEnumerable<Burp>ProcessFood(IEnumerable<Dish> dishes, ImperativeGeek bob) { return dishes.SelectMany(d => d.Items) .Where(i => i.IsTasty(bob)) .Select(i => ProcessFoodItem(i, bob)); } void Burp() { foreach(var burp in_burps){ Environment.Release(burp); } }
  • 23. void InsertNutritionalSubstance(IEnumerable<Dish> dishes, ImperativeGeek bob) { Burp(ProcessFood(dishes, bob)); } IEnumerable<Burp>ProcessFood(IEnumerable<Dish> dishes, ImperativeGeek bob) { return dishes.SelectMany(d => d.Items) .Where(i => i.IsTasty(bob)) .Select(i => ProcessFoodItem(i, bob)); } void Burp(IEnumerable<Burp> burps) { foreach(var burp inburps){ Environment.Release(burp); } } Composition
  • 24. Step 7. Computational abstraction
  • 25. IEnumerable<Burp>ProcessFood(IEnumerable<Dish> dishes, ImperativeGeek bob) { return dishes.SelectMany(d => d.Items) .Where(i => i.IsTasty(bob)) .Select(i => ProcessFoodItem(i, bob)); } Burp ProcessFoodItem(FoodItemfoodItem, ImperativeGeek bob) { Burpburp = null; switch(foodItem.Kind) { caseFoodKind.Yummy: bob.ConsumeItem(foodItem, out burp); break; caseFoodKind.Chewy: var timeout = TimeSpan.FromSeconds(10); bob.Chew(foodItem, timeout); break; default: throw new Exception(“Yuk!”); } return burp; } void Burp(IEnumerable<Burp> burps) { foreach(var burp inburps){ Environment.Release(burp); } } Could be null
  • 26. IEnumerable<Maybe<Burp>>ProcessFood(IEnumerable<Dish> dishes, ImperativeGeek bob) { return dishes.SelectMany(d => d.Items) .Where(i => i.IsTasty(bob)) .Select(i => ProcessFoodItem(i, bob)); } Maybe<Burp>ProcessFoodItem(FoodItemfoodItem, ImperativeGeek bob) { Burpburp = null; switch(foodItem.Kind) { caseFoodKind.Yummy: bob.ConsumeItem(foodItem, out burp); break; caseFoodKind.Chewy: var timeout = TimeSpan.FromSeconds(10); bob.Chew(foodItem, timeout); break; default: throw new Exception(“Yuk!”); } return burp != null ? burp.ToMaybe() : Maybe<Burp>.Nothing; } void Burp(IEnumerable<<Maybe<Burp>> burps) { foreach(var burp inburps.Where(b => b.HasValue)){ Environment.Release(burp); } }
  • 27. Step 8. Constrained state space
  • 28. ∞ ∞ IEnumerable<Maybe<Burp>>ProcessFood(IEnumerable<Dish> dishes, ImperativeGeek bob) { return dishes.SelectMany(d => d.Items) .Where(i => i.IsTasty(bob)) .Select(i => ProcessFoodItem(i, bob)); } Maybe<Burp>ProcessFoodItem(FoodItemfoodItem, ImperativeGeek bob) { Burpburp = null; switch(foodItem.Kind) { caseFoodKind.Yummy: bob.ConsumeItem(foodItem, out burp); break; caseFoodKind.Chewy: var timeout = TimeSpan.FromSeconds(10); bob.Chew(foodItem, timeout); break; default: throw new Exception(“Yuk!”); } return burp != null ? burp.ToMaybe() : Maybe<Burp>.Nothing; } ∞ ∞
  • 29. IEnumerable<Maybe<Burp>>ProcessFood(IEnumerable<Dish> dishes, ImperativeGeek bob) { Contract.Requires(dishes.Any()); Contract.Requires(bob.IsHungry); Contract.Ensures(bob.IsFull); return dishes.SelectMany(d => d.Items) .Where(i => i.IsTasty(bob)) .Select(i => ProcessFoodItem(i, bob)); } Maybe<Burp>ProcessFoodItem(FoodItemfoodItem, ImperativeGeek bob) { Contract.Requires(foodItem.ExpiryDate < DateTime.Now); Contract.Requires(foodItem.IsWarm); Contract.Requires(bob.IsHungry); Burpburp = null; switch(foodItem.Kind) { caseFoodKind.Yummy: bob.ConsumeItem(foodItem, out burp); break; caseFoodKind.Chewy: var timeout = TimeSpan.FromSeconds(10); bob.Chew(foodItem, timeout); break; default: throw new Exception(“Yuk!”); } return burp != null ? burp.ToMaybe() : Maybe<Burp>.Nothing; }