Lookups with Switch Expressions

In a recent PR I have optimized parsing known header values, such CacheControlHeaderValue or MediaTypeHeaderValue.

The original code used a Dictionary<> to look up a Func<string, T?> parsing method, with a string input and a generic type T return type. In this blog post I will summarize the pros and cons of different alternatives that were considered as part of the optimization.

FrozenDictionary

This dictionary contains known Func<string, T?> parsers. These known parsers do not change at runtime. Using a FrozenDictionary would bring performance benefits for the lookup operations. However, freezing the dictionary itself takes time at runtime, because when freezing the new dictionary builds an efficient lookup. This is usually worth the trade-off as freezing only needs to happen once during the lifetime of the application. However, in this case all the contents of this dictionary are known at compile time.

Find out more


Stable and Unstable Sorting for Structs in .NET

Sorting items of a collection is a common task that .NET developers perform. A collection is a data structure that stores multiple values, such as an array, a list, a dictionary, etc.

One common way to sort items of a collection in .NET 8 (and previous .NET versions) is by using LINQ’s OrderBy extension method. LINQ stands for Language Integrated Query, and it is a set of features that allow you to query and manipulate data in various ways. For example, you can use OrderBy to sort a list of numbers:

// Create a list of numbers
List<int> numbers = [3, 1, 2];

// Sort the list using OrderBy
var sorted = numbers.OrderBy(x => x);

Another way to sort items of a collection is by using Array.Sort method. This method works on arrays, which are fixed-size collections of values. For example, you can use Array.Sort to sort an array of numbers in ascending order:

Find out more


Choosing Package Dependencies

Applications today are built upon well-tested, reusable packages. These packages solve the most common aspects of software development, such as logging, authentication, handling HTTP requests, serialization etc.

It is a regular task for a development team to decide which dependency to take on. New features will be built-up these dependencies. A new dependency is an additional responsibility for team: updating and replacing the package without breaking the features built on it. Updating non-breaking or patch releases is a straightforward task, however replacing a library or updating it with breaking changes can be extremely costly. I have come across teams stuck with no longer maintained libraries for years, before the team managed to completely get rid of it. Any security vulnerabilities during this period pose a risk. Therefore, the decision for picking the right library has a huge weight.

I very often observe teams deciding application dependencies based on the packages' popularity. I think this is a fraud way of picking libraries. In this post I summarize the questions that I prefer to ask when I have to introduce a new dependency in an application. I expand on my questions in the space of .NET, C# and NuGet packages. I know some of the languages are vastly different to the NuGet package ecosystem, for example npm is has more and tiny packages. Developers of such ecosystems might take my concerns with a grain of salt and apply that best works for them.

NuGet Packages

Find out more


Stable and Unstable Sorts in .NET

Sorting items of a collection is a common task that .NET developers perform. A collection is a data structure that stores multiple values, such as an array, a list, a dictionary, etc.

One common way to sort items of a collection in .NET 8 (and previous .NET versions) is by using LINQ’s OrderBy extension method. LINQ stands for Language Integrated Query, and it is a set of features that allow you to query and manipulate data in various ways. For example, you can use OrderBy to sort a list of names alphabetically:

// Create a list of names
List<string> names = new List<string>() { "Charlie", "Alice", "Bob" };

// Sort the list by name using OrderBy
var sortedNames = names.OrderBy(name => name);

Another way to sort items of a collection is by using Array.Sort method. This method works on arrays, which are fixed-size collections of values. For example, you can use Array.Sort to sort an array of numbers in ascending order:

Find out more


Building an IAQ device with Meadow

Indoor air quality (IAQ) is increasingly important for our health, productivity and well-being. The industrialized world suffers from air pollution, and buildings are no exceptions. A room's air might be filled with pollutants such as bacteria, carbon monoxide or carbon dioxide.

Meadow is a microcontroller that can run C# code. It is designed by Wilderness Labs. Meadow can be attached to a Project Lab, which is a functional IoT prototyping platform. It has numerous sensors built-in, buttons, a screen and a wide variety of connectors.

One particular sensor on the Project Lab board is the BME688 manufactured by Bosch. This sensor is interesting as besides the regular temperature, pressure, humidity, it can measure gas resistance. It is a metal oxide-based sensor that can detect gases by adsorption on its sensitive layer. It also comes with a software library, which can distinguish the different gases using AI. It can detect Volatile Organic Compounds (VOCs), Volatile Sulfur Compounds (VSCs), carbon monoxide and carbon dioxide.

The sensor also comes with an AI feature and a comprehensive SDK that enable developers to design and customize its behavior to their use-case. One such example I found online is the Cheese vs. Meat detector.

Find out more