With the rise in popularity recently, functional programming has become ""The Next Big Thing"". As of today, there are tons of frameworks and tools that can be used for front-end, back-end, desktop, and mobile development. With that being said, the majority of us are still using object-oriented languages for our day jobs and don't need to learn functional programming, right?
In this talk, I'll walk you through my experiences learning functional programming over the last year, how my style of programming has changed, and how I now think about programming with regards to both functional and object-oriented paradigms.
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
5
6. “
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
57. 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
63. Boundary
○ Databases, files, APIs
○ Typically impure
○ For easier testing of workflows,
recommend using an interface
○ Inspiration from Alistair Cockburn’s
Hexagonal Architecture
63
68. 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
75. 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
The Road Taken Learning Functional Programming (i.e. What Got Me Started Down This Path?)
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)
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?
Asking the Twitterverse for help
Like most things in software development, I wasn’t the first to come to this
Chose F#
Familiar with .NET
Comfortable with the tooling
Community resources
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
First Attempt!
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
Defined a type
Attached functions to it
Some of those functions threw exceptions
Defined a type
Attached functions to it
Some of those functions threw exceptions
Defined a type
Attached functions to it
Some of those functions threw exceptions
Defined a type
Attached functions to it
Some of those functions threw exceptions
I wrote F#
But wrote OO in FP
Didn’t really see the point
Need to learn more about FP
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
Modeling Data and State using Discriminated Unions
Using type aliases
Making new types out of existing types using records
No classes, no exceptions!
3rd Attempt
Making Illegal States Unrepresentable
Modeling how to turn a Card into a
How to add two points together
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#
Learned all of this cool stuff, how does it affect my coding habits now?
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?
What’s easier to test?
Which method would be easier to troubleshoot?
Implies there are no side effects (databases, file systems, apis, etc…)
Easiest thing to test because no dependencies are needed
Elm Architecture (time traveling debugger)
React with Redux (reducers)
Pure because 3 will always return the same value
Not pure because the API could fail or give me back different values
Improved LINQ Skills
When working with lists of data, here’s how I’d used to do it
Quickly, what am I trying to do here?
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
Designing Interfaces that don’t suck
Allows us to lean on the compiler
Why doesn’t this work? Options have no clue about Id, FirstName or LastName
Pattern match to figure out what to do.
Compiler forced us to do that!
Win!
How I build software
Getting data into the system
Getting data out of the system
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