1<!DOCTYPE html> 2 3 4 5<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en-us"> 6<head> 7 <meta charset="utf-8"> 8 <meta http-equiv="content-type" content="text/html; charset=utf-8"> 9 10 11 <title>3 Reasons to Use F# · Ryan Clarke</title> 12 <meta name="author" content="Ryan Clarke" /> 13 <meta name="description" content="The blog of Ryan Clarke" /> 14 <link rel="canonical" href="//www.ryanclarke.net/post/3-reasons-to-use-f-sharp/" /> 15 16 17 <meta name="viewport" content="width=device-width, initial-scale=1"> 18 19 20 <link href="//fonts.googleapis.com/css?family=Open+Sans:300,400,600,700,800" rel="stylesheet" type="text/css"> 21 <link href='https://d33wubrfki0l68.cloudfront.net/css/d5600019382bffae515f81da2c2d06c7502ea590/css/all.css' rel='stylesheet'/> 22 <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css"> 23 24 25 <link rel="apple-touch-icon-precomposed" sizes="144x144" href="/apple-touch-icon-144-precomposed.png"> 26 <link rel="shortcut icon" href="/favicon.ico"> 27 28 29 <link href="" rel="alternate" type="application/rss+xml" title="Ryan Clarke" /> 30 31 32 <meta name="twitter:card" content="summary" /> 33 <meta name="twitter:site" content="@RyanSClarke" /> 34 <meta name="twitter:title" content="3 Reasons to Use F#" /> 35 <meta name="twitter:description" content="In lieu of anything more objective, I will be using my opinions to explain why F# is such a wonderful language." /> 36 <meta name="twitter:image" content="//www.ryanclarke.net/logo.png" /> 37 <meta name="twitter:url" content="//www.ryanclarke.net/post/3-reasons-to-use-f-sharp/" /> 38 39 40 <meta name="application-name" content="RyanClarke.net"/> 41 <meta name="msapplication-config" content="browserconfig.xml" /> 42</head> 43 44<body> 45 <div class="container"> 46 <div class="four columns sidebar"> 47 <nav> 48 <h1>Hi.</h1> 49 <a href="//www.ryanclarke.net/"> 50 <img src="//www.ryanclarke.net/logo.png" id="logo" alt="Blog logo"/> 51 </a> 52 <h2>I'm <a href="//www.ryanclarke.net/about">Ryan Clarke</a>.</h2> 53 <hr/> 54 <ul> 55 <p>Varied submissions of an adventurer</p> 56 <hr/> 57 <div> 58 <div id="social"> 59 Follow me: 60<div id="stalker"> 61 62 <a title="ryanclarke on Github" href="https://github.com/ryanclarke"> 63 <i class="fa fa-github-square"></i> 64 </a> 65 66 67 68 69 70 71 72 73 74 75 76 <a title="RyanSClarke on Twitter" href="https://twitter.com/RyanSClarke"> 77 <i class="fa fa-twitter-square"></i> 78 </a> 79 80 81 82 83 84 85 86 87 88 89 <a title="RSS feed" id="rss" href="/index.xml"> 90 <i class="fa fa-rss-square"></i> 91 </a> 92</div> 93 94 </div> 95 </div> 96 </ul> 97</nav> 98 99 </div> 100 101 <div class="eleven columns content"> 102 103 104<p class="meta"> 105 Tue, Nov 5, 2019 106 <a href="/"> 107 <i class="home fa fa-home"></i> 108 </a> 109</p> 110 111<h1 class="title">3 Reasons to Use F#</h1> 112 113<div id="post"> 114 115 116<p>Picking the best programming language is difficult because we canât just pick the one with the largest goodness number. Obviously, these objective goodness numbers are the easiest and most reliable way to find the best language, but someone neglected to assign them. Instead we individually convert our time slowly into experiences and our experiences into understanding and our understanding into opinions. Then we add these thoughtfully-honed opinions to all the other ones weâve created in the heat of debate as the need arose.</p> 117 118<p>So in lieu of anything more objective, I will be using my opinions to explain why <a href="https://fsharp.org/">F#</a> is such a wonderful language. I will use reasons to support my claim that these are good opinions and not the flimsy kind.</p> 119 120<h3 id="predictable-functions-and-safe-data">Predictable functions and safe data</h3> 121 122<p>Most of the programming I do involves editing, fixing and extending existing code. The ease at which I can create brand new software is less important than my ability to understand and adapt existing functionality and architecture. F#âs emphasis on pure functions and immutable data structures make F# easier to understand.</p> 123 124<p>A function is pure if an input will always return the same output and it has no side-effects. This means it does not depend on any mutable state for determining its output and it does not change anything in the program or environment other than
124its return value. F#âs bias toward immutable values assists in that any value once set will never change. A data structure can be shared around knowing that one function will not change it out from other another function.</p> 125 126<p>This may seem extreme to a programmer used to creating an object and updating its properties and fields as necessary, but the interdependencies that spread through reliance on shared mutable state quickly lead to tricky debugging sessions. And writing solid tests around a group of impure functions with mutable state can be onerous or even impossible. With pure functions, dependencies are easy to see and tests usually require simpler setup and can have better coverage.</p> 127 128<p>Immutable data can be hard to handle at first because a shared object, if changed in a function, will not be changed in any other references since the change involves creating an altered copy and leaving the original as it was. This altered copy will only exist as the return value of the function which updated it. This aspect of immutable data forces program flow to be architected more like a railroad track than a bumper car rink. Think of most web backends: a single track for the request to the database and back with the response. The data is following a single path; many business applications are like this. Obviously, there will be a lot of complexity in an application of any worth, but having the high-level architecture be this simple succession of functions returning new immutable pieces of state makes for highly reasonable and testable code. It makes for code that can be tweaked and extended with confidence.</p> 129 130<p>Let me add that impure functions are necessary and mutable data can be convenient. F# is a very practical language and allows both of those things. Interacting with a database or filesystem, writing to logs and debug statements are no trouble with F# and the better you encapsulate the intrinsic impurity they add, the cleaner the rest of your program will be. A mutable value simply requires the <code>mutable</code> keyword and the compiler and tooling will assist and warn as necessary as you interact with it.</p> 131 132<h3 id="algebraic-types-for-domain-modeling">Algebraic types for domain modeling</h3> 133 134<p>F# has a powerful algebraic type system. Itâs called algebraic because it includes both sum and multiplicative types. Sum types say that a type could be this OR that, so all the possibilities of this are added to that. Multiple types say that a type can be this AND that, so all the possibilities of this are multiplied with that. Additionally, there are types that wrap one type in another. This algebraic way of combining types is very conducive for domain modeling.</p> 135 136<p>F#âs single case union types are wonderful for wrapping simple types in a more descriptive and compiler protected object. For instance, a string with an email address could be wrapped with <code>UnverifiedEmailAddress</code> or <code>VerifiedEmailAddress</code>. That way it couldnât get used accidentally for the user name like it could with raw strings.</p> 137 138<p>F#âs multiple case union types are useful for combining various states that can be quite diverse within a single type. For example, an <code>EmailAddress</code> type could be a union of <code>UnverifiedEmailAddress</code> and <code>VerifiedEmailAddress</code>. When dealing with <code>EmailAddress</code> the compiler knows and enforces that you are handling the three valid options. This is far more expressive and safer than having an <code>emailAddress</code> string and a <code>isVerified</code> Boolean, either of which could be null, and which must be combined to figure out what the full story is. With the union types we are just summing together all the possibilities of <code>EmailAddress</code> into one compiler understood type. All this code looks like this:</p> 139 140<pre><code class="language-fsharp">type UnverifiedEmailAddress = UnverifiedEmailAddress of string 141type VerifiedEmailAddress = VerifiedEmailAddress of string 142type EmailAddress = 143 | UnverifiedEmail of UnverifiedEmailAddress 144 | VerifiedEmail of VerifiedEmailAddress 145</code></pre> 146 147<p>Where union types provide a way to sum a type by saying it could be this OR that OR something else, record and tuple types allow multiplication by saying this type contains this AND that. I donât have space to fully explore F#âs record and tuple types, and they really arenât much different from a conventional class. But where many languages have a lot of syntax involved in creating a class (many of them conventionally giving each class definition its own file) F# makes it easy to create very expressive data structures in a few lines of code. Itâs even further out of scope of this article, but F# also includes syntax for creating standard C# objects with inheritance, interfaces, attributes, virtual methods and all that jazz, if you should need the go fully-fledged into OO programming in the middle of an F# project.</p> 148 149<p>The way these powerful sum and multiplicative types can be combined to build accurate domain models is best left to <a href="https://fsharpforfunandprofit.com/">Scott Wlaschin</a> in his excellent book <a href="https://fsharpforfunandprofit.com/books/#domain-modeling-made-functional-ebook-and-paper"><em>Domain Modeling Made Functional</em></a>. Many of the principles and techniques he describes in that book can be used in other languages (and probably should be!) but F# is particularly well suited, thanks to its type system, to domain modeling. I think this fact alone makes F# a first-choice language for business applications.</p> 150 151<h3 id="net-framework-and-ecosystem">.NET framework and ecosystem</h3> 152 153<p>Lastly, we come to the ecosystem. Without tooling and a full library of useful packages, a language is not going to get much use, and many great languages are left in this condition. But F# is built on the .NET framework and runs on .NET Core, which is open-source and available for all platforms, and backed by the full resources of Microsoft. Furthermore, F# comes with the ability to easily consume the vast collection of .NET packages, mostly written in C#, that have been collected in the Nuget package manager and elsewhere. And on top of that, F# has what is effectively an interdimensional portal into the object oriented world that allow you to create and use real C# classes and methods inside F#.</p> 154 155<h3 id="so-f-is-the-best">So F# is the best</h3> 156 157<p>Choosing to build with a functional programming language, despite the many benefits, can feel like a risky move. Functional languages can often get a bad reputation of being hard to use or poorly supported. But F#âs pragmatic approach gives the richness of a functional language with easy portals back into C# where desirable. And it is backed by the full variety and legacy of the .NET ecosystem. F# c
157ontains the very best mix of functional programming features including functions as first-class objects, immutable data by default, algebraic data types, and many more that I havenât mentioned. For a deeper look at five of these F# features, check out David Inmanâs clear and detailed article <a href="https://www.sep.com/sep-blog/2019/10/03/best-reasons-for-learning-f/">Best Reasons to Learn F#</a>.</p> 158 159<p>Comparing F# to all the other languages is outside the scope of this article, but I believe that this unique set of features clearly positions F# in the elite group of best languages. I would enthusiastically use it on a new piece of software. In my very good and not flimsy opinion, it is the very best programming language.</p> 160 161 162 163 <div class="frameworks"> 164 <hr /> 165 <ul> 166 <li>Known to apply to:</li> 167 168 <li><code>F Sharp</code></li> 169 170 <li><code>.NET</code></li> 171 172 </ul> 173 </div> 174 175 176</div> 177 178 <div class="footer"> 179 <div class="disclaimer"> 180 <p> 181 The postings on this site are my own and don't necessarily represent my 182 employerâs positions, strategies or opinions. 183 <br /> 184 © 2019 Ryan Clarke — Built with <a href="http://gohugo.io/">Hugo</a>. Theme based on <a href="https://github.com/swanson/lagom">Lagom</a>. 185 </p> 186 </div> 187 188 189
189<script>var clicky_site_ids = clicky_site_ids || []; clicky_site_ids.push(101219778);</script>
189 190
190<script async src="//static.getclicky.com/js"></script>
190 191 <noscript><p><img alt="Clicky" width="1" height="1" src="//in.getclicky.com/101219778ns.gif" /></p></noscript> 192 193 </div> 194 </div> 195 </div> 196</body> 197</html> 198
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.