Intro

Last time, we discovered some interesting patterns with the first and last line of the diamond. Today, we'll look more globally at the diamond's shape and figure out if there's a property hidden that we could exploit.

Height equals the width

If you look closely at any possible diamond, you'll find that the diamond is quadratic or, in other words, the height is always equal to the width. We cannot see it here because of the font I use on my blog, but if you look at the number of char per line, you'll realize that it's equal to the number of lines.

Wrapping up

So far, we have three useful tests that are reducing the scope of possible errors in our implementation of the DiamondGenerator. However, there's still some way to go before we can call it a complete suite of tests and be confident that our code is unbreakable.

Every week, I share my engineering notes. They include everything I can dig up on the internet, and that is worth sharing. Last week Microsoft was hosting Build, their annual developer conference, so there's a TON of new announcements related to that. If you want my complete recap of the event, check it out here.

Intro

This year was a special edition of MS Build; for obvious reasons, it was 100% virtual. I must say that I had my doubts at first, but I'm forced to admit that Microsoft nailed it. The experience was fantastic. I liked the personal schedule builder and the rebroadcast in 3 different timezones, to only name a few things. Microsoft even took the time to ship the conference's gear to all attendees, DIRECTLY to their doorstep, what a nice touch.

There were over 600 sessions, running 24h/day continuously. Unfortunately, this recap only covers what I was able to watch. If you see something missing, don't hesitate to engage in the comments down below.

Recap

Once again, Satya had a very compelling opening keynote. He remembered everyone that in a world constantly changing, the developer is more important than ever. Microsoft is building the best toolchain to empower those developers.

No big announcements from his part, but he highlighted some critical aspects of the conference to keep on eye on:

WSL 2.0

Windows terminal

Developing applications has always been made working locally. This approach comes with some downsides. For instance, you need to have all the tools and other dependencies of the project installed locally. It tends to create snowflakes environment. How many times have you heard "Strange it works on my machine".

Microsoft is coming with a new developer experience that runs entirely remotely. CodeSpaces (formerly VS online) is a new tool that enables you to spin a complete dev environment in the cloud, on a remote machine. Then you can develop directly in your browser or connect to it with your favorite tools like VS Code or Visual Studio.

It's also now possible to spin up a fully functioning environment from a GitHub repository. It will come pretty handly to review a pull request.

Collaboration is at the heart of this new experience. With LiveShare, you can collaborate with multiple people on that remote environment like you would in an Office document. This functionality will enable a whole new way of doing pair programming, mainly since most of us are still confined at home.

That session started with some impressive statistics about the Microsoft Intelligent Cloud (a.k.a. Azure + O365).

Used by 95% of Fortune 500 companies

61 azure regions, more than any other cloud provider

MS Teams hosts 4.1 billion meeting minutes per day

These are some pretty impressive numbers, and it seems they keep growing quarter after quarter. All of these remarkable services are built on top of the same building blocks that are publically available to you in Azure; ServiceFabric, Kuberbernates, Media Services, Redis, FrontDoor, etc.

Microsoft wants to enable all the developers to focus on their application and not on the infrastructure supporting it. Speaking of which, they announced some new capabilities:

First of all, there's a new kid on the block, Blazor WASM, which brings a new alternative for devs to run .NET in the browser to build rich client applications using the technology they know. It provides complete debugging capabilities in the browser. It's also possible to debug a full loop of Front-end and Back-end altogether in Visual Studio.

It's the beginning of the unification of the whole .NET stack.
Dotnet Core and Framework will be merged in the new version .NET 5.
MAUI will be the evolution of Xamarin Forms and will bring a single UI that can target any platform like Android, iPhone, Windows, etc.

I think that session was my favorite. I must admit I'm a big fan of Functional Programming (FP) and especially F#. Microsoft showed some preview of what's coming in C# 9.0. Most of the new features are all about FP concepts and are implemented very similarly to F#, which will help bridge the gap between the two languages.

Immutability in C# classes

Simplicity, you can now skip some boilerplate code like Program.cs and the Main function

Structural equality

Better pattern matching

With all of this, Microsoft will reach an even broader audience and onboard them in the rich ecosystem of C#. It will also provide all the necessary tools for C# developers to start playing with FP concepts and write better code.

