SlideShare a Scribd company logo
1 of 64
How Functional Programming
Made Me A Better Developer
Cameron Presley
@PCameronPresley
http://blog.TheSoftwareMentor.com
Thank The Sponsors!
Little About Me
 From Knoxville, TN
 Software Engineer at Pilot Flying J
 Musician, board gamer, and mentor
 Co-organizer of FunctionalKnox
Outline
 The Journey of Learning Functional Programming
 How My Style Has Evolved
 How I Think About Software
 Resources
The Journey
Link
Some SOLID Inspiration
 Mnemonic for software design coined by Robert C. Martin
 By following SOLID, we tend to write more maintainable code
 Teach and present on SOLID a lot
 Want to focus on the Single Responsibility Principle
Some SOLID Inspiration
“The single responsibility principle states that every module or class should have
responsibility over a single part of the functionality provided by the software, and that
responsibility should be entirely encapsulated by the class.”
-Wikipedia
Some SOLID Interpretation
What happens if you follow Single Responsibility to the extreme?
Lots of small classes with a single method
Felt like a lot of ceremony
Asking the Twitterverse
Twitter Responds!
Seems Like I Wasn’t The First With This
Idea…
http://blog.ploeh.dk/2014/03/10/solid-the-next-step-is-functional/
Mark’s Thoughts
 Extreme SRP and ISP, leads to a lot of fine grain classes
 An object can be thought of data with behavior
 What if we flipped that concept around?
 Behavior with data
 Sounds like a functional approach to the problem
Time to Learn Functional
 Chose F#
 Familiar with the .NET framework
 Comfortable with the .NET tooling
 Community resources
Steps Along The Journey
Link
First Attempt
Premise
 Decided to port a C# application (Task Snapper)
 Grabs a screenshot every so often
 Focus on the language, not the problem
 Dipping my toes into FP
Implementation
Reflection
 It’s F#!
 Wrote object-oriented in a functional language
 Definitely not idiomatic
 Didn’t really see the point
 Need to learn more about functional
Second Attempt
Premise
 Found out that card games tend to be a great exercise
 Want to use Love Letter as an example card game
 8 different kinds of cards, each with their own special rules
 Write more idiomatic functional
 No classes, no exceptions
Implementation
Reflection
 No exceptions!
 Learning more about data modeling
 Making illegal state unrepresentable
 Game rules a bit more complicated than expected
 Spending a lot of time learning the problem, not solving the problem
Third Attempt
Premise
 Modeling a card game still makes sense
 Something with easier business rules
 Able to solve a real problem without getting lost
 Decided on Blackjack
 Just enough business rules to do something
Making Illegal States Unrepresentable
Business Rules for How Many Points
a Card Is Worth
Business Rules For Adding Points Together
Calculating How Many Points For
A Hand
Reflection
 Comfortable with handling errors without exceptions
 Using pattern matching more effectively
 Working with built-in List operators
 Map and Reduce
 Starting to think about how to use these concepts in C#
At the End
 Worked on/off over the course of the year
 Got frustrated at times
 Spent time learning, asking questions on Twitter, StackOverflow
 Didn’t give up
 Starting to think about these concepts in OOP
Evolution of Style
Link
Writing Testable Code
Why Write Testable Code?
 If code is easy to test, it’s usually easy to maintain
 Implies that hard to test code may not be maintainable
 What defines easy to test code?
A Hypothetical
Pretend we were writing tests around a particular
method. Which one would you rather test?
A) A method that when given the same two inputs, it returns the same output
B) A method that when given the same two inputs, it returns different outputs
Thinking About Testability
 If a method always returns output solely based on inputs, we call that method
pure.
 No side effects or state involved
 Easiest types of test to write
 No mocking, no fake implementations to use
 Frameworks that use this concept
 Reducers (Redux)
 Elm architecture
Improved LINQ
Typical Approach
 When working with a list of data, I’d typically work with a for each loop.
A Different Approach
 In functional programming, typically work with multiples of data as lists
 Typically use built-in list functions (map, filter)
