mistake proofing in C programming

Mistake Proofing in C Programs

Table of Contents
    Add a header to begin generating the table of contents

    Introduction

    The Japanese concept of ‘poka-yoke’ talks about preventing mistakes by introducing certain mechanisms. It was originally designed for machinery which can be applied for any other aspect of life as well. What about mistake proofing in C programming ?

    The earlier we get to know about mistakes in programs it is easier to fix them.

    Let us consider the following code snippet (Fig 1): 

    mistake proofing in C programming
    Fig 1: Simple if condition to check against MAX_VALUE

    t is a simple conditional code where integer variable value is compared against absolute value MAX_VALUE and prints appropriate messages. While this appears to be a very simple program many times during development the equal-to operator (‘==’) is mistakenly replaced with assignment (‘=’) operator, which will yield unfavorable results (Fig 2): 

    mistake proofing in C programming
    Fig 2: Small mistake giving incorrect results
    mistake proofing in C programming
    Fig 3: Mistake proofing during compile time

    In this case message under if condition always will get printed irrespective of value of variable value.

     

    Now how do we prevent this mistake? Very simple, change the way the equal-to operator is used (Fig 3). 

    mistake proofing in C programming
    Fig 4: Mistake proofing during compile time

    That way if assignment operator is used against an absolute lvalue, appropriate error message is given during compilation phase itself (Fig 4):

    mistake proofing in C programming
    Fig 5 : Output

    By making such small changes in the code and making it as a programming practice, developers can avoid mistakes during programming which can be called as ‘poka yoke’. There could be many such examples that can be adopted for writing error free programs by getting issues earlier phase of development.

    Other than mistake proofing in C programming what other mistake proofing mechanisms you can think of? What other facilities that C offers for developers to operate in prevention mode? 

    Click here to know how spilt command helps you to organize files.

    Share this material with your friend:

    Leave a Comment