We use event sourcing in many sub-systems of our application to store data. We serialize the data of the events as JSON and store the JSON in the database. To ensure that we don’t change the structure of these event data and by this introduce backward compatibility issues, we use Verify to snapshot test the event data.
Today’s random F# code: Code Structure
Myths about F#: We tried FP in C#, and it’s unreadable! Yes, but that’s where F# shines.
Just today, I read on a social media platform that the author doesn’t like that most programming languages incorporate more and more functional features. The post was accompanied by a short example of pattern matching in C# using some of the features introduced in the latest updates. More generally, I hear and read repeatedly from people that tried to write code in a more functional programming style in C# but weren’t happy with the resulting code. The code was just too hard to read...
Myths about F#: Imperative code is simpler than functional code! No, not at all, but you are more used to it.
A myth about F# that I hear repeatedly is that imperative code (e.g. loops) is simpler than functional code (e.g. folds). On one hand, simplicity is very subjective. On the other hand, simplicity is mostly determined by familiarity.
A typically used example is something like the following:
Simple:
Difficult:
Let’s see what’s wrong with this myth.
Today’s random F# code: Regex Active Pattern in API Validation
Myths about F#: F# is for FP, C# for OOP! No, F# loves OO.
Sometimes when I talk to C# developers about F#, they say they don’t want to switch to a functional programming language. When I reply that F# is my favourite object-oriented programming language, they look a bit puzzled. They typically think that F# is for functional programming, while C# is for object-oriented programming. And this is wrong!
Today’s random F# code: Nice test error messages with Unquote and Diffract
When comparing complex data types in unit test assertions, for example, records with nested lists containing discriminated unions, the error messages quickly get hard to understand. How to spot a single difference?
That’s why I mixed Unquote and D-Edge.Diffract together.
An example unit test from our codebase:
Myths about F#: F# has too many operators! Many, but not more than C#.
“F# code is full of operators, making the code unreadable.” said a C# developer to me once. And he is not the only person to dislike F# for its operators – not specific operators, but the sheer number of operators there are.
Before continuing reading, take a moment and guess the number of operators in vanilla C# and F#.
Using SRTP-Active Patterns in F#
A few days ago, we embarked on the process of tidying up a particularly complex piece of code. It involved moving typical frontend code to the backend, as it was becoming too cumbersome and we felt more secure writing it in F# in the backend. (more on that in another blog post) After much of it was rewritten, we ran into a pattern matching construct that threatened to contain a lot of code duplication. To increase readability, we wanted to use an Active Pattern. The problem was that the...