Leveraging LINQ
 Instead of Map and Filter, LINQ uses Select and Where
Useful Interfaces
How I Used Interfaces
What Is an Interface
 It’s a guarantee that a method with that signature exists
 Essentially a contract
 If the contract is not fulfilled, the compiler tells you
 Allows us to use the compiler to find errors
 “Leaning on the compiler”
Pop Quiz!
Given this interface, what should GetById return?
Anything that implements this interface
will always return a Record object, right?
How about this implementation?
Interfaces That Don’t Lie
 Interface says that a Record will always be returned
 If I handle the Record, then the compiler is happy
 However, the compiler can’t enforce that I handled the null condition
 Likely for me to introduce bugs into the system
The Functional Approach
 No concept of null, typically use something called Option or Maybe
 Has two values, either Some<‘a> or None
The Functional Approach
 If a function returns an Option, callers must deal with it.
 Otherwise, code fails to compile
How My Style Evolved
 Using purity to make business rules testable
 Reducing complexity by leveraging LINQ
 Think more about types and interfaces
How I Think About Software
Types of Components
 Boundary
 How data comes in or goes out of the system
 Business Rule
 How to work with data for our business needs
 Workflows
 Combines both boundary and business rule components
Boundary Components
 Anything that deals with getting data in/out of a system is a boundary
 Examples include databases, servers, file system, user input, etc.
 Makes sense to have these components implement an interface for testability
 Inspired by Hexagonal Architecture by Alistair Cockburn
Getting Data In
Getting Data Out
Business Rule Components
 Contain the rules for our application
 Will make up the bulk of your application
 Remember SOLID principles while implementing
 For testability, purity is key
Business Rules
Workflow Components
 Combines both Boundary and Business Rule components
 Can be described as
 Data comes in through the boundary
 Data is processed by the business rules
 Data goes out through the boundary
Bringing It All Together
Bringing It All Together
Bringing It All Together
Bringing It All Together
Wrapping Up
 Learning functional was hard for me
 Lots of failures, but improved incrementally
 Made me re-evaluate how I build software
 Still use functional concepts in my coding
 Even if I’m not using a functional language
