Testability
Interface-based design leads to decoupled software, which improves testability. Let us analyze the former example and see how decoupled design helps with testing. We’ll focus on the tap detection algorithm.
In this example, we create a simple algorithm that detects a tap when the difference between the current sample and the previous sample on any axis exceeds a predefined threshold. This oversimplified implementation is shown in this code:
#include <cmath>
#include <algorithm>
class tap_detection_algo {
public:
tap_detection_algo(accelerometer &accel)
: accel_(accel), first_sample_(true) {}
bool run() {
auto current = accel_.get_data();
if (first_sample_) {
prev_ = current;
first_sample_ = false;
return false;
}
bool tap = (std::fabs(current.x - prev_.x) > c_threshold) ||
(std::fabs(current.y - prev_.y) > c_threshold) ||
(std::fabs(current.z - prev_...