Refactor Embedded Software Code – A Proven Method

refactor embedded software

Refactor embedded software code is not an easy task. Any software code refactoring is something developers, and team leads often try to avoid. When there’s no proper test automation and good test coverage they have a good reason to.

Think about the hassle of supporting current customers’ setup when you consider refactoring embedded software code few years after you’re done developing. It means that you probably don’t have your physical testing lab setup ready, the developers that wrote the original code might have left your company and the test scripts are outdated.

OK… after we’ve covered the issues with refactor embedded software code, let’s talk about what we can do to make it easier.

Record I/Os

The first thing to do is running your current, stable version of the product against different inputs and record the outputs. This part is tricky and long but once you’ve done it the rest is easy. You’ll need to get very good coverage here as it will serve all your black box regression tests in the next steps. Think it’s impossible? Think again – Compaq did it when it cloned IBM’s IP to produce the first portable IBM compatible machine legally in the 1980s (learn more).

Generate test for each I/O interaction

Once you have a record of all the outputs that resulted from an input scenario you need to write a test. It’s a black box test for that matter (but kinda grey box test). When choosing the testing scripts language, choose one that can work with your testing framework.

Run regression tests on your current device

After you’ve got all your test scripts, test runners and testing harness in place, it’s time to run it on your current device to validate that your black box regression tests run as it should. In this step, it’s worth to investigate if all tests are necessary or you have tests that cover duplicate scenarios.

Automate your test runner

Once your regression tests work it’s time to automate the test run. Automating test runs on real hardware is not an easy task, and for that, you can use Virtual Devices, simulators, and emulators such as QEMU and our Jumper Virtual Lab.

Start refactor with confidence

Connect your automated black box test process to your source control and build tool so you can refactor embedded software while making sure the functionality is still working as it should. It is recommended to run this regression daily, or at least part of it (often defined as sanity). The idea is to detect bugs as early as possible. This creates great confidence in the refactoring process.

Is there a better way?

Yes! But it requires you to have a continuous integration process in place. Want to learn more? Head to these links:

  1. How to build continuous integration for embedded.
  2. How to setup STM32 continuous integration.
  3. How to setup nRF52 build server.