Contact us


Engineer Blog

Beware of Global Variables

Traps for first-time software engineers

This story is from early in my career, back when I had little experience with software development. It concerns a blunder I made on a firmware development project.

The device I was writing firmware for was designed so that the input and output (I/O) signals could be switched remotely. We decided that in order to implement this functionality, I should write a program that periodically monitored I/O status and performed processing as appropriate. The processing itself was relatively simple and involved creating a function that would directly store the I/O states that formed the judgement criterion in global variables for processing. After testing the firmware under a reasonable range of anticipated conditions, I decided that the code worked fine, and released the firmware. The testing I had performed consisted of evaluating the various permutations of I/O on and off states─there were only two possible states.

A few years after the firmware was released, however, I received a message that external I/O signals were not being processed correctly. I immediately set about investigating. It turned out that the conditions under which the firmware was actually used were characterized by rapid remote toggling of I/O states. I could not offer any defense, because when developing the code, I had not subjected it to such stringent conditions. Further investigation revealed a straightforward cause to the problem. The I/O states were stored in global variables, which I had assumed would remain unchanged, irrespective of where they were called from. I had not anticipated the overwriting of I/O states by a separate function. This was the cause of the processing errors.

This experience made me acutely aware of the importance of understanding the algorithms behind the functions that are envisaged for the code you are developing and satisfying yourself that you understand how data flows and is processed, rather than using global variables to process everything. While a basic consideration, the question of where global variables will be accessed from and when they will be updated is an important one. The experience also made me very aware of the importance of considering all possible permutations of multiple states when coding (and creating specifications for testing), rather than simply considering the permutations of on/off states. This said, considering every possible permutation is a difficult undertaking.

While these issues are obvious to some, they may not occur to those writing their first program. If you get stuck when coding, there’s nothing wrong with going online and reading books, but I believe the best thing you can do is ask your neighbor. You’d be surprised how often they will know the answer or can at least offer a hint.

Satoshi Watanabe
Solutions Development Division, Solutions Team (Deputy Manager)

[Major achievements in product development]
PAT-T series regulated DC power supplies, PLZ-4W series electronic load devices
Grid simulator software for simulation of systems
Software for avionics testing
The Wavy software series, used for creating and controlling sequences

Contact us