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

  • Defense

    DOD wants prime contractors to be 'help desk' for new cybersecurity model

    The Defense Department is pushing forward with its unified cybersecurity standard for contractors and wants large companies and industry associations to show startups and smaller firms the way.

  • FCW Perspectives
    tech process (pkproject/Shutterstock.com)

    Understanding the obstacles to automation

    As RPA moves from buzzword to practical applications, agency leaders say it’s forcing broader discussions about business operations

Stay Connected

Sign up for our newsletter.

I agree to this site's Privacy Policy.