Abstract for the presentation:
F# is a language I am passionate about and a language I would like to use on my day job, and for that to be a reality I need you to join me! I want you to join me in the welcoming F# community where you can see a new world open up for you and enabling you to solve problems more efficiently than you do today with C#. F# is not a language that is used only in academia, it is a general purpose language that is growing every day with an awesome community of brilliant people. F# also defines the future of C# in many ways, so if you want to be ahead of the game you should jump on the F# bandwagon today. There are many reasons to learn F# like; a new paradigm will make you a better overall developer, F# will make you write less bugs and you will have more fun at work!
I this presentation I will show you:
What the strengths of F# are
Why you should start using F#
How you get started with F#
Don't hesitate, join the F# movement. The only way to improve is to change!
1. F# AS OUR DAY JOB BY 2016
Oslo/NDC Oslo
Tomas Jansson
19/06/2015
2. THIS IS ME!
Tomas Jansson
Manager & Practice Lead .NET
BEKK Oslo
@TomasJansson
tomas.jansson@bekk.no
github.com/mastoj
slideshare.net/mastoj
blog.tomasjansson.com
20. #2: Separation
"Objects are not data structures. Objects may use data structures; but
the manner in which those data structures are used or contained is
hidden. This is why data fields are private. From the outside looking in
you cannot see any state. All you can see are functions. Therefore
Objects are about functions not about state."
Uncle Bob - http://blog.cleancoder.com/uncle-bob/2014/11/24/FPvsOO.html
21. #2: Separation
"Objects are not data structures. Objects may use data structures; but
the manner in which those data structures are used or contained is
hidden. This is why data fields are private. From the outside looking in
you cannot see any state. All you can see are functions. Therefore
Objects are about functions not about state."
Uncle Bob - http://blog.cleancoder.com/uncle-bob/2014/11/24/FPvsOO.html
23. #3: Null handling
Null References: The Billion Dollar Mistake
- Sir Charles Antony Richard Hoare
http://tinyurl.com/TheBillionDollarMistake
24. #3: Null handling
type Order = {OrderId: int; Total: int}
let getOrder orderId =
match orderId with
| 0 -> None
| _ -> Some { OrderId = orderId
Total = 23*orderId }
let printOrder order =
match order with
| Some o ->
printfn "Order id: %i, Total: %i«
o.OrderId o.Total
| None ->
printfn "No order"
25. #3: Null handling
type Order = {OrderId: int; Total: int}
let getOrder orderId =
match orderId with
| 0 -> None
| _ -> Some { OrderId = orderId
Total = 23*orderId }
let printOrder order =
match order with
| Some o ->
printfn "Order id: %i, Total: %i«
o.OrderId o.Total
| None ->
printfn "No order"
Returns
Order option
26. #3: Null handling
type Order = {OrderId: int; Total: int}
let getOrder orderId =
match orderId with
| 0 -> None
| _ -> Some { OrderId = orderId
Total = 23*orderId }
let printOrder order =
match order with
| Some o ->
printfn "Order id: %i, Total: %i«
o.OrderId o.Total
| None ->
printfn "No order"
Returns
Order option
Explicit null
handling!
27. #3: Null handling
public class Order
{
public int OrderId { get; set; }
public int Total { get; set; }
}
public Order GetOrder(int orderId)
{
if (orderId == 0) return null;
return new Order() {
OrderId = orderId,
Total = 23*orderId };
}
public void PrintOrder(Order order)
{
Console.WriteLine("Order id: {0}, Total: {1}",
order.OrderId, order.Total);
}
28. #3: Null handling
public class Order
{
public int OrderId { get; set; }
public int Total { get; set; }
}
public Order GetOrder(int orderId)
{
if (orderId == 0) return null;
return new Order() {
OrderId = orderId,
Total = 23*orderId };
}
public void PrintOrder(Order order)
{
Console.WriteLine("Order id: {0}, Total: {1}",
order.OrderId, order.Total);
}
How do I
differ success
from failure?
29. #3: Null handling
public class Order
{
public int OrderId { get; set; }
public int Total { get; set; }
}
public Order GetOrder(int orderId)
{
if (orderId == 0) return null;
return new Order() {
OrderId = orderId,
Total = 23*orderId };
}
public void PrintOrder(Order order)
{
Console.WriteLine("Order id: {0}, Total: {1}",
order.OrderId, order.Total);
}
How do I
differ success
from failure?
What about
null?
51. #6: Readability
// Version 1
let executeCommand command =
command |> validate |> log |> execute |> log
let result = executeCommand command
// Version 2
let executeCommand = validate >> log >> execute >> log
let result = executeCommand command
52. #6: Readability
// Version 1
public ExecutionResult ExecuteCommand(Command command)
{
return Log(Execute(Log(Validate(command))));
}
var result = ExecuteCommand(new Command());
// Version 2
public ExecutionResult ExecuteCommand(Command command)
{
var validationResult = Validate(command);
Log(validationResult);
var executionResult = Execute(validationResult);
Log(executionResult);
return executionResult;
}
var result = ExecuteCommand(new Command());
55. Cycles
Compared 10 C# projects with 10 F# projects
Compared «top-level» types
Top-level type: âa type that is not nested and
which is not compiler generatedâ
@ScottWlaschin
http://tinyurl.com/CyclesInTheWild
56. PROJECTS
Mono.Cecil, which inspects programs and libraries in the
ECMA CIL format
NUnit
SignalR for real-time web functionality
NancyFx, a web framework
YamlDotNet, for parsing and emitting YAML
SpecFlow, a BDD tool
Json.NET
Entity Framework
ELMAH, a logging framework for ASP.NET
NuGet itself
Moq, a mocking framework
NDepend, a code analysis tool
FSharp.Core, the core F# library.
FSPowerPack.
FsUnit, extensions for NUnit.
Canopy, a wrapper around the Selenium test automation
tool.
FsSql, a nice little ADO.NET wrapper.
WebSharper, the web framework.
TickSpec, a BDD tool.
FSharpx, an F# library.
FParsec, a parser library.
FsYaml, a YAML library built on FParsec.
Storm, a tool for testing web services.
Foq, a mocking framework.
C# F#
64. Does your
language
matter?
Application to assist in maintaining the stability and security
of the electricity supply in the UK
Existing solution almost fully implemented in C#
Rewritten in F#
@simontcousins
http://tinyurl.com/DoesTheLanguageMakeADifference
65.
66.
67. âThe C# project took five years and peaked at ~8 devs. It never fully
implemented all of the contracts.
The F# project took less than a year and peaked at three devs (only one had
prior experience with F#). All of the contracts were fully implemented.â
http://tinyurl.com/DoesTheLanguageMakeADifference
68. THE LANGUAGE YOU USE MAKE A DIFFERENCE
Encapsulated small types
Less top-level dependencies
Less «leaky» abstractions
Types and functions must be
defined
Prevents cycles
Encapsulation
Much easier to do in F#
Linear Generics
Welcome!
Iâm glad you made it.
Almost the end of the conference
As most of you I eas sleep, code and repeat
Not 100% satisified
Misunderstood
Solving problem suboptimal in C#, and the language is part of the problem
As you can see here
Live like a super hero in 2016
Need to change to this
100% would be stretching it because of legacy
C# ï Would never make me feel like a super hero
The industry is not in a good shape
To improve we need to change
I studied myself for two periods,
one period where I refused change and
one where I embraced it, the result were mind blowing
Awesomeness decreased if no change
World was changing and I didnât embrace it
Itâs not a smooth ride, but if Iâm willing to change I can stop bad trends
Applies to all of us in many situations
Bryan Hunter Lean & FP
You need to measure and continuosly improve
So what can change?
Letâs consider an application.
In ideal world the code would match the application and every line would produce value
Reality is that we need to do a lot of plumbing
The plumbing is both in the domain and just access to external services for example.
All the code that doesnât produce value is extra complexity
Decreasing extra complexity would lead to more value delivered
That leads us to the agenda.
Why will give you some of my technical arguments to why I like F#
The facts show you some number from two different studies
How you can get started and what you can do
Enforces more good habits then C#, still need dicipline. But F# set you up in a much better position than C# and many other languages.
Higher order reason
Better type checking and less typing, because of better inference and linear style programming
more declarative than C# imperative style ï I can focus on what and let the language deal with how
Proper F# separates data structures and functions in a healthy way
C# mixes them and stores state internally
Uncle Bob wrote this in a post about Functional Programming vs. OO
Letâs highlight some important parts
Objects are not data structures â but why do we tie them so tightly together?
All you can see are functions â focus on functions
Objects are about functions
These are good points, but it almost feel like he is trying to explain something that better fits in a FP language than OO. Immutable by default.
Do I really need to say something?
Known as Tony Hoare, introduced it in Algol 1965.
Talk from 2009
Itâs not much less code than a C# version of the same thing, but letâs look at the details
Instea of returning something of type Order weâre returning an Option, which you can think of as nullable for everyt kind of type. ï must handle missing order explicit
Instea of returning something of type Order weâre returning an Option, which you can think of as nullable for everyt kind of type. ï must handle missing order explicit
Looks ok.
How was I suppose to know I should handle null? The compiler doesnât tell me!
How was I suppose to know I should handle null? The compiler doesnât tell me!
I wonât cover everything about types, but they are awesome in F#. Much more powerful than they are in C#.
This is something you can grasp
You see the data and you know it is immutable and have structural equality
Letâs check the C# version
Here it is much more harder to reason about what the structure give us
Am I trying to do it immutable? (yes)
Equality? (yes)
Is it correct? (Who knows?) â resharper told me it is
This is probably one of thing you can start using quite easily in your own projects to replace ORM and endpoint clients
ORM
Strongly typed
Multiple sources
Automatic types
Intelli sense
Often you here people complaining about readability in Functional languages
So let us check this conversation I had with C#Hero
Two simple ways of implementing a function thatâs clear what it does
Reads left to right in both examples
Of course you need to know the pipeline syntax and binding syntax, but that is just syntax!
More noise
Reversed logic from inner to outer or right to left, instead of left to right
Dependecies and cycles
Scott Wlaschin â Evelina Gabasova made som further studies â based on the first study
Comparison of C# and F#
Simon Cousins
What is top-level type?
Tries to define modularity?
Need to find a balance of modularity!
TickSpec and SpecFlow is sort of comparable
Much more top-level types
It might be more pluggable, but not necessary because of generics
It might also be harder to use and develop in since there are more top-level types to integrate with
Doesnât say that much by itself so letâs check dependencies
Dependencies is good if can keep it down, because then we have module that fit in our head
Harder to reason
Here you see how the depencies are distributed among number of dependencies
When there are dependencies they are much less of them, because generics and internal types
Almost no cycles in F# projects
Fewer depencies -> less likely to have cycles
Linear programming -> hard to introduce cycles
Visualization of two «comparable» projects
Itâs hard to grasp what this project does
Maybe harder to fix bug
Provides a lot of the same features
Maybe not as many, but I think the structure looks simpler here
Knew more about the domain I think
Doesnât explain a 1:10 ratio between LOC for F# vs C#
Less code ï less code to test ï higher test ratio
Less code ï less boiler plate ï more useful code
The language you use do make a difference!
Why do we get these results in these two studeis?
Correctnes â fewer bugs
Conciseness ï Reason about the code
So what can you do to start using these super powers?
You do need to code & read!
If you donât know the strengths and weaknesses itâs hard to discuss and argue for them
You have to put in the hours that Yan talked about in his languages talk Wednesday
There is an amazing community out there that are willing to help!
If you live in Oslo, reach out to the new F# meetup!
If not in Oslo, go on twitter and the #fsharp tag and follown along and engage or check the F# foundation
Donâtâ wait for the community, be the community
Start small to try it out
Test
Tools
DTOs
Data layers -> Demo