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


Featured

  • IT Modernization
    shutterstock image By enzozo; photo ID: 319763930

    OMB provides key guidance for TMF proposals amid surge in submissions

    Deputy Federal CIO Maria Roat details what makes for a winning Technology Modernization Fund proposal as agencies continue to submit major IT projects for potential funding.

  • gears and money (zaozaa19/Shutterstock.com)

    Worries from a Democrat about the Biden administration and federal procurement

    Steve Kelman is concerned that the push for more spending with small disadvantaged businesses will detract from the goal of getting the best deal for agencies and taxpayers.

Stay Connected