GCN Tech Blog

By GCN Staff

Blog archive

Why so many bugs?

Why do programs have so many bugs? Because they are so easy to make, especially given the challenging syntaxes of most programming languages.

Recently, Elkridge, Md.-based consultancy firm Netrino published a book, 'Netrino Embedded C Coding Standard,' written to help programmers write code with fewer bugs. The volume contains about 100 rules and principals, all involving how to reduce errors that programmers make while composing functions, macros, variables and the like.

At the Embedded Systems Conference, being held this week in Boston, we caught up with Netrino president Michael Barr at and asked him to give us a guiding principle from the book. The one he gave reminded us how a simple mistake could create a huge problem.

Here it is: When having the program compare a variable with a constant, Barr advises to always put the constant on the left side of the comparison, i.e.

"If 1 == Q then [execute the rest of routine]"

This statement says that if the variable Q equals 1 then go ahead and perform the routine. In the parlance of C, the double equal signs mean that the digit on left side is equal to the variable to the right side. It is used to determine when to execute conditional loops, such as this one.

Here is where a mistake can creep in: The C programming language has a very similar statement to the double equal sign, namely the single equal sign "=." The single equals sign actually assigns a value to a variable. When you write "Q = 1" then the variable Q will then have the value 1.

Now, say a hasty programmer meant to write "If Q == 1..." but forgot the second equal sign and instead wrote "If Q = 1."

With C, the program would just assign the value 1 to the variable Q, overwriting the true value of Q. No warnings would be given, no flags would be raised. But with this small, unintended change, all heck could break loose when the program actually runs, because Q now has an incorrect value.

But if the programmer were to follow Netrino's guide and get into the habit of writing "1 == Q" rather than "Q == 1" then when such a mistake were to be made (i.e. " 1 = Q") then the compiler would immediately highlight the error.

The C programming language is dangerous language to work with, insofar as you can create a lot of these kinds of deadly errors. But it is also the overwhelming favorite language for embedded systems developers, Barr noted. So by having programmers abide by these practices such as those offered by Netrino, a development shop could save hours of programmer time spent debugging code.

Posted by Joab Jackson on Oct 30, 2008 at 9:39 AM

inside gcn

  • digital license plate (Reviver)

    Sacramento drives digital license plate test

Reader Comments

Tue, Nov 18, 2008 M T

MISRA C, real-time Java, sheesh - why not just go Ada 2005

Sun, Nov 2, 2008 David Cooper

The MISRA C standard already defines a safety oriented subset of C (and has done for more than a decade). How is this different?

Please post your comments here. Comments are moderated, so they may not appear immediately after submitting. We will not post comments that we consider abusive or off-topic.

Please type the letters/numbers you see above


HTML - No Current Item Deck
  • Transforming Constituent Services with Business Process Management
  • Improving Performance in Hybrid Clouds
  • Data Center Consolidation & Energy Efficiency in Federal Facilities

More from 1105 Public Sector Media Group