f# for bloba, by brandon d'imperio
DESCRIPTION
The prevalent wisdom seems to be that FSharp is only for Math, Science and Engineering. F# for Boring Line of Business Apps aims to show why it's capable of far more than that.TRANSCRIPT
F# for BLOBAIt isn’t just for Science and Financial DomainsBoring Line Of Business Applications
imaginarydevelopment.blogspot.com @maslowjax
Brandon D’Imperio
A team that peaked at 3 developers (only one of which knew F#) finished in 1 year what a team that peaked at 8 developers couldn’t do in 5.
A Team of 3 Devs Beat a Team of 8
● @TachyusCorp - From zero to shipping product in 12 weeks ( $28 million within 26 months)
● Princeton University - F# ... made it trivial ... ( Graduate courses on Parallelism)
But That’s Just One Company Rave
Define WinImplementation C# F#
Braces 56,929 643
Blanks 29,080 3,630
Null Checks 3,011 15
Comments 53,270 487
Useful Code 163,276 16,667
App Code 305,566 21,442
Test Code 42,864 9,359
Total Code 348,430 30,801
In most ways yes.C#, F#● Test Code Ratio 14%, 44%● Useful Code Ratio 53%, 78%
o Almost half the c# code was noise● The C# was not finished and often failed to
reach near-real-time
LoC is a Terrible Metric
● Could you find bugs more easily in 1/5th of the code? 1/10th?
● OOP SOLID and Design patterns become a thing of the past o Design patterns are language bug reports -
(paraphrased) - Peter Norvigo DI is not even relevant
What Does This Mean to Me?
A Code Break - What’s Wrong? (with both)
C#
public class Contact {public string FirstName {get;set;}public string MiddleInitial {get;set;}public string LastName {get;set;}public string EmailAddress {get;set;}// true if ownership of email address
is confirmedpublic bool IsEmailVerified {get;set;}
}
F#
type Contact = {FirstName: stringMiddleInitial: stringLastName: stringEmailAddress: string// true if ownership of email address
is confirmedIsEmailVerified: bool
}
Which are optional?What are the constraints? (are all strings allowed to be unlimited length?)Which fields have a relationship?What is the domain logic?
What’s Wrong?
● Express requirements clearly
Desired Features of BLOBA Development
● Express requirements clearly - Is the flow of the code clearer with less noise?
Desired Features of BLOBA Development
● Express requirements clearly● Rapid development cycle
o Built-in REPLo Far less boilerplate codeo Production-ready code capability for non-f# devs in
2-4 weeks
Desired Features of BLOBA Development
● Express requirements clearly● Rapid development cycle● High quality deliverables
o Why don’t we sub-type ints? (Primitive Obsession)o Is that int the Id of a user or the Id of something
else?
Desired Features of BLOBA Development
● Express requirements clearly● Rapid development cycle● High quality deliverables● Fun (Ruby anyone?)
Desired Features of BLOBA Development
● No null● Extended strong typing● Enums (DU), Records (no more mapping
code), Units of Measure, Strongly typed strings!o Type Providerso Design-time checking of string formats
Features not to be missed
Units of Measure
[<Measure>]type MemberId[<Measure>]type m // meters[<Measure>]type milelet mPerMile: float<m/mile> = 1609.34<m/mile>let milesToMeters (mi:float<mile>) = mi * mPerMile
● Inheritance● Mutability● Accidental Recursion● Cyclic Dependencies
o In C# Very do-able via Web References Very do-able within an assembly
Features We’d Like to Forget
● Pattern matching● Composition over inheritance● typedefof<> and typeof<>
Features Worth Mentioning
Methods
C#public void Main(string[] args){ /* … */ }
F# ● let Main (args: string[]) = ...● let Main (args: string array) = …● let Main args = ...
Method calling options
let HelloWorld a b c = …let HelloWorld (a,b,c) = …let HelloWorld (a,b) c = … // different// more concretelet ProcessName first m last = …
● Compiler comparisono LoC
Roslyn C# - almost 500k lines of code F# - 150k lines of code over half the C# lines are non-useful
o Cycles 899 mutually dependent classes vs ZERO
More stats
Warts● (args:string[]) [] means array here every other context in F# [ ] would be a
list.● required fun! fun e -> e instead of C#’s e => e● printfn with [<Measure>] (fixed in next version most likely)● this. everywhere inside a type● no protected members● Property syntaxes ( immutable, mutable) (auto option)
o member this.MyProp = foo //immutable o member this.MyProp with get() = … // mutable o member val MyProp = “” // auto immutableo member val MyProp = “” with get,set // auto mutable
Links● My blog
o http://imaginarydevelopment.blogspot.com/o Why am I so enthusiastic about F#? -
http://imaginarydevelopment.blogspot.com/2014/06/why-am-i-so-enthusiastic-about-f.html● F# cheat sheet - http://dungpa.github.io/fsharp-cheatsheet/● Domain Driven Design using the F# type system -
https://www.slideshare.net/slideshow/embed_code/28992749 - Scott Wlaschin● Does the language you use make a difference (revisited) -
http://simontcousins.azurewebsites.net/does-the-language-you-use-make-a-difference-revisited/● What about inheritance? -
http://codewonderings.blogspot.com/2012/09/what-about-inheritance.html● SOLID: the next step is Functional
http://blog.ploeh.dk/2014/03/10/solid-the-next-step-is-functional/ - Mark Seemann