# Brewing Beer With a Raspberry Pi: Making Cooling Rate Calculations Testable

### The cooling rate calculation we added to our beer cooling solution in the previous post worked, but there was still room for refactoring to achieve better testability and architecture. In this post we took a calculation method with complex dependencies and separated calculations from the old method to the new one.

Join the DZone community and get the full member experience.

Join For FreeMy previous beer IoT post introduced how to measure the cooling rate of beer. As I introduced the first calculation there, I implemented it into the code to complete our calculations. Now, it’s time to focus on the implementation and make some small improvements that clean up the code and improve the technical design.

Although we can calculate cooling rate, the calculation is not implemented very well. Here’s the current method.

```
private void MeasureCoolingRate(IEnumerable<Measurement<double>> temps)
{
if (!_initialMeasurementsDone)
{
var ambient = temps.First(t => t.DeviceId == AmbientSensorId);
_Ta = ambient.Value;
_T0 = temps.First(t => t.DeviceId == BeerSensorId).Value;
_firstMeasurementTime = ambient.Time;
_initialMeasurementsDone = true;
return;
}
var timeDelta = (temps.First().Time - _firstMeasurementTime).TotalMinutes;
if (timeDelta >= 5)
{
var beer = temps.First(t => t.DeviceId == BeerSensorId);
var d1 = _T0 - _Ta;
var d2 = beer.Value - _Ta;
_k = Math.Log(d1 / d2) / timeDelta;
_kMeasurementDone = true;
Debug.WriteLine("k = " + _k);
}
}
```

We have the following problems:

measuring method contains external logic (measuring using two steps)

if we want to test the calculation we have to use some fake client

writing unit tests for methods like this is complicated

To solve these problems we have to separate the methods of measuring from calculations. As a result we have one method that deals with cooling rate measuring and storing logic, and a second method that calculates the cooling rate based on the given data.

## Separating Calculation of Cooling Rate

Let’s focus first on the cooling rate calculation. If we want to test just the calculation, we have to move the calculation to a separate method. Let’s call this method GetCoolingRate and move it to some other class.

```
public static class Calc
{
public static double GetCoolingRate(double T0, double Ta, double Tt,
double t)
{
var d1 = T0 - Ta;
var d2 = Tt - Ta;
return Math.Log(d1 / d2) / t;
}
}
```

We can make this method static as it doesn’t have any dependencies with other classes in the solution. Now, we can use some test framework and write tests for this method.

```
[Fact]
public void TestCoolingRate()
{
double Ta = 8;
double T0 = 22;
double Tt = 19;
double t = 120;
double k = 0.002;
double kToTest = Math.Round(Calc.GetCoolingRate(T0, Ta, Tt, t),3);
Assert.Equal(0.002, kToTest);
}
```

This code is just an illustration and doesn’t handle rounding problems the way it should in the real code. But, you get the point.

## Measuring Logic

As the calculation of the cooling rate is now in a separate method, we have to modify our old method to use the new one.

```
private void MeasureCoolingRate(IEnumerable<Measurement<double>> temps)
{
if (!_initialMeasurementsDone)
{
var ambient = temps.First(t => t.DeviceId == AmbientSensorId);
_ta = ambient.Value;
_t0 = temps.First(t => t.DeviceId == BeerSensorId).Value;
_firstMeasurementTime = ambient.Time;
_initialMeasurementsDone = true;
return;
}
var timeDelta = (temps.First().Time - _firstMeasurementTime).TotalMinutes;
if (timeDelta >= 5)
{
var beer = temps.First(t => t.DeviceId == BeerSensorId);
_k = Calc.GetCoolingRate(_t0, _ta, beer.Value, timeDelta);
_kMeasurementDone = true;
Debug.WriteLine("k = " + _k);
}
}
```

Now our old method carries out only one task–controlling the process of gathering the data for the cooling rate calculation. It’s now just a helper method, and if we need to test it, then it’s more for integration tests.

## Wrapping up

It’s always a good idea to find the time to clean up your code if you have introduced something new to it. The cooling rate calculation we added to our beer cooling solution in the previous post worked, but there was still room for refactoring to achieve better testability and architecture. In this post, we took a calculation method with complex dependencies and separated calculations from the old method to the new one. As a result, we can easily test cooling rate calculation using the unit test framework of our choice.

## Brewing Beer With a Raspberry Pi: Table of Contents

- Brewing Beer With a Raspberry Pi: Measuring Temperature
- Brewing Beer With a Raspberry Pi: Moving to ITemperatureClient Interface
- Brewing Beer With a Raspberry Pi: Measuring Cooling Rate
**Brewing Beer With a Raspberry Pi: Making Cooling Rate Calculations Testable**- Brewing Beer With a Raspberry Pi: Reporting Measurements to Azure IoT Hub
- Brewing Beer With Raspberry Pi: Stream Analytics
- Brewing Beer With Raspberry Pi: Visualizing Sensor Data
- Brewing Beer With Raspberry Pi: Building a Universal Windows Application

Published at DZone with permission of Gunnar Peipman, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Comments