Experimental C# with Scientist .NET
Tags: csharp, dotnet, testing, tutorial
Published: 2019-12-11 01:26:10

---
title: Experimental C# with Scientist .NET
published: true
date: 2019-12-08 05:01:00 UTC
tags: csharp, dotnet, testing, tutorial
cover_image: https://i2.wp.com/killalldefects.com/wp-content/uploads/2019/12/Science-1.png
series: .NET Quality
canonical_url: https://killalldefects.com/2019/12/08/experimental-csharp-with-scientist-dotnet/
---

If you asked me what the biggest game changer for me in 2019 was for developing quality software, I would point to Scientist .NET without hesitation.

Scientist .NET lets you test experimental code safely without exposing end users to new errors or defects.

In this article, I’ll go in depth into the Scientist .NET library, explore what it is and when you should use it. I’ll highlight its benefits and drawbacks so you can make an informed decision as to whether Scientist is something you should consider for your team.

_Note: This post was originally created as part of the collaborative [C# Advent series](https://crosscuttingconcerns.com/The-Third-Annual-csharp-Advent) on C# and .NET Development. This article is also an expanded and more in-depth version of [an article](https://killalldefects.com/2019/08/27/victimless-canary-testing-with-scientist/) I wrote on Scientist in late August._

## Why Science is the Answer

So, what is Scientist .NET and what problem does it exist to solve?

Scientist .NET is part of the Scientist family of libraries, popularized by GitHub’s [original Scientist library in Ruby](https://github.com/github/scientist) but available in most modern programming languages.

Scientist is a library designed to perform experiments comparing multiple implementations of a solution to each other without introducing adverse effects to end users.

* * *

So, what does that mean?

Imagine you had a critical section of code that was steeped in complexity and years of technical debt. You’d want to pay down that debt and roll out a new or otherwise improved version of it, but you’d need to make sure you weren’t accidentally taking a step backwards.

The traditional solution might be to have a beta user flag where beta users get to interact with the new version of a feature while old users use the traditional version.

![Canary Testers getting access to new code before other users](https://i1.wp.com/res.cloudinary.com/practicaldev/image/fetch/s--_U9HtrlY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://imgur.com/vy6nhFg.png?w=770&ssl=1)

That works, but it does have a few risks. First of all, you never _really_ want to expose bugs to users – even beta users. Secondly, your beta users might not be the ones with the problematic data or scenarios, so your beta test could miss broken scenarios.

What you really want to do is test your new logic against all types of users, but remove the risk of any bugs from that logic ever reaching them.

This is where Scientist comes in.

## How Scientist Helps

With Scientist, your code calls to the Scientist .NET library to perform an experiment. You provide it a legacy implementation to use as well as one or more experimental methods to try.

Scientist will then execute all provided implementations and return only the result of the legacy implementation.

![An experiment featuring a legacy and new version of a routine](https://i0.wp.com/res.cloudinary.com/practicaldev/image/fetch/s--vIr4A3Jz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://imgur.com/fPwimYJ.png?w=770&ssl=1)

This effectively isolates the calling code from any exceptions or inaccuracies in the experimental implementations and allows you to run these pieces of code in production or other environments you might not normally try.

So how is this valuable if we’re only returning the result of the legacy version?

Scientist .NET has something called a _results publisher_ which lets us collect the results of individual experiments and store or send them on for analysis in whatever way we want.

![In cases where the new routine errors or doesn’t match the old, the results can be recorded and fixed later and the customer is ignorant that an issue even occurred.](https://i0.wp.com/res.cloudinary.com/practicaldev/image/fetch/s--K_ZTM6A2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://imgur.com/FDAgrQO.png?w=770&ssl=1)

This results publisher is where the value comes in because it can be used for things like:

- Recording if new implementations encounter exceptions
- Recording instances where the new implementation returned a different result than the old one
- Measuring the result of different implementations against large sets of data over time
- Comparing the duration of old and new implementations

Essentially, Scientist .NET gives us the data we need to make informed decisions about whether new code should see the light of day.

Best of all, Scientist .NET lets us do so without endangering the end users by pushing potentially dangerous new implementations out into production before they’ve been proven to work.

## Installing Scientist .NET

Now that we know what scientist is and what problems it helps with, let’s get into the details.

We’ll start by installing the Scientist .NET package in NuGet into whatever library we want to be able to run the experiments from.

Do this by right clicking on a project in solution explorer in Visual Studio and then selecting _Manage NuGet Packages…_

Once open, go to the _Browse_ tab and type in `Scientist .NET` to find the library:

![](https://i0.wp.com/killalldefects.com/wp-content/uploads/2019/12/image-8.png?fit=770%2C377&ssl=1)

Click install and the dependency will be added to your project.

## Experimenting with Science

Now that we have access to Scientist, let’s run an experiment.

In this example, we’ll be looking at a method that works with a problematic legacy component and trying out an experiment.

{% gist https://gist.github.com/IntegerMan/a75b9f965aa7b9b4958bc385d254b5f3 %}

Since the implementation details of the legacy code aren’t relevant to discussing Scientist .NET, just pretend that the `LegacyAnalyzer` file is thousands of lines long, rife with code smells, and needs to be burned to the ground.

Let’s start replacing it by incorporating Scientist .NET:

{% gist https://gist.github.com/IntegerMan/d65e1109ae11194ceb26105aa85823cc %}

Holy nested lambda expressions! Okay, don’t panic, let’s talk about this code.

Line 5 is what kicks off the Scientist process. We call to `Scientist.Science` method specifying the generic result type that will be returned and providing a name for our experiment. This name is provided to the result publisher and can be used to distinguish between results if you are running multiple experiments on the same result publisher.

The `experiment` parameter to the `Science` method allows us to configure Scientist .NET inside of a lambda expression. This is where the nuts and bolts of configuring our experiment are accomplished. We’ll go over these line by line.

The `experiment.Use` method tells Scientist that when it runs, it should perform the provided action _and return its result_. This is where we’ll call out to our legacy implementation for the moment. You can have one and only one `Use` invocation.

Scientist allows us to specify zero to many `experiment.Try` statements to run comparisons that will be provided to the results publisher. This is where you try your experimental logic since its results will not go back to the calling method.

You can use `experiment.Compare` to tell Scientist how to compare experiments to each other and determine if they came to the consensus. This is used by the results publisher for logging differences. For us, we’ll just look at the `Score` property on our `AnalysisResult`.

We can also add context to the results for the result publisher in order to help distinguish one result from another. In our simple example, we record the resume’s name to help differentiate one resume from another. If we didn’t do this, we would know that there’s an issue comparing some resumes, but we wouldn’t have enough information to recreate it later, making it less useful.

Take a look at [the full documentation for Scientist .NET](https://github.com/scientistproject/Scientist.net) for more ways you can configure the experiment.

If you run this now, Scientist will run the experimental and legacy version in random order and then return the result of the legacy version, leaving our behavior unchanged.

## Reporting Findings with Result Publishers

Let’s look at how to make this useful by incorporating result publishers.

By default, Scientist uses an internal in-memory result publisher which doesn’t do much for us to get usable results out of it.

You’ll need to implement `IResultPublisher` to store whatever results you want. Thankfully, it’s not too hard:

{% gist https://gist.github.com/IntegerMan/2b9dd88d6e03ffcb72badbfdd9303784 %}

Ignore the scary `<T, TClean>` syntax – it’s necessary for Scientist’s internals, but not relevant to what we really need to do.

What we really want to do is loop over the `MismatchedObservations` collection. This collection contains any experiments that threw exceptions or resulted in a value that didn’t pass the `Compare` check we defined earlier.

In this case we’re just logging to the console for simplicity.

Typically you’d log results to some form of consumable event source – either a log file, the Windows Event Viewer, add entries in a database somewhere, or track them on some form of [exception logging service](https://killalldefects.com/2019/11/23/monitoring-quality-with-error-tracking/).

* * *

In order to use our new result publisher, we have to tell Scientist about it. We do this by setting a property on `Scientist`:

{% gist https://gist.github.com/IntegerMan/75591b750d23bfe87c6969a9feee13e5 %}

Line 8 is what really matters here – we tell Scientist which `ResultPublisher` to use for experiments.

Note that I’m using static fields to track whether we’ve set the result publisher for Scientist. This is because Scientist will error if you try to change the result publisher while a test is running if you use Scientist within your unit test suite.

Personally I would rather work with a non-static version of Scientist where I didn’t need to worry about this, but this is one of the few drawbacks of the Scientist .NET library.

## Science Whenever it’s Ready: Async Results

Scientist can be made to work in asynchronous mode as well, removing some of the performance drawbacks for executing results in parallel. All that changes is that you call an async method from your `Use` \ `Try` methods and you call to `ScienceAsync` instead of `Science`:

{% gist https://gist.github.com/IntegerMan/a9977e08f4803f3baa00cef9f36e8d9c %}

## Science without Side Effects

I’m a huge advocate for Scientist and the ways it can allow you to optimize your algorithms for performance and best results as well as its hand in helping safely pay down technical debt.

That said, Scientist can have some side effects if applied without thought.

Let’s say you have an algorithm that sends out an E-Mail with results of applying for a mortgage, for example. You could provide a legacy and new implementation of this algorithm, but if Scientist .NET runs both of these implementations, two E-Mails will be sent – possibly with different contents!

Obviously this is not what you want to have happening.

Because of this, you should isolate dependencies with side effects via dependency injection and pass them in to your actual algorithm but a dummy object (or null) into your experimental implementation.

Here’s a sample to help illustrate what I’m talking about:

{% gist https://gist.github.com/IntegerMan/474f68c57973b64d78dd640dfc4861fc %}

_Note: Instead of relying on dependency injection, you could alternatively add properties or parameters to allow certain behavior to be disabled. For example, a `ShouldSendEmail` property would accomplish a similar result._

This should help illustrate that if you adopt Scientist, you’re going to start naturally decoupling your complex logic from their dependencies such as database operations, working with disk, network calls, or sending E-Mails.

Ultimately, this change in behavior is going to make your code easier to change, maintain, and test, so this is an added benefit, despite being additional work in the short term.

## Scientific Testing

Scientist .NET doesn’t just have to work in production. In fact, I’ve had phenomenal success integrating it into my unit tests.

When you think about it, all you want to happen is for a mismatch to fail your unit tests.

![Unit Testing with Scientist](https://i0.wp.com/res.cloudinary.com/practicaldev/image/fetch/s--9pMmnhy7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://imgur.com/qJ8JOk6.png?w=770&ssl=1)

I used to advocate for writing a custom `IResultPublisher` that fails unit tests on mismatches, but Scientist .NET now supports a property out of the box for unit test integration:

{% gist https://gist.github.com/IntegerMan/9d6fe758f27ca36a4a9f7af5a29cc23b %}

Setting `ThrowOnMismatches` to `true` (see line 9 above) is all you need to do to incorporate comparison tests via Scientist.

Note that for simple comparisons _in unit tests_ you may also want to consider Jest style [snapshot tests via Snapper](https://killalldefects.com/2019/08/30/snapshot-testing-in-javascript-net/).

## The Scientist Workflow

Generally, when incorporating Scientist into my workflow, the process goes something like this:

- Wrap up the existing implementation into a single method or class
- Write unit tests around the existing behavior as needed
- Introduce a new method / class that improves on the existing implementation
- Use Scientist in Unit Tests to compare the old implementation to the new implementation
- Send a Scientist build with the new logic as an experiment to Quality Assurance
- Release the scientist build into production
- After a fixed period of time without issues, remove the legacy implementation and only use the new implementation (following the full testing and release process)

The drawbacks of this workflow are:

- It takes longer for code following this flow to reach production in an active state
- Code must be tested and deployed multiple times due to the phased rollout approach
- If bugs are found in the legacy implementation, they must be fixed in both the old and new implementation during the transition period

However, the benefits are considerable:

- The code has been vetted in production against actual production data
- Depending on what was logged in your result publisher, you now have detailed information to support the performance of your new implementation
- If you follow this process, you should not be introducing any observable defects from the end user’s perspective
- You now have detailed tests covering your new implementation
- Your architecture is going to be more extensible and testable in the long term

Scientist .NET isn’t for every team, and it’s not for every change, but I’ve been able to use this technique to great effect when paying down technical debt.

## Closing Thoughts

As you can see, Scientist .NET offers a polished wrapper around a simple but revolutionary concept: test new implementations in production without exposing any of their new defects or weak areas to end users.

Ultimately, everything we do in software development is to serve some user – whether it’s someone using a web application or an automated system consuming an API. Software that is worth writing is worth writing in such a way that we don’t risk harming the end users.

Scientist .NET is an extremely powerful and capable tool for protecting the users as we pay down technical debt, improve quality, and add additional functionality.

Give it a try. Your users and codebase will thank you for it.

* * *

_Cover Photo by Gabriel Gurrola on Unsplash_

The post [Experimental C# with Scientist .NET](https://killalldefects.com/2019/12/08/experimental-csharp-with-scientist-dotnet/) appeared first on [Kill All Defects](https://killalldefects.com).