Electronic Design

Paranoid Programming

Even paranoids can have enemies. And there is a place for paranoid behavior in design. Good programmers and engineers treat incoming data and signals the way a paranoid would—they don't trust them. Safety first—checking and validating data—is their watchword.

Basically, paranoia equates to good design practices, at least when fielding incoming data and signals. Of course, there's a price to pay. In most programs, there's a lot of code overhead in just checking the bounds, type, size, and value of incoming data and variables. Some estimate that such checks can take up to 10% or more of overall code. Moreover, this checking code is typically rife with branches, which slows execution.

Worse, this code—the checks and validations—tends to be error-prone. In C and C++, it's usually implemented as complex switches. Needless to say, these statements definitely aren't the fun part of coding (or debugging). They make up the "admistrivia" of programming, boring checks that must be done. Unfortunately, such complex switches also tend to be hard to read and maintain.

As a programmer (assembly and C), most of my problems came down to complex switches—i.e., complicated multiple branches based on selected values. Here, cryptic coding led invariably to my downfall. I'm not alone. Complex switch statements are the bane of most programmers. Also, they do most of the boundary checks, an error-prone activity. Other than input or algorithm errors, this is where most coding errors lurk.

Let's eliminate the problem. Let's automate complex switch and bounds-checking. This isn't hard to do. Just put an open table entry form in the IDE editor, which generates the code for compilation or assembly. This code is "correct by construction." Additionally, the open switch form is easy to read. It documents the what, why, and how of the switch. These forms let us see what we did. Even better, the test and maintenance programmers can see it also over the code's lifecycle.

Automating complex switch statements may not lead to scientific glory, but let's get efficient and let the software do some of the paranoid thinking for us.

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish