How Functional Programming Made Me a Better Developer

Cameron Presley
Cameron PresleySoftware Engineer at SentryOne
Cameron Presley
@pcameronpresley
Cameron@TheSoftwareMentor.com
How
Functional Programming
Made Me
a Better Developer
2
Hello!
3
4
“
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
5
“
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
6
What Happens
If You Follow SRP
to the Extreme?
7
8
9
10
11
http://blog.ploeh.dk/2014/03/10/solid-the-next-step-is-functional/
Extreme SRP and ISP
=
lots of fine grain classes
12
Objects can be thought of data
with behavior, but what if we
flipped that around?
13
14
F#
15
16
Task Snapper
○ C# application that grabs a
screenshot every so often
○ Focus on the language, not the
problem
17
18
19
20
21
22
23
Love Letter
○ 8 different kinds of cards, each with
their own rules
○ Write more idiomatic fp
○ No classes or exceptions
24
25
26
27
28
29
30
Blackjack!
○ Still modeling a card game
○ Something with easier business rules
○ Already knew the rules
31
32
33
How would you score a hand in
Blackjack?
Figure out how many Points
each Card is worth
Add the Points up
34
35
36
37
38
39
40
different outputs
the same output
A method, that when given the
same two inputs, it returns…
41
Methods that always return
output solely based on input
are known to be pure
42
Sound crazy?
Elm
React with Redux
43
44
45
46
47
48
49
50
51
52
53
Interfaces are powerful because
they enforce a contract at the code
level.
If the contract isn’t observed,
the code won’t compile!
Given this interface, what should GetById return?
Pop Quiz!
Anything that implements this interface will always
return an Employee object, right?
55
56
57
What Happened?
Interface said that an Employee
will always be returned
But sometimes, there won’t be an
Employee, so what to return?
Compiler can’t enforce that I
handle null / exceptions
58
59
60
61
62
Boundary (data in/out)
Business Rules (processing the data)
Workflow (combination of both)
Boundary
○ Databases, files, APIs
○ Typically impure
○ For easier testing of workflows,
recommend using an interface
○ Inspiration from Alistair Cockburn’s
Hexagonal Architecture
63
64
65
Business Rules
○ Bulk of the application
○ Purity is key
○ Remember SOLID when implementing
66
67
Workflows
○ Combines both Boundary and Business Rule
○ General shape is:
○ Data comes in through the boundary
○ Data is processed by the business rules
○ Data goes out through the boundary
68
69
70
71
72
73
74
75
References
F#
• 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
Functional
• Mark Seemann’s blog: (http://blog.ploeh.dk)
• Hexagonal Architecture (post) by Alistair Cockburn
• Reid Evans YouTube
(https://www.youtube.com/channel/UCMxR2KmDlDMEsvfKOjzRNbA)
Code
• CRUDy : https://github.com/cameronpresley/CRUDy
• Optionally: https://github.com/cameronpresley/Optionally
Feedback!
76
http://blog.TheSoftwareMentor.com/F
eedback
1 of 76

More Related Content

What's hot(20)

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

Quick Intro to Clean CodingQuick Intro to Clean Coding
Quick Intro to Clean Coding
Ecommerce Solution Provider SysIQ659 views
TxJS 2011TxJS 2011
TxJS 2011
Brian LeRoux1.2K views
Design poo my_jug_en_pptDesign poo my_jug_en_ppt
Design poo my_jug_en_ppt
agnes_crepet577 views
Intro To AOPIntro To AOP
Intro To AOP
elliando dias431 views
Thinking FunctionallyThinking Functionally
Thinking Functionally
Piyush Katariya379 views
Performance Tuning with XHProfPerformance Tuning with XHProf
Performance Tuning with XHProf
Salesforce Engineering850 views
GraphQL vs. (the) RESTGraphQL vs. (the) REST
GraphQL vs. (the) REST
coliquio GmbH266 views
Software Craftmanship - Cours PolytechSoftware Craftmanship - Cours Polytech
Software Craftmanship - Cours Polytech
yannick grenzinger31 views
Real World Java CompatibilityReal World Java Compatibility
Real World Java Compatibility
Tim Ellison542 views
Software design principlesSoftware design principles
Software design principles
Md.Mojibul Hoque2.2K views

Recently uploaded(20)

METHOD AND SYSTEM FOR PREDICTING OPTIMAL LOAD FOR WHICH THE YIELD IS MAXIMUM ...METHOD AND SYSTEM FOR PREDICTING OPTIMAL LOAD FOR WHICH THE YIELD IS MAXIMUM ...
METHOD AND SYSTEM FOR PREDICTING OPTIMAL LOAD FOR WHICH THE YIELD IS MAXIMUM ...
Prity Khastgir IPR Strategic India Patent Attorney Amplify Innovation24 views
[2023] Putting the R! in R&D.pdf[2023] Putting the R! in R&D.pdf
[2023] Putting the R! in R&D.pdf
Eleanor McHugh36 views
ThroughputThroughput
Throughput
Moisés Armani Ramírez31 views
Green Leaf Consulting: Capabilities DeckGreen Leaf Consulting: Capabilities Deck
Green Leaf Consulting: Capabilities Deck
GreenLeafConsulting177 views
The Research Portal of Catalonia: Growing more (information) & more (services)The Research Portal of Catalonia: Growing more (information) & more (services)
The Research Portal of Catalonia: Growing more (information) & more (services)
CSUC - Consorci de Serveis Universitaris de Catalunya59 views
CXL at OCPCXL at OCP
CXL at OCP
CXL Forum203 views

How Functional Programming Made Me a Better Developer

Editor's Notes

  1. The Road Taken Learning Functional Programming (i.e. What Got Me Started Down This Path?)
  2. Some SOLID Inspiration SOLID = Mnemonic for software designed By following SOLID we tend to write more maintainable software Want to focus on S (Single Responsibility Principle)
  3. Lot of small classes with a single public method A lot of ceremony Could use ReSharper or another tool to create the boilerplate, but how about eliminate the need for it?
  4. Asking the Twitterverse for help
  5. Like most things in software development, I wasn’t the first to come to this
  6. Chose F# Familiar with .NET Comfortable with the tooling Community resources
  7. 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
  8. First Attempt!
  9. Decided to port a C# application (Task Snapper) Grabs a screenshot every so often Idea was to focus on the language, not how to solve the problem
  10. Defined a type Attached functions to it Some of those functions threw exceptions
  11. Defined a type Attached functions to it Some of those functions threw exceptions
  12. Defined a type Attached functions to it Some of those functions threw exceptions
  13. Defined a type Attached functions to it Some of those functions threw exceptions
  14. I wrote F# But wrote OO in FP Didn’t really see the point Need to learn more about FP
  15. Look ma, no exceptions! Learned more about data modeling (making illegal states unrepresentable) Game rules a bit more complicated than expected *Spent a lot of time learning the problem and attempting a solution, not porting
  16. Modeling Data and State using Discriminated Unions
  17. Using type aliases
  18. Making new types out of existing types using records
  19. No classes, no exceptions!
  20. 3rd Attempt
  21. Making Illegal States Unrepresentable
  22. Modeling how to turn a Card into a
  23. How to add two points together
  24. 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#
  25. Learned all of this cool stuff, how does it affect my coding habits now?
  26. Code is easy to test implies code is easy to maintain The opposite may hold then, hard to test code may not be maintainable What defines easy to test code?
  27. What’s easier to test? Which method would be easier to troubleshoot?
  28. Implies there are no side effects (databases, file systems, apis, etc…) Easiest thing to test because no dependencies are needed
  29. Elm Architecture (time traveling debugger) React with Redux (reducers)
  30. Pure because 3 will always return the same value
  31. Not pure because the API could fail or give me back different values
  32. Improved LINQ Skills
  33. When working with lists of data, here’s how I’d used to do it Quickly, what am I trying to do here?
  34. Before and after The one on the left, I have to keep track of what I’m doing The one on the right, I can trust that LINQ works and I’m plugging my business rules into the correct places
  35. Designing Interfaces that don’t suck
  36. Allows us to lean on the compiler
  37. Why doesn’t this work? Options have no clue about Id, FirstName or LastName
  38. Pattern match to figure out what to do. Compiler forced us to do that! Win!
  39. How I build software
  40. Getting data into the system
  41. Getting data out of the system
  42. 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