We're sorry but this page doesn't work properly without JavaScript enabled. Please enable it to continue.
Feedback

Building stuff that works with F#

00:00

Formal Metadata

Title
Building stuff that works with F#
Title of Series
Number of Parts
133
Author
License
CC Attribution - NonCommercial - ShareAlike 3.0 Unported:
You are free to use, adapt and copy, distribute and transmit the work or content in adapted or unchanged form for any legal and non-commercial purpose as long as the work is attributed to the author in the manner specified by the author or licensor and the work or content is shared also in adapted form only under the conditions of this
Identifiers
Publisher
Release Date
Language

Content Metadata

Subject Area
Genre
Abstract
Many standard F# libraries and tools, including the compiler itself, are developed as open-source and have a large number of contributors. To successfully build such projects, you need to be serious about your craft. This includes comprehensive testing, using automated build tools, continuous integration, as well as creating great documentation and tutorials. In this talk, I'll talk about what I learned as an open-source F# contributor.Along the way, we'll look a number of risk-free ways of introducing F# into your workflow: - How to use F# Interactive for explorative programming and writing code that works on the first try - Using FAKE - an F# build tool - to automate everything in your build process - Writing readable unit tests with F# and using FsCheck for property-based testing - Generating great documentation using F# Formatting toolsIn summary, this talk is a walkthrough covering some of the software engineering aspects of programming that have been working extremely well for the F# open-source ecosystem. After the talk, you'll have a good idea how to use some of the techniques in your daily job - but you may as well become an F# contributor!
BuildingSoftware developerComputing platformOpen sourceStack (abstract data type)SoftwareLink (knot theory)SoftwareMereologyQuicksortPoint (geometry)Different (Kate Ryan album)Real numberAlgorithmSoftware developer40 (number)SpacetimeVirtual machineWindowShared memoryJSONXMLUMLComputer animation
Software developerPairwise comparisonQuicksortSpacetimeLibrary (computing)Point (geometry)Basis <Mathematik>TwitterOpen sourceSource codeSoftware developerCompilerObservational studyMathematicsProjective planeVisualization (computer graphics)Open set
Software developerInternet service providerFiber bundleScripting languageCompilation albumFlow separationQuicksortProjective plane1 (number)Text editorHoaxCompilerLibrary (computing)Open sourceCore dumpDifferent (Kate Ryan album)Internet service providerInstallation artCodeINTEGRALVisualization (computer graphics)Type theoryRight angleArchaeological field surveyBit rateService (economics)Prisoner's dilemmaEvent horizonIntegrated development environmentPlug-in (computing)Shared memoryData managementContext awarenessFile formatArrow of time
Software developerOpen sourceNumberProjective planeCategory of beingBuildingOpen sourceRevision controlType theorySoftware developerAddress spaceQuicksortSoftware testingLibrary (computing)Source codeProcess (computing)Lecture/Conference
Library (computing)Software developerInternet service providerOpen sourceAutomationLibrary (computing)QuicksortOpen setOpen sourceSoftware testingTorusPoint (geometry)Source codeWritingSource codeComputer animation
Scripting languageUser interfaceSoftware testingSoftware developerSoftware frameworkIntegrated development environmentComputer fontQuicksortMereologyCodeResultantSoftware testingPrototypeScripting languageCodeProjective planeLibrary (computing)Heegaard splittingVideo gameSlide ruleComputer animation
Software developerPasswordString (computer science)Fluid staticsASCIISpacetimeLengthInterface (computing)Single-precision floating-point formatFeedbackMultiplication signComputer fileCASE <Informatik>QuicksortNamespacePower (physics)Scripting languageUnit testingLengthBitBoss CorporationWordValidity (statistics)Physical lawProjective planeLibrary (computing)Software testingState of matterFunctional (mathematics)Server (computing)Set (mathematics)Flow separationSampling (statistics)SubsetLink (knot theory)Exploratory data analysisSocial classCodeElectronic mailing listException handlingLambda calculusExecution unitHash functionPasswordComputer animationLecture/Conference
Software frameworkSoftware testingSoftware developerScripting languageManufacturing execution systemHecke operatorMereologyDemo (music)Operator (mathematics)Revision controlDifferent (Kate Ryan album)Visualization (computer graphics)Data managementComputer filePlug-in (computing)BitProjective planeSoftware testingWeb 2.0Library (computing)Execution unitComplete metric spaceConfiguration spaceOcean currentInternetworkingValidity (statistics)Source codeLine (geometry)Right angleGame controllerComputer animation
Software developerLengthSoftware testingSoftware frameworkPower (physics)Mountain passEquals signCASE <Informatik>Social classUnit testingBitWrapper (data mining)Functional (mathematics)Module (mathematics)CodeInformationExecution unitSoftware testingBuildingWindowLine (geometry)LengthMessage passingValidity (statistics)Power (physics)QuicksortSoftware frameworkRight angleEndliche ModelltheorieSurfaceVariable (mathematics)RepetitionGroup actionShooting methodAttribute grammarEstimatorPhysical systemComputer configurationComputer animation
Software developerSoftware frameworkSoftware testingScripting languageExecution unitEquals signInteractive televisionLengthPower (physics)Complex (psychology)Software testingBitCodeWindowHash functionStructural loadValidity (statistics)Visualization (computer graphics)Power (physics)Normal (geometry)Software frameworkImplementationInterface (computing)Interactive televisionQuicksortPasswordLine (geometry)WordSocial classComputer animation
Software developerSoftware frameworkSoftware testingScripting languageLengthEquals signConvex hullDifferent (Kate Ryan album)Software testingCategory of beingPoint (geometry)Line (geometry)CASE <Informatik>SpacetimeoutputValidity (statistics)LengthPasswordImplementationSoftware bugComputer configurationCodeDot productSampling (statistics)Task (computing)SpeciesLogicComputer fileSystem callAddress spaceString (computer science)Computer animation
Equals signLengthSoftware developerExecution unitLimit (category theory)String (computer science)Fluid staticsWeb pageASCIIExecution unitLogicSoftware testingImplementationRight angleQuicksortNormal (geometry)CASE <Informatik>Category of beingVirtual machineException handlingHoaxCodeInfinityoutputLibrary (computing)Formal languageSpeech synthesisInsertion lossMultiplication signSubject indexingError messageShared memoryLengthRoundness (object)Functional (mathematics)String (computer science)SpacetimeConfiguration spacePointer (computer programming)WindowArrow of timeDoubling the cubeRandomizationTime seriesComputer animation
Software developerSoftware frameworkSoftware testingScripting languagePasswordDemo (music)Element (mathematics)Software testingLibrary (computing)BuildingSystem callPasswordCartesian coordinate systemWeb applicationApplication service providerWeb 2.0Graphical user interfaceDemo (music)Wrapper (data mining)CodeRepetitionDevice driverDirected graphComputer animationLecture/Conference
Software developerDemo (music)Graphical user interfaceElement (mathematics)Element (mathematics)TouchscreenSoftware testingRight angleWindowInteractive televisionSet (mathematics)Asynchronous Transfer ModeUnit testingBitRepetitionForm (programming)Server (computing)Reading (process)MathematicsException handlingExecution unitComputer animation
Element (mathematics)Demo (music)Software developerGraphical user interfaceSoftware frameworkSoftware testingScripting languageLibrary (computing)Software testingRepetitionQuicksortPoint (geometry)MereologyVisualization (computer graphics)Revision controlPlug-in (computing)BitScripting languageProjective planeWeb browserWebsiteComputer fileINTEGRALComputer animation
Software developerVisualization (computer graphics)Library (computing)Template (C++)Projective planeQuicksortOpen sourceLibrary (computing)Software testingMereologyType theoryReading (process)Source codeWordWeb browserWeb pageComputer fileScripting languageMotion captureSoftware developerCodeSource code
Software developerScripting languageFunction (mathematics)Motion captureComputer fileWeb pageFile formatCodeSource code
Software developerData managementCellular automatonWeb pageProjective planeComputer fileTraffic reportingSoftware repositoryLecture/ConferenceSource codeComputer animation
Software developerMultiplication signProjective planeScripting languagePasswordWindowAuthorizationDescriptive statisticsValidity (statistics)Staff (military)Point (geometry)Data managementSource codeVideo gameMathematics
Software developerGraphical user interfaceElement (mathematics)LengthScripting languageProjective planeComputer fileCodeImplementationPasswordValidity (statistics)Default (computer science)DemosceneSoftware testingLibrary (computing)Data managementBuildingComputer animationSource code
Software developerData managementSource codeLocal GroupEquals signPower (physics)BuildingProjective planeOpen sourceLibrary (computing)AuthorizationCodeBitSet (mathematics)Revision controlQuicksortComputer fileVideo gameGroup actionDifferent (Kate Ryan album)NumberRight angleSuite (music)Special unitary groupLecture/ConferenceComputer animation
Software developerData managementPasswordSpacetimeTrigonometryFluid staticsString (computer science)QuicksortRight angleLibrary (computing)Type theoryElement (mathematics)Uniform resource locatorValidity (statistics)Electronic program guideElectronic mailing listMereologySampling (statistics)Social classSolid geometryCodePower (physics)Positional notationComputer animation
Software developerCASE <Informatik>BuildingScripting languagePay televisionVisualization (computer graphics)DataflowComputer fileFunction (mathematics)PasswordValidity (statistics)CodeLecture/ConferenceComputer animation
Software developerSpacetimePower (physics)PasswordFatou-MengeBitValidity (statistics)Computer fileScripting languageComputer animation
Software developerPasswordLibrary (computing)Power (physics)Scripting languageComputer fileResultantRevision controlComputer animationLecture/Conference
Software developerLengthResultantBitValidity (statistics)PasswordComputer fileStructural loadQuicksortSampling (statistics)Line (geometry)CodeScripting languageDemo (music)Computer animation
Software developerSample (statistics)PasswordGraphic designFunction (mathematics)MathematicsInteractive televisionBuildingUnit testingProjective planeExecution unitComputer animationSource codeLecture/Conference
PasswordSoftware developerLibrary (computing)BuildingData managementSoftware testingFrictionOperations support systemDemo (music)Source codeTask (computing)Sampling (statistics)InformationType theorySoftware testingShared memoryLibrary (computing)Web 2.0Category of beingLink (knot theory)Point (geometry)InternetworkingRevision controlMultiplication signNumberFunctional (mathematics)Projective planeNP-hardElectronic mailing listHoaxWave packetOnline helpOpen sourceSlide ruleProcess (computing)Demo (music)QuicksortPhysical lawExistential quantificationComputer animationLecture/Conference
Transcript: English(auto-generated)
Cool. Well, thanks for coming. It's amazing to see that actually people still come even though it's 5.30, 5.40.
So this talk, I have various titles for the talk. And the one I kind of like but I decided against it is doing boring things with F sharp because nobody would come. But really it is a talk about doing boring things with F sharp. Because when you're doing software development, the real fun part is coming up with solving
the interesting problems, but there's really not that many interesting problems. You don't come to the work every day saying now I'm going to write this new amazing algorithm
that nobody ever did before. And a lot of the stuff that I ended up doing recently was sort of how do I make everything work correctly and automate it. And it turns out that this is actually a thing that you can do very nicely with F
sharp, and it's also been a very sort of successful way of introducing F sharp into new companies. So if you're curious about F sharp, then using it for some of the boring things is actually a really nice way to get started with it because you have a safe place where
you can learn it, and it will make the boring things more fun. Before we get started, how many people have heard about the F sharp software foundation or have visited F sharp.org? It's quite a few. If you haven't, F sharp foundation is now actually an official registered US nonprofit
that tries to bring together all the community, all the companies, basically everything that's happening in the F sharp space, and F sharp org is just a nice entry point. So if you're curious, you want to learn more about F sharp, you can go there.
There's also instructions how to get F sharp installed on different sorts of machines, how to install it on the Mac with Mono, how to install it on Windows, and so on. So it's just a nice entry point, and you'll find links to other resources from there as well.
Now, the talk is sort of partly inspired by my experience with F sharp open source, and F sharp has fairly long history. It started as a Microsoft research project, and in the early days, it didn't have permissive
license, but it still came with a source code that you could build, and then I think from 2010, it was actually open source under the Apache license, so there were people from the Mono team contributing to it as well. And the visual F sharp tooling, so that's actually what you get in Visual Studio, now
also accepts contributions, so if you're not happy with Visual Studio, you can fix it. And this is a tweet from shortly after that was announced, and Microsoft started taking
back pull requests from the community, and it turns out that there's quite a lot of people in the F sharp community who want to contribute, and the sort of biggest new thing in F sharp 4.0 is that most of the changes actually come from the community
rather than from Microsoft. So there's sort of a lot of open source development happening these days around the compiler and the libraries, and the home for this is on GitHub, but there's also
another thing, which is that the F sharp community builds quite a lot of open source projects, so if you sort of install Visual Studio and you create a new F sharp project, it doesn't really contain anything, it's just an empty console, but a lot of the
core libraries for F sharp come from the community, so this is just sort of a picture with a couple of them that I picked, the red ones on the right are related to data science, so there's things like F sharp data, which is a library with type providers for accessing JSON, calling REST services, the green ones in the middle
are sort of tools, so pocket is a tool for dependency management, fake is a built tool, I'll show you both of these, and the orange ones or the yellow ones on the left, there are various integration for F sharp, for Xamarin Studio or Monodevelop,
there's Ionite, which you could have seen earlier, Emacs plugin, any Emacs people in the room? Win people? We have that too. And the sort of really interesting thing here is that the community is building quite a
separate open source project, but if you look at the sort of context, they really share a lot, so, for example, all the editor tools on the left, they use something that's called the F sharp compiler service, which is like a Roslyn for F sharp, the tools
on the right, they, well, I'll show you the picture, because when I was putting this together, I tried to draw all the dependency arrows, and then I got tired, so it doesn't really show all of them. But the interesting thing that I think is sort of different in the F sharp world is
that there's a lot of projects that really depend on many other things. So if you look at, say, the Ionite plugin, Atom, that depends on FunScript, which translates F sharp to JavaScript, it depends on another application, which provides the tool tips,
which then depends on the F sharp compiler service, which is sort of the compiler itself. Or things on the left here, FSLab is like a data science environment, depends on F sharp formatting, which nicely presents F sharp code as HTML, which depends on the
compiler. So you'll end up with these sort of really interesting projects that have like 20 other dependencies, and they're all open source projects, so the fact is that the community had to learn how to make this all work, because open source is fun, and I really
like doing it, but there's all these other things, like when you're doing open source, you really have to make sure that it keeps working, because if there's like crazy number of dependencies, if I break something, it will just break all the F sharp projects.
So I think that the community has been developing and learning how to do this, how to address things like testing, how to be able to build and release. So and I think this is, all of these are things that affect everyone as a software developer.
It kind of hurts more in the open source world, where if someone sends me a pull request for my open source project, and I'm currently busy doing some work, I can't really say, yeah, this is really nice feature, I'll get back to it in like three weeks, and then
maybe release a new version if I have the day, because it's really hard. So on the building and releasing side, most of the F sharp projects actually work so that you can go through the whole process with just one command. You just type build, and it will do everything, and that's what I want to show you.
Another thing that turns out to be important is documentation, and I think this has a bad reputation because in C sharp you were told you should always write the XML comments, and then you have property name with a comment gets or sets property, gets or sets name.
But one thing that I think is working pretty well in the F sharp ecosystem is that there's tutorials, so if you go to some project, and I'm going to use this one as an example,
so it's an F sharp data, data access library. There's a couple of examples here, and for every sort of important topic, there's just one tutorial that describes how you can do it, and that's something that for open source is really important, but I think if you can write sort of short tutorial about
internal tools, internal libraries, that's something that people really appreciate. So how do we solve it? Well, I think that the general sort of theme of the talk is if something requires a lot of manual boring work, then people will not be doing it, and so what I want to
go through is a couple of tools that you can use to make the boring things easier so that the pain point sort of is removed, and you only have to do the more interesting
thing like come up with examples for documentation or write the clever test. And this is probably the end of my slides. The rest will be live coding, and I split it into two parts. The first part will be mostly about testing, so how can you use F sharp to add some testing
to your existing projects? And I'll keep this here as an outline, so the first thing I'll do is when I start using when I start writing some code in F sharp, I very often use scripts, which
is sort of a REPL-style environment where you can run things, see what the result is, and I use it to explore libraries, to prototype my code, and it's also something that you can later turn into tests.
And I'm going to use a sample library that I wrote. This is actually a C sharp library, so I did write some C sharp. I still haven't forgotten how to do that. And it's a very silly library to do password validation, and I even split it in four different
C sharp files, so I have this interface I requirement that represents some sort of requirement on the password, and it checks whether the requirement is satisfied. Then I have a simple validator, which just takes a function, so you give it a lambda
function in C sharp because I don't want to create that many classes, and then I have this nice library of validators, so I get lower case and upper case ASCII characters, and then I have a length requirement with checks that password has length of eight
or more. I have lower case and upper case requirement that checks whether the password contains all of the characters. So it's pretty basic. The last bit I have is this power validator that takes a collection of other requirements
and checks that they all are satisfied. So I'm going to build it. Does it actually build? I think it did build. And one of the questions that I often get is what's the easiest way to start using
F sharp in your projects, in your C sharp ecosystem? And the answer is right-click, add new item, and F sharp script file.
So what I added here is just a single script file that I can now use to write a little bit of F sharp code, and in pretty much all of my F sharp projects, I do have some script file that I just use for testing things, for trying out ideas, see if something behaves the way I think it does.
And what I'm going to do here, so the script file is sort of separate entity, and what I can do here is reference the library that I built. So now I have to find where the compiled DLL is.
And what this hash R does in F sharp is that it says reference this DLL, and then you can start using things that come from that library. So now I can open the namespace, that's it, and I can start playing with my library,
create a length requirement, and let's see, so length is satisfied, foo, that should be false, and if we say foo bar, that's not long enough, this, then it's true.
So this is really a nice way to sort of, it's not even like unit testing, I'm just getting the quick feedback in another way in a script file, I run everything by hand. And the power validator, that's another fun one. So I can create a power validator and give it a list with length requirement, and let's
go for one more, so uppercase, and then if I say pow dot is satisfied, so something
long but without uppercase letter, that says false, this says true. So this is already a nice playground that you can actually use just when you want to test how something works, and I know many people who sort of use this kind of, use
edge serve in this kind of exploratory way rather than using something like linkpad. The other thing that you can do here is that you can actually, when you write these scripts, they're kind of like tests, except that you have to run them by hand. So the obvious next step is to take them and turn them into proper unit tests.
So that's what I'm going to do next. And what I'm going to do is add a new project, F sharp library, and I'll call it, I clicked on something on which I shouldn't have clicked, I guess.
This is the fun part with live demos. So what exactly is Visual Studio doing right now? Nobody knows. Let's start another one. I also have two different versions of Visual Studio installed, so I'm sure we'll manage.
Yeah, this is the stuck one. Let's go for this one. So I'm going to add a new project, and I'll select a library, and let's call it password validator dot tests, and it creates a little library.
The other thing I'm doing here, and I'll say more about this later, but F sharp, or this isn't really F sharp related, but what I'm using here is a tool called pocket, and pocket is a dependency management tool built on top of NuGet.
It lets you reference NuGet packages, but it resolves some of the pain points that people had with NuGet. So what I'm going to do here, I already have pocket dependencies file, which is where you specify what libraries you want, and I need a couple of libraries.
The first one I want is called FS check, and if your internet works, this actually gives you auto completion as well, and I'm going to be using FS unit, which is another testing tool that's available for F sharp, and I'll add one more called
canopy, which is a tool for doing web testing. So now we are going to use three different testing tools, three different libraries that are there for F sharp, and in this file, I just have to
specify what are the dependencies I want, and then if I want to actually add the reference, so the first thing I need is to reference my C sharp library here, and then the other bit I need is to add pocket dot references, which is a
file where you specify which of the dependencies of the solution should be added to this project. So here, we need FS unit, FS check, and canopy, and that's it.
And then I can save the whole project and run, install the packages. Oh, I'll actually do one more thing before we move on, which is that I'm going to add up config to this project, and now I can, and there is
actually a visual studio plug-in for pocket, so you don't have to do everything from command line if you don't feel like. I can run the install command, and it runs the installation, adds all the references, now I have them all here, and it also produces a file
called pocket dot lock, which is where it specifies what are all the transitive dependencies that it downloaded, and this file also determines what are the current versions of things. So the way you typically use it is that you check this into your
source control, and then everyone who's going to build your project will get the same version of everything. And you can notice that in the pocket references, I just have to say FS unit, for example, even though FS unit actually depends on any unit,
and I don't have to worry about that. So that's one thing I really like about it, but I'll say more about pocket later on, and now that we have everything loaded, we can actually start writing some tests. So this Visual Studio is still dead. No, it's back, it's back.
I'll just ignore what I did here. I just want to copy these. All right. So it helpfully created class in case you wanted to put your unit tests in a class, but in F sharp you can actually do it in a little bit easier way, because in F sharp, when you say let,
you can use this for defining local variables, but also you can use it for defining functions in a module. So if we call this module and we say let's do test one, this is actually defining a function, and function will be compiled
as a static method, and any unit and X unit, and I think the Microsoft one does it as well these days, they will pick up static methods too. So if we add any unit framework and the test attribute,
then this will pick up the tests from here. So you don't need the class and you don't need the member and you don't need to build sort of complicated syntax. The other thing is, and this is where we have audience participation,
so how many people here prefer this style of writing your names? Let's see. Length works on foo bar zoo. Who likes Pascal case for your tests?
Three, four, five maybe. Who prefers, and then I have to do it. This is boring. You can get the picture, right? Who prefers this? I think this is slightly more. Any other favorite options?
So in F sharp, there's actually a feature where you can put double back ticks around a name and it will allow anything in there. So now I can say length requirement holds for foo bar zoo, but not foo.
This is the longest line I'll ever write in this talk. And then power validator can combine length and upper.
All right. And when I first saw it, I was like, whoa, this looks really weird, right? But when you're defining a test method, you're never going to call it
from anywhere else in your code. It's just name. So it turns out that this actually works really nicely. The other thing is, so I already referenced fs unit, and this is, they call it, I think they call it F sharp unit testing framework,
but really it's just like 30 lines of code. It's a lightweight wrapper over any unit that makes it easier to write your test assertions. So if you're like me and you never know if you write assert equals, does the expected come first or does the actual come first? Then you can just use fs unit and pipe and say len is satisfied foo should equal false.
And the other one should equal true. And here, this one should equal false. And the last one should equal true. There's a couple of other things, like you can say should contain
or should not equal depending on or should throw and so on. But that's about it. So now I've just used a couple of F sharp features and things to make the syntax a bit more readable and get a bit nicer tests.
So let's try if they actually work. Oh, I can't even build it because I did break something. What did I do here? This is some assembly info. Let's rename this to test0.
Now it builds. And do the test pass. And this is where I get to show you my new Windows trick. You can actually see the nice names that we entered in the backticks in the test runner.
So when you're going through the tests, it's just visually much easier to parse this. And my two tests did succeed. So far, this was actually really easy. We've used F sharp, but the code, you don't really need to use any complex F sharp features there.
It's just writing tests. And we've used F sharp just because it makes the testing a bit nicer, makes writing the code a bit easier, and so on. There's a couple of other nice F sharp features. And I'm actually going to do one more trick here.
So I said before that the REPL is really nice for trying things out. You can actually set up your tests so that they work in both the test runner and in a REPL. And to do that, I just need to find... I think this is what I want.
No, this is not what I want. When my Visual Studio was crashing, it also reordered my hints. This is what I want. So I added this hash if interactive, which just says, do this, load the references only when I run it using alt enter.
And when I select this piece of code and do alt enter, you can see here in the F sharp interactive window down there, we loaded all the dependencies, and then I can run the tests from here as well.
And if it doesn't throw, then it worked. So the next thing I wanted to do is to show you another nice F sharp feature, a little detail that makes testing a bit easier. So if we wanted to do some, let's say, test that if we have a validator that always fails,
the power validator will also fail. And the normal way you would do it in C sharp is to use some mocking framework to mock the I requirement. And it turns out that in F sharp you can actually do it without any frameworks because there's a way to create an inline implementation of a class.
So what I'm saying here is give me an implementation satisfied that for any password just returns false. And we are going to put it in a test saying if validator fails, power validator also fails.
So this is my mock without any framework. And then power will be just a new power validator with this one.
And now I can run this in F sharp interactive just to actually, without even sort of switching to the runner, see if it works. So if we say nothing here, it says false. If we say something really, really good, it should say false as well.
And that I'll do with should equal false. So this is a nice feature where you can create inline implementations of interfaces.
It's useful in normal F sharp code, but it's really useful in tests as well. And now we are getting to the last two points which are actually more exciting. So how many people have seen a talk or have heard about property-based testing? Ah. This is almost reaching to everyone.
Property-based testing is a really nice idea. So I'll show you one more demo as well. The idea is with the tests I wrote, like the first one, say, I covered four different cases.
But that's really not very, like, it might cover a lot of lines in my code, but it doesn't really cover a lot from the space that people will be using. And I already referenced fs-check. And what fs-check does is that it lets me randomly or partly randomly
generate a lot of inputs and check that my code works on these inputs. So who thinks there's a bug in my code? Ah. You trust me. Nah. Come on. It's like five files. Let's find some bugs. So the way you use fs-check is that you can say check and let's do quick.
And here I can say something that should hold. So I can say for every string, for example, let's create a length validator dot. Length requirement. So for every string, it should be the case that length is satisfied for this password.
Ah. I'm switching to C sharp. This is true when s dot length is more than eight.
And I need some parentheses. All right. Who thinks this will work? Who thinks it will fail? Ah. Who thinks something else?
The third option. Yeah. Well, let's see. So what happened is that fs-check randomly or partly randomly generated 11 tests. And then it turned out that when it calls me with null, I fail.
All right. This is boring. So I guess we should change the logic. Like, this is something that will fail in both the implementation and the test. So this is very wrong. But if we say so if s isn't null, end.
So now I fixed my test, but it still reveals an issue in the implementation. So if I go to my sample validators, then indeed, this is wrong. Because it should say pass isn't null, and it's more than eight.
So if we recompile it, let's just see if we fixed it. All right. Now it works.
I need to do double end here. But now it works without warnings. So this is kind of an obvious one. And fs-check is really good at finding these sort of common issues like nulls or nuns.
I've used it on one data science sort of time series analysis library. And it was continuously discovering errors that I did when dealing with null, plus infinity, minus infinity, the smallest double, largest double, and all these things. So for that, it's really powerful.
So let's try... Well, let me first wrap this. So if I use throw on failure, then it will actually throw an exception when it fails. And then you can wrap it in a nice n-unit test. So length logic works.
I'll stop writing really long test names, because then we wouldn't get anywhere. The other one that we can test is this upper requirement. And so the logic of the upper requirement is that
it succeeds if it contains one of these letters. And here I'm just going to write the same implementation in F-sharp. So let's create set with all the letters that we are checking for. And you can actually configure various things of how fs-check runs.
So one common question is, can you make sure that it always produces the same random inputs? Yes, you can. You just have to give it the same random seed. Here I'm also telling it to run 1,000 tests rather than 100 tests.
And then you can say check one, which is just another method that takes this config. And then... So what I want to do here... For every string, we get... Oh, the C-sharp arrow again. For every string, up is satisfied.
So this is my actual. And what am I expecting? I'm expecting that this is satisfied if there exists a letter
such that letters contain that letter. So here we don't really have to worry about the null,
because... No, we still do have to worry about the null. So what can I do? I don't care about the null, but there is a nice feature where you can say if s isn't null, then... And put another function here. So here what I did is that I just said run the test only if s isn't null.
So we are ignoring nulls for the moment, and we are just checking the actual logic. All right, who thinks this will work? Do you want to see my C-sharp code for comparison? Here's my C-sharp code.
Who thinks it will fail? Who thinks it will fail because I'm asking? Ah, right. It does fail. So what's happened here? It actually had to generate 180 tests, and then it came up with i.
So for i, well, I do have i here, right? I do. And I do have i here, so what's wrong?
Anybody's from Turkey. It turns out, and I turned... I had to do this thing to my machine to actually show you this. So I don't know what day it is, but... It turns out that in Turkish, when you call toUpper on lowercase i,
and I can show it to you here, they have uppercase i with dot, which is some other Unicode character. So my code was actually wrong, and FSCheck find that out, but only after running 180 tests.
And if we change it to upper invariant, then this is where it will turn the lowercase i into the normal uppercase i. So this is the kind of subtle issues that the properties can discover, because I did run it previously on, like, foo, bar, and things,
but it would never occur to me that I should put in i. And I definitely can feel the pain of the people living in Turkey, because in Czech language, we have a letter that's written as CH,
so if you switch your windows to Czech and you do CH index of H, it returns minus one, because CH is one letter. So those kind of issues are the most annoying issues, right? And they're really impossible to discover if you don't use something that just tries a lot of really weird cases.
So let me just package this as a test, and let's see if I fixed it. So now this is a test. If we rebuild it and I rerun my tests, then see what happens.
And it all works. So this was looking at basic testing, but there's actually another nice FSharp library in the testing space,
and I'm going to show you just one little example of this, if I can find it, of course. There we go.
So aside from building the CSharp library, I did build one more thing in CSharp. I built a simple web application using ASP.NET,
where you can validate your passwords. I'll collect all of them and then sell them to someone. So let's see if we do goo-hoo. That's not good, but this is definitely good.
Is it good? It is good. And now we want to test the UI of this application. And one nice library that's available for FSharp is called Kanopy, and it's a lightweight wrapper over Selenium. And I already have a demo here, so if I go back to my tests,
I can go to web tests, and this contains the same loading I did before. What did I do wrong? I probably didn't build something, as always. So did this all load?
Oh, I'm not using fs unit. And then, so this is the code that I'm doing here to test things. I just set where's my Selenium Chrome driver, and then I can go and say start Chrome, and somewhere over here, Chrome appears.
Then, this is where it gets tricky. Then you can say go to a URL, and this is where having something like REPL and FSharp Interactive
is really useful because you can sort of see what happens rather than running it, seeing that it didn't work, and then going back and trying to figure out what's wrong. So the next bit I'm doing here is this syntax, where I say set the value of the element on the left to the thing on the right.
And you can see, indeed, it changed the text, and then you can use element to find elements. So here, I'm just locating the button inside the form, and then I click on it, and it did call it, and then we can find this element alert,
read the value, and check that it contains success. Or we can go back, set it to AA, click the button, and check that it contains error. If I do something wrong, then right here,
you can see an exception, and that will just get reported as a unit test failure. And then we can quit Chrome and go back to nice full screen mode. And if I run the tests from here,
this is where it gets creepy because a window appears and starts doing things. So I think that the sort of interesting point here is that the REPL and the lightweight syntax can really do quite a lot for you if you're just doing things like testing.
And the canopy library is pretty amazing. There's actually an amazing talk that Stefan Forkman did. He's the creator of Puckit, and it turns out that in there... So they're building the Visual Studio plugin.
It turns out that if you want to release a new version of the Visual Studio plugin, you have to go to the Microsoft website, click a bunch of buttons in the browser, upload the new VSIX file, and click a bunch of other buttons. And they're very agile,
and Puckit gets a couple of new releases every day, so this obviously isn't very convenient. So what they did is that they wrote this amazing canopy script that, as part of their continuous integration, goes to Microsoft website, clicks on things.
I think Microsoft also has some hidden where you have to put a magical value, and then uploads it there. So you can use it not just for testing but also for automated things that somebody did wrong. All right, so that was the first bit.
Now, I still have, I think, 15 minutes, so what I want to show you is another really nice tool, and it's called Project Scuffled, and I think Paul mentioned it earlier. So what this is sort of doing is that
it turns out when you're doing open source libraries, you have to have all the same things over and over again, from running tests to releasing NuGet packages, updating documentation, updating your release notes, all the boring things.
And Project Scuffled is basically a template that you can use and customize for whatever project you're doing. And this is just an example of a page you get. So one part of the Project Scuffled
is that it lets you build nice documentation, and this is one library that uses it, and it's a library for charting, so you get a nice page with some explanation. There's the source code. One nice thing it does
is that it type checks the source code, and it can actually show you types, so when you're reading FSharp code, it's kind of useful to see the types of things, and this brings it to the documentation in the browser. And who thinks that I had to start Word to generate this?
Ah, nobody. Yes. As a developer, I definitely don't want to write my documentation in Word, so the way this works is that all you have to write to get that nice documentation is actually an FSharp script file for FSharp or Markdown document.
So this is an FSharp script file that generates the charts, and then I have a bunch of comments in there with the stars, double star, which the tool looks at. It extracts it, formats it as nice HTML, and the very shiny thing is here.
Before the chart of candlestick, it says something like define output prices, and then it says included prices. So what this is doing is that it actually, the tool parses the script file, generates the HTML,
runs all the code snippets, captures their output, and embeds the output in the page. So the chart, I didn't have to create it by hand. It was just inserted automatically. And when you look at other pages, there's just lots of nice examples, and the tool removes most of the pain
of producing this nice documentation. So if you want to use something like this, the easiest way to get started is to actually go to the project scaffold page, and you can just fork it or fork it.
You can download the zip file because you probably don't want to have everything as a fork of the same repo. So you can download the zip file, and then when you have it, you'll end up with a folder that just has a bunch of files in there.
I'll explain what they exactly are if we go there. So this is scaffold source. There's a build script. There's one for Linux with .sh, one for Windows. If I run it, it downloads the dependencies.
So here it's just downloading the Puckit package manager, and then it downloads tool fake, which is the tool that's running fserp build scripts. And when it downloads it, it downloads some other tools for generating the HTML and stuff.
And the first time you run it, it actually just asks you for some details about your project. So I can say this is password validator. I have to write all these different descriptions that will be used for generated NuGet packages, so I'm just going to do jjj, I think.
kkk. Author is me. Tags are none. GitHub is Microsoft. They should be using it. There is actually one Microsoft research project which uses it. Initialize git repo no.
And it generates, like, renames the project and stuff to get started, and it builds it. So what I'm going to do here is, the way I usually use this is that I just start with,
start by writing my code in a script file, and when I think it's something that I should polish and share, I use the project scaffold, and then I just run the tool, let it generate the default template, and put my actual implementation in there.
So I did that behind the scenes, and then you'll end up with a project where, I really, I still have all the things I did before, so I have my password validator and password validator tests, and then it added a bunch of other things.
And once we are in the project scaffold, you basically get three things from there. You get nice dependency management, nice documentation tools, and nice build and releases tools. And I still have my library actually written in C sharp,
so if you're author of a C sharp open source library, you should still be using this, because you'll get really pretty documentation that's easy to generate and easy to release. So I'm going to keep my C sharp code here, and what did I want to show you?
Oh, so my packet dependencies, which is what you sort of get there, now got a bit more complicated, because packet also lets you distinguish between different groups of dependencies. So here we have one set of dependencies that we need to build things,
one set of dependencies that we need to test things, and within the group, you'll always get sort of compatible versions of everything, but you can have multiple groups where different groups have, say, different version of some dependency. So, but still, you just have that single file
that specifies your dependencies, and you have packet lock, where it says what are the versions that it installed for the different groups. The other thing that's really sort of minor detail, but I really like it, is that packet doesn't put your dependencies in a folder
that has the version number in there. So I can write these hashrs with just fs check slash lib, and it will always work. And probably my most unpleasantly spent day in life
was when I was working with a company where they had like 100 project solution, and I wanted to update one NuGet dependency to a newer version, because then I spent the whole day searching for the version number in all the different files where they happen to have it, including the project files,
and changing it to the right one, and then finding out why it doesn't work. So that's something that doesn't happen to you with packet. The other nice bit is how you can write the documentation. So I'm not going to write the documentation,
because you would be watching me for the remaining 10 minutes, and it would be really boring, but I'll just copy it. One thing I already did is that I added some XML comments. And to be fair, I think the XML comments are the less important part, because it's the tutorial that sort of guides you
through the library and shows you how to do it. But if you want to have really sort of solid documentation that explains everything, then the XML comments for your public important types make sense. So here I added XML comments for my power validator,
and I'll need to add one more, which I do have ready here. Is it this one? Yep. No. Oh, yeah, that was the right one. The tool can handle the sort of C-sharp,
P-style XML elements in your documentation, but what I can also do is just write that summary tag and put some markdown in there, because then it's less noisy. So here I'm saying here's some text, and then a code sample which is written in C-sharp,
and it will be able to work with that. And I have my requirement documented. Here I just put omit, which means skip it. This class doesn't matter to the reader.
Let's just not put it in the documentation at all. And for validators, I'm going to add another useful comment, and here I'm using the sort of bulleted list notation from markdown. I'm also using backticks for code inside the markdown.
All right, so I've written some documentation. If I go back here and run the build, it will recompile everything and regenerate the documentation. You can invoke these things from Visual Studio as well. This actually just calls one F-sharp script file,
but depends on what's your workflow. I'm kind of used to just have the build script around in case I change the docs. And then in my folder, in docs, output, it puts all the generated HTML.
So if I go here, here's the documentation for my password validator, and there's API reference with that code snippet that I wrote, and with the nicely formatted markdown comment that I wrote for my validators.
And the last bit I want to show you is writing the tutorials. So I think I have written these already. I don't know, I'll have to regenerate this. All right, let's run this again. So I do have already two files, but they didn't contain anything.
Or maybe I'm actually in the wrong folder. Ah, that's it. So that's why it hasn't changed as I was expecting. Let me go to my right folder.
Scuffled. So here I have two files. One is a markdown file, one is an F-sharp script file, because I want to write some documentation for both C-sharp and F-sharp. And in C-sharp, someone might...
When C-sharp gets script files and someone sends us a nice pull request to edit, we'll support that as well. For now, in C-sharp, you'll have to write it in markdown. And then this regenerated everything. So my tutorials... I'm still in the wrong folder.
No, I think I'm... Now I'm in the right folder. I'm just doing something silly. So the idea here is...
And I'll just show you the result. For the C-sharp version, you can just write your documentation in markdown, and then when you want to show a code sample, put it in there as a code sample. You can also add sort of references to another file.
So say, pick these lines from a CS file. That's one thing. In F-sharp, the nice aspect here is that this is actually just a valid F-sharp script file. So when I'm writing the documentation, I can load everything,
load my password validator, and as I'm writing the documentation and the code snippets, I can actually run it as well. So this is where the way I usually write some documentation is that I just create a script file, write some demos,
and then add a little bit of text around it. And let's see what happened with my output. So index. We go to the C-sharp. Now this is what should have happened.
So before I did the changes, I just had these two empty tutorials. If we run the build again, then it should... Oh, it did something crazy. All right, that happens when... No, I know exactly what happened. F-sharp interact...
if actually locked the DLL when I loaded it. So if we reset this, then we should be able to run the build. And the build has actually compiled the project, run my unit tests, and now it's generating my documentation.
And if we go back here, I didn't save my F sharp sample, but I did save my C sharp sample. So I do get syntax highlighted C sharp here. You don't get tool tips for C sharp because we can't easily get that information,
but you do get tool tips for F sharp. There's a bunch of other things that you can do. Not all the libraries use it, but for example, in F sharp data, it actually type checks the documentation and it fails a test if your documentation doesn't compile.
So you can't release a new version of your library if you don't fix your docs. So just to summarize, this is a list with all the libraries that I was using. And I put it in the slides just because I'll put it on the internet
and I'll tweet a link with the NDC London hashtag. So if you want to go back to any of this, you can find it there. There's like three different things for testing, the simple syntax, property testing, web testing, and then project scaffold is the sort of overall demo
project. Fake is what run all the automated magic and pocket for dependencies. And the last three points, so what should you remember from the talk? F sharp has this reputation of being good for science or hard things, but it actually works really well
for boring things too. So if you need to automate something, that's actually where F sharp has really good tools. And a lot of these tools that I was showing are used by a large number of open source projects where not only you have to be able to really quickly
release, but you also just might not have time to do it properly. So the automation there is really important. And the most important bit, this is a very risk-free way of starting with F sharp. So if you're curious about F sharp but you work in a company that does mostly C sharp,
F sharp, nobody ever gets kicked out from a job for adding more tests or adding documentation or removing the common pain points that you have. And that's one place where F sharp can be used really
nicely. If you want to learn more, so there's FBLabs around lunchtime tomorrow. You can come there and just chat with the F sharp and functional crowd. There's also a Pluralsight Corner there. And I'll be talking there or showing there some things
tomorrow in the morning. And if you want some help with doing some of this in your business, we have a F sharp Works training and consulting company. And we'll be happy to help. Thanks.