Developers can now use a new Ruby library called Scientist, which will help them rewrite critical code.

As codebases in applications mature, often developers will need to replace or rewrite part of a system, according to a blog post by GitHub engineer Jesse Toth. As a way to ensure testing correctness for these applications, GitHub built Scientist to test production data and behavior.

Scientist works by creating a lightweight abstraction called an “experiment” around the code that is to be replaced. The original code is “delegated to by the experiment abstraction, and its result is returned by the experiment,” wrote Toth.

(Related: Why some developers are frustrated with GitHub)

Then the rewritten code is added as a candidate to be tried by the experiment when it is time to execute. At that time, both code paths are run randomly. The order is randomized to avoid ordering issues.

By running and comparing both of the systems, Toth wrote, developers can use that data as a feedback loop to modify the new system to fix errors, measure, and repeat until both systems are the same. It’s possible to use Scientist before the rewritten system has been fully implemented.

Over the last few years, GitHub has used Scientist for a few of its projects, such as:

  • A rewrite and cleanup of its permission code
  • Switching to a new code search cluster
  • Optimizing queries for performance
  • Refactoring parts of the codebase

Toth wrote that developers who are making a “risky change” to their Ruby codebase can experiment with Scientist in their own system. Since Scientist is open source, GitHub encouraged developers to build open-source libraries.