Resources
 F Sharp for Fun and Profit (https://fsharpforfunandprofit.com)
 The Book of F#: Breaking Free with Managed Functional Programming by
Dave Fancher
 F# Jumpstart (Pluralsight Course) by Kit Eason
 Mark Seemann’s blog: (http://blog.ploeh.dk)
 Hexagonal Architecture (post) by Alistair Cockburn

More Related Content

Viewers also liked

Marshall hm poster_vra2015
Marshall hm poster_vra2015Marshall hm poster_vra2015
Marshall hm poster_vra2015
Hannah Marshall
 
IL MOBBING ed il BURN OUT
IL MOBBING ed il BURN OUTIL MOBBING ed il BURN OUT
IL MOBBING ed il BURN OUT
Drughe .it
 

Viewers also liked (17)

Boletín 30/03/2017
Boletín 30/03/2017Boletín 30/03/2017
Boletín 30/03/2017
 
Marshall hm poster_vra2015
Marshall hm poster_vra2015Marshall hm poster_vra2015
Marshall hm poster_vra2015
 
Recull de premsa #Figueres - 30/03/2017
Recull de premsa #Figueres - 30/03/2017Recull de premsa #Figueres - 30/03/2017
Recull de premsa #Figueres - 30/03/2017
 
VISTO BUENO AL PLIEGO PARA LA CONTRATACIÓN DE DIFERENTES SERVICIOS PARA LA FE...
VISTO BUENO AL PLIEGO PARA LA CONTRATACIÓN DE DIFERENTES SERVICIOS PARA LA FE...VISTO BUENO AL PLIEGO PARA LA CONTRATACIÓN DE DIFERENTES SERVICIOS PARA LA FE...
VISTO BUENO AL PLIEGO PARA LA CONTRATACIÓN DE DIFERENTES SERVICIOS PARA LA FE...
 
How to actually get software build
How to actually get software buildHow to actually get software build
How to actually get software build
 
Нетехническая ТРИЗ для ваших профи, продавца, исследователя
Нетехническая ТРИЗ для ваших профи, продавца, исследователяНетехническая ТРИЗ для ваших профи, продавца, исследователя
Нетехническая ТРИЗ для ваших профи, продавца, исследователя
 
The London Underground. Your 100 year old guide to website navigation
The London Underground. Your 100 year old guide to website navigationThe London Underground. Your 100 year old guide to website navigation
The London Underground. Your 100 year old guide to website navigation
 
How to Increase Employee Participation in Social Good Programs
How to Increase Employee Participation in Social Good Programs How to Increase Employee Participation in Social Good Programs
How to Increase Employee Participation in Social Good Programs
 
Its all about the domain honey
Its all about the domain honeyIts all about the domain honey
Its all about the domain honey
 
Towards a software-defined programmable digital infrustructure
Towards a software-defined programmable digital infrustructureTowards a software-defined programmable digital infrustructure
Towards a software-defined programmable digital infrustructure
 
MeasureWorks - Online Tuesday - Time = Money
MeasureWorks - Online Tuesday - Time = MoneyMeasureWorks - Online Tuesday - Time = Money
MeasureWorks - Online Tuesday - Time = Money
 
IL MOBBING ed il BURN OUT
IL MOBBING ed il BURN OUTIL MOBBING ed il BURN OUT
IL MOBBING ed il BURN OUT
 
Enmienda Conjunta Presupuestos 2017
Enmienda Conjunta Presupuestos 2017 Enmienda Conjunta Presupuestos 2017
Enmienda Conjunta Presupuestos 2017
 
The Patterns of Distributed Logging and Containers
The Patterns of Distributed Logging and ContainersThe Patterns of Distributed Logging and Containers
The Patterns of Distributed Logging and Containers
 
Top U.S. Imports: Apparel
Top U.S. Imports:  Apparel Top U.S. Imports:  Apparel
Top U.S. Imports: Apparel
 
From pair programming to mob architecting
From pair programming to mob architecting From pair programming to mob architecting
From pair programming to mob architecting
 
Asynchronous micro-services and the unified log
Asynchronous micro-services and the unified logAsynchronous micro-services and the unified log
Asynchronous micro-services and the unified log
 

Similar to How Functional Programming Made Me A Better Developer

Agile Methodologies And Extreme Programming - Svetlin Nakov
Agile Methodologies And Extreme Programming - Svetlin NakovAgile Methodologies And Extreme Programming - Svetlin Nakov
Agile Methodologies And Extreme Programming - Svetlin Nakov
Svetlin Nakov
 
Code Review
Code ReviewCode Review
Code Review
Ravi Raj
 
Case Study: Experiences Using IBM Rational Method Composer to Deliver a BPM I...
Case Study: Experiences Using IBM Rational Method Composer to Deliver a BPM I...Case Study: Experiences Using IBM Rational Method Composer to Deliver a BPM I...
Case Study: Experiences Using IBM Rational Method Composer to Deliver a BPM I...
ghodgkinson
 
I Smell A RAT- Rapid Application Testing
I Smell A RAT- Rapid Application TestingI Smell A RAT- Rapid Application Testing
I Smell A RAT- Rapid Application Testing
Peter Presnell
 
The "Evils" of Optimization
The "Evils" of OptimizationThe "Evils" of Optimization
The "Evils" of Optimization
BlackRabbitCoder
 
97 thingseveryprogrammershouldknow
97 thingseveryprogrammershouldknow97 thingseveryprogrammershouldknow
97 thingseveryprogrammershouldknow
REHAN KHAN
 

Similar to How Functional Programming Made Me A Better Developer (20)

How to Deliver the Right Software (Specification by example)
How to Deliver the Right Software (Specification by example)How to Deliver the Right Software (Specification by example)
How to Deliver the Right Software (Specification by example)
 
Continuous Deployment and Testing Workshop from Better Software West
Continuous Deployment and Testing Workshop from Better Software WestContinuous Deployment and Testing Workshop from Better Software West
Continuous Deployment and Testing Workshop from Better Software West
 
Agile Methodologies And Extreme Programming
Agile Methodologies And Extreme ProgrammingAgile Methodologies And Extreme Programming
Agile Methodologies And Extreme Programming
 
Agile Methodologies And Extreme Programming - Svetlin Nakov
Agile Methodologies And Extreme Programming - Svetlin NakovAgile Methodologies And Extreme Programming - Svetlin Nakov
Agile Methodologies And Extreme Programming - Svetlin Nakov
 
Are You a SOLID Coder?
Are You a SOLID Coder?Are You a SOLID Coder?
Are You a SOLID Coder?
 
Top 10 Interview Questions for Coding Job.docx
Top 10 Interview Questions for Coding Job.docxTop 10 Interview Questions for Coding Job.docx
Top 10 Interview Questions for Coding Job.docx
 
Top 10 Interview Questions for Coding Job.docx
Top 10 Interview Questions for Coding Job.docxTop 10 Interview Questions for Coding Job.docx
Top 10 Interview Questions for Coding Job.docx
 
Code Review
Code ReviewCode Review
Code Review
 
Case Study: Experiences Using IBM Rational Method Composer to Deliver a BPM I...
Case Study: Experiences Using IBM Rational Method Composer to Deliver a BPM I...Case Study: Experiences Using IBM Rational Method Composer to Deliver a BPM I...
Case Study: Experiences Using IBM Rational Method Composer to Deliver a BPM I...
 
Generative AI in CSharp with Semantic Kernel.pptx
Generative AI in CSharp with Semantic Kernel.pptxGenerative AI in CSharp with Semantic Kernel.pptx
Generative AI in CSharp with Semantic Kernel.pptx
 
I Smell A RAT- Rapid Application Testing
I Smell A RAT- Rapid Application TestingI Smell A RAT- Rapid Application Testing
I Smell A RAT- Rapid Application Testing
 
Web Usability in the Enterprise with Ajax
Web Usability in the Enterprise with AjaxWeb Usability in the Enterprise with Ajax
Web Usability in the Enterprise with Ajax
 
Cs121 Unit Test
Cs121 Unit TestCs121 Unit Test
Cs121 Unit Test
 
Cinci ug-january2011-anti-patterns
Cinci ug-january2011-anti-patternsCinci ug-january2011-anti-patterns
Cinci ug-january2011-anti-patterns
 
The "Evils" of Optimization
The "Evils" of OptimizationThe "Evils" of Optimization
The "Evils" of Optimization
 
User Driven Software Architecture
User Driven Software ArchitectureUser Driven Software Architecture
User Driven Software Architecture
 
Software design.edited (1)
Software design.edited (1)Software design.edited (1)
Software design.edited (1)
 
Selective 97 things every programmer should know
Selective 97 things every programmer should knowSelective 97 things every programmer should know
Selective 97 things every programmer should know
 
97 thingseveryprogrammershouldknow
97 thingseveryprogrammershouldknow97 thingseveryprogrammershouldknow
97 thingseveryprogrammershouldknow
 
SLDC Presentation
SLDC PresentationSLDC Presentation
SLDC Presentation
 

More from Cameron Presley

More from Cameron Presley (9)

The Engineer's Playbook: Starting a New Role
The Engineer's Playbook: Starting a New RoleThe Engineer's Playbook: Starting a New Role
The Engineer's Playbook: Starting a New Role
 
Taking a Gamble with Functional Domain Modeling
Taking a Gamble with Functional Domain ModelingTaking a Gamble with Functional Domain Modeling
Taking a Gamble with Functional Domain Modeling
 
Level Up Your Functional Programming Skills with LINQ
Level Up Your Functional Programming Skills with LINQLevel Up Your Functional Programming Skills with LINQ
Level Up Your Functional Programming Skills with LINQ
 
Functional Programming Through Construction : First Principles
Functional Programming Through Construction : First PrinciplesFunctional Programming Through Construction : First Principles
Functional Programming Through Construction : First Principles
 
Establishing a SOLID Foundation
Establishing a SOLID FoundationEstablishing a SOLID Foundation
Establishing a SOLID Foundation
 
How Functional Programming Made Me a Better Developer
How Functional Programming Made Me a Better DeveloperHow Functional Programming Made Me a Better Developer
How Functional Programming Made Me a Better Developer
 
How to Have Code Reviews That Developers Actually Want
How to Have Code Reviews That Developers Actually WantHow to Have Code Reviews That Developers Actually Want
How to Have Code Reviews That Developers Actually Want
 
Making the Unstable Stable - An Intro To Testing
Making the Unstable Stable - An Intro To TestingMaking the Unstable Stable - An Intro To Testing
Making the Unstable Stable - An Intro To Testing
 
Establishing a SOLID Foundation - An Intro to Software Design
Establishing a SOLID Foundation - An Intro to Software DesignEstablishing a SOLID Foundation - An Intro to Software Design
Establishing a SOLID Foundation - An Intro to Software Design
 

Recently uploaded

AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
VictorSzoltysek
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
shinachiaurasa2
 

Recently uploaded (20)

VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
Pharm-D Biostatistics and Research methodology
Pharm-D Biostatistics and Research methodologyPharm-D Biostatistics and Research methodology
Pharm-D Biostatistics and Research methodology
 
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfThe Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
 
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verifiedSector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdfAzure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 

How Functional Programming Made Me A Better Developer

  • 1. How Functional Programming Made Me A Better Developer Cameron Presley @PCameronPresley http://blog.TheSoftwareMentor.com
  • 3. Little About Me  From Knoxville, TN  Software Engineer at Pilot Flying J  Musician, board gamer, and mentor  Co-organizer of FunctionalKnox
  • 4. Outline  The Journey of Learning Functional Programming  How My Style Has Evolved  How I Think About Software  Resources
  • 6. Some SOLID Inspiration  Mnemonic for software design coined by Robert C. Martin  By following SOLID, we tend to write more maintainable code  Teach and present on SOLID a lot  Want to focus on the Single Responsibility Principle
  • 7. Some SOLID Inspiration “The single responsibility principle states that every module or class should have responsibility over a single part of the functionality provided by the software, and that responsibility should be entirely encapsulated by the class.” -Wikipedia
  • 8. Some SOLID Interpretation What happens if you follow Single Responsibility to the extreme? Lots of small classes with a single method Felt like a lot of ceremony
  • 11. Seems Like I Wasn’t The First With This Idea… http://blog.ploeh.dk/2014/03/10/solid-the-next-step-is-functional/
  • 12. Mark’s Thoughts  Extreme SRP and ISP, leads to a lot of fine grain classes  An object can be thought of data with behavior  What if we flipped that concept around?  Behavior with data  Sounds like a functional approach to the problem
  • 13. Time to Learn Functional  Chose F#  Familiar with the .NET framework  Comfortable with the .NET tooling  Community resources
  • 14. Steps Along The Journey Link
  • 16. Premise  Decided to port a C# application (Task Snapper)  Grabs a screenshot every so often  Focus on the language, not the problem  Dipping my toes into FP
  • 18. Reflection  It’s F#!  Wrote object-oriented in a functional language  Definitely not idiomatic  Didn’t really see the point  Need to learn more about functional
  • 20. Premise  Found out that card games tend to be a great exercise  Want to use Love Letter as an example card game  8 different kinds of cards, each with their own special rules  Write more idiomatic functional  No classes, no exceptions
  • 22. Reflection  No exceptions!  Learning more about data modeling  Making illegal state unrepresentable  Game rules a bit more complicated than expected  Spending a lot of time learning the problem, not solving the problem
  • 24. Premise  Modeling a card game still makes sense  Something with easier business rules  Able to solve a real problem without getting lost  Decided on Blackjack  Just enough business rules to do something
  • 25. Making Illegal States Unrepresentable
  • 26. Business Rules for How Many Points a Card Is Worth
  • 27. Business Rules For Adding Points Together
  • 28. Calculating How Many Points For A Hand
  • 29. Reflection  Comfortable with handling errors without exceptions  Using pattern matching more effectively  Working with built-in List operators  Map and Reduce  Starting to think about how to use these concepts in C#
  • 30. At the End  Worked on/off over the course of the year  Got frustrated at times  Spent time learning, asking questions on Twitter, StackOverflow  Didn’t give up  Starting to think about these concepts in OOP
  • 33. Why Write Testable Code?  If code is easy to test, it’s usually easy to maintain  Implies that hard to test code may not be maintainable  What defines easy to test code?
  • 34. A Hypothetical Pretend we were writing tests around a particular method. Which one would you rather test? A) A method that when given the same two inputs, it returns the same output B) A method that when given the same two inputs, it returns different outputs
  • 35. Thinking About Testability  If a method always returns output solely based on inputs, we call that method pure.  No side effects or state involved  Easiest types of test to write  No mocking, no fake implementations to use  Frameworks that use this concept  Reducers (Redux)  Elm architecture
  • 36.
  • 38. Typical Approach  When working with a list of data, I’d typically work with a for each loop.
  • 39. A Different Approach  In functional programming, typically work with multiples of data as lists  Typically use built-in list functions (map, filter)
  • 40. Leveraging LINQ  Instead of Map and Filter, LINQ uses Select and Where
  • 41.
  • 43. How I Used Interfaces
  • 44. What Is an Interface  It’s a guarantee that a method with that signature exists  Essentially a contract  If the contract is not fulfilled, the compiler tells you  Allows us to use the compiler to find errors  “Leaning on the compiler”
  • 45. Pop Quiz! Given this interface, what should GetById return? Anything that implements this interface will always return a Record object, right?
  • 46. How about this implementation?
  • 47. Interfaces That Don’t Lie  Interface says that a Record will always be returned  If I handle the Record, then the compiler is happy  However, the compiler can’t enforce that I handled the null condition  Likely for me to introduce bugs into the system
  • 48. The Functional Approach  No concept of null, typically use something called Option or Maybe  Has two values, either Some<‘a> or None
  • 49. The Functional Approach  If a function returns an Option, callers must deal with it.  Otherwise, code fails to compile
  • 50. How My Style Evolved  Using purity to make business rules testable  Reducing complexity by leveraging LINQ  Think more about types and interfaces
  • 51. How I Think About Software
  • 52. Types of Components  Boundary  How data comes in or goes out of the system  Business Rule  How to work with data for our business needs  Workflows  Combines both boundary and business rule components
  • 53. Boundary Components  Anything that deals with getting data in/out of a system is a boundary  Examples include databases, servers, file system, user input, etc.  Makes sense to have these components implement an interface for testability  Inspired by Hexagonal Architecture by Alistair Cockburn
  • 56. Business Rule Components  Contain the rules for our application  Will make up the bulk of your application  Remember SOLID principles while implementing  For testability, purity is key
  • 58. Workflow Components  Combines both Boundary and Business Rule components  Can be described as  Data comes in through the boundary  Data is processed by the business rules  Data goes out through the boundary
  • 59. Bringing It All Together
  • 60. Bringing It All Together
  • 61. Bringing It All Together
  • 62. Bringing It All Together
  • 63. Wrapping Up  Learning functional was hard for me  Lots of failures, but improved incrementally  Made me re-evaluate how I build software  Still use functional concepts in my coding  Even if I’m not using a functional language
  • 64. Resources  F Sharp for Fun and Profit (https://fsharpforfunandprofit.com)  The Book of F#: Breaking Free with Managed Functional Programming by Dave Fancher  F# Jumpstart (Pluralsight Course) by Kit Eason  Mark Seemann’s blog: (http://blog.ploeh.dk)  Hexagonal Architecture (post) by Alistair Cockburn

Editor's Notes

  1. I worked on/off with functional for a year Lots of heads down, then come up for air for a bit Rinse and repeat Took three attempts for me to really “get” it