Microsoft has always invested a lot in great tooling. Call me a fanboy, but Visual Studio and VS Code are, in my opinion, the two best IDE out there. They are now bringing an improved experience within collaborative development with chat and voice integration directly in LiveShare. Git support also got a significant revamp, improving how VS handles git repositories. IntelliCode got smarter; it's now able to suggest coding standards that your teammates are following. It will save a lot of comments in PR when onboarding new developers in a project.

Infrastructure as Code (IaC) is the new trend in DevOps practices, and Microsoft knows it. They are doubling down in their IaC technology, the ARM Templates. They added a useful new parameter that you can use in PowerShell or Azure CLI when deploying an ARM template. What-if will output all the changes that will be executed if you deploy that ARM Template. A great feature to validate impacts before deploying. There are also some new features to ARM template themselves:

Auto-generation of parameter files

Intellisense for ResourceId

Improved schema

Microsoft added TemplateSpec, a new way to package, version, and store ARM templates. It will help simplify the deployment of big infrastructures.

Microsoft has built one of the biggest supercomputers on earth (Top 5), putting it at the service of AI for everyone's benefit. They are improving their models and offering it to you as a service. They are also running a lot of research projects on it.

In that session, they showed an AI that can automatically read a user requirement and write Python code that fulfills that requirement. What's impressive is that the generated code integrates flawlessly with existing code. Not only the code is accurate, but it's well written and easy to read. It's utterly indistinguishable from code written by a real person. I don't think this kind of capability will ever replace a good old developer, but it can indeed be an excellent tool to have to speed up the development of an application.

Microsoft is one of the few providers that brings the AI at the edge. It enables the use of AI in the most challenging environments that don't always have a stable internet connection. On one of the demos, the equip a robot from Boston Robotics to make it fully autonomous.

The Power platform is having a lot of traction lately. It allows super rapid application development, which requires little to no code at all. It's currently used by more than 97% of the Fortune 500. This rich environment of tools for power users is also adding new services. Power Virtual Agent is a new automated chatbot build on top of the Microsoft bot framework.

Intro

We continue our adventure trying to solve the Diamond Kata while using Property-Based testing. Last time, we added our first test, Non-empty and discovered how to use input generators. Now let's figure out the next test.

First and last line content

In the diamond Kata, the first and last line of every diamond always contains A. Such regularity is perfect for a property. Even the particular case with input A, where the first line is also the last one, respects that property.

Introducing C# Source Generators
(Phillip) - The first preview of Source Generators, a new C# compiler feature that lets C# developers inspect user code and generate new C# source files that can be added to a compilation

Blog posts

Event Sourcing is Awesome!
(Grad Berger) - A good overview of event sourcing and a pretty compelling story at the end of the article.

Intro

Now that we have all the tools installed, we are finally ready to write our first Property-Based test.

FsCheck

Creating a property-based test is as simple as tagging a method with the [Property] attribute, similarly as you would do in xUnit with the [Fact] or [Theory] attributes. If you dig a little, you will realize that there's a lot of configurable options on the [Property] attribute. You can control how many random tests will be run each time, timeouts, etc. One of them is essential; the Arbitrary property. That property will define which method to use to generate random inputs. In our specific case, we are not interested in all the possible values of char. We only care about the 26 letters of the alphabet. To scope the input, we'll need to configure a generator that will do just that.

One thing I forgot to mention earlier, the test method also needs to return a property object. Fortunately for us, the library contains an extension method that can transform any boolean expression into a property object by calling ToProperty().

Microsoft Services is now a Kubernetes Certified Service Provider
(Bahram Rushenas) - Microsoft Services is your partner to enable your organization to leverage container capabilities and frameworks, such as Kubernetes, to adopt modern technologies to increase speed and agility while also maintaining control and good governance.

How Distributed Application Runtime (Dapr) has grown since its announcement
Dapr is an event-driven, portable runtime for building microservices on cloud and edge and embraces the diversity of languages and developer frameworks. Dapr codifies best practices for building distributed applications and, at its core, are API abstractions that provide common capabilities needed to build microservices, including state management, service invocation, pub/sub messaging, secrets management, and others.

Intro

So far, we've talked about the benefits of property-based tests in my two previous blog posts here and here. I'm sure for most of you; this concept is still pretty abstract. We've seen the technique, but how do we implement it in C#? In this series, we'll go through all the steps to solve the Diamond Kata using TDD and property-based tests.

The Diamond Kata

Given a letter, print a diamond starting with A with the supplied letter at the widest point.

e.g., For input: D

---A---
--B-B--
-C---C-
D-----D
-C---C-
--B-B--
---A---

Why did I choose this specific Kata? Well, if you try to solve it with example-based tests, you effectively need 26 tests to make sure it works properly. It may not seem like a lot of tests, but if you try it, you'll soon discover that the first few tests are trivial, and as you approach Z, the tests are becoming harder to read and harder to write. It's usually a code smell that indicates that some property-based tests would be a better approach.

TDD

In this series, we'll also use TDD, and you'll see that it's much easier to use with Property-based tests than example-based tests. So in excellent TDD practitioners that we are, we are going to start with the most straightforward implementation of the algorithm that we can think of.

Easy, right? It only works for the A case, but it's a good starting point.

I already hear the purists among you.

This is not the right way to do TDD! Red -> Green -> Refactor! You should start with a failing test, not with the implementation.

You are not wrong, but let's be realistic here. It's just a starting point, and I promise we'll do better TDD going forward :D.

The tools

Property-based tests is a simple enough concept, but generating proper pseudo-random inputs can be harder than it looks. Fortunately for us, other people solved that issue and built great tools. In our case, we'll use FsCheck, a property-based testing framework made for F#.

I'm confused, are we doing this in C# or F#?

That's the beauty of .NET, even though the library is made for F#, all the .NET languages compile to Intermediate Language (IL or MSIL) and thus are interoperable. It means we can call that library from C#.

News

Blog posts

Writing .NET Database Integration Tests
(Khalid Abuhakmeh) - In this post, we will see why integration testing is my recommended path moving forward, discuss the pros and cons, and see how to implement it in our test infrastructure.

CQRS + Event Sourcing
(Daniel Whittaker)
A common issue I see is understanding the flow of commands, events, and queries within a typical CQRS ES based system. The following post is designed to clear up what happens at each step. Hopefully, this will help you to reason about your code and what each part does.

As I was mentioning in my previous post, the way we are doing unit testing today has many issues. One of the biggest problems is to know for sure when we have enough tests to be confident to release in production. Why is that? Well, we mostly base our tests on examples, so it's tough to know for sure if we covered all the edge cases and that we have all the required samples.

Tale of a developer

Let's use a story to demonstrate what I mean.

Notes:

Here, the gray circle will be defining what the client wants, the Xs will visually represent the tests, and the red lines will represent the actual output of the algorithm.

Once upon a time

Client: Could you create an algorithm that draws a circle given a radius? Something like that.

Developer: Of course! Easy peasy.

After some time

Developer: Alright I'm done, it's working.Client: How do you know if it's working properly? Did you test it?Developer: Well... I made it run on my machine. If you want, I can add a unit test to prove it.Client: That would be great.Developer: Here you go. I added a unit test that makes sure that the point (1,0) is part of the circle.

Client: How do you know that you are not only drawing a horizontal line that passes through that point?Developer: Ok, here's another test that makes sure that the point (0,1) is part of the circle.

Client: What about coordinates that are not int?Developer: Sure. I added another test for this.

Client: I don't want just a quarter of a circle. Can you make sure other quadrants are also supported?Developer: Arrrrg... Sure. I added another test for this. I'm pretty sure we covered everything. We should ship it.

Client: Alright, after all, you are the expert. Let's ship it.

Once in production

Client: What the heck. It looks nothing like the circle I wanted.Developer: I don't understand. All my tests are passing, which means this should work just fine according to your requirements.

Developer: Maybe if I add another test, I'll find the issue.

The truth is

In fact, with this kind of problem, you'll never be done adding examples. It's because a circle is composed of an infinity of points, which makes it almost impossible to validate with example-based tests.

What do we do then

At this point, I'm pretty sure we all agree that we need a better strategy to test this. That's precisely the kind of problem where Property-Based testing shines. Instead of validating points along the circle, let's try to understand and model the relationship between the input (the radius) and the output (the circle). It turns out that this relationship is well known and defined in the math world.

A circle can be defined as the locus of all points that satisfy the equation
x^2 + y^2 = r^2

It's an excellent property for a test. With only one test, we can validate any point of a circle given a radius. Voilà!

Conclusion

Stay tuned for my next blog post, where we'll solve a real problem with C# and property-based testing. In the meantime, subscribe down below to make sure you receive my blog updates directly in your inbox.