Q

Emertxe-News & Blog

Home » C programming » Mistake Proofing in C Programs
Mistake Proofing in C Programs
mistake proofing in C programming

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

It 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 

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 3: 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):
Error getting detected in compile time itself

 
Fig 4: Error getting detected in compile time itself

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.

YOU MAY ALSO LIKE

Emertxe’s Online Training Programs in Embedded Systems & IoT

Emertxe’s Online Training Programs in Embedded Systems & IoT

The reason why Emertxe is the best choice for career advancement: eLearning and Online training programs are the way forward in the COVID-19 disrupted world. Riding along the digital revolution will ensure engineers are future-ready with skills to not only secure but...

Our Training Programs for Freshers

Our Training Programs for Freshers

Introduction: Emertxe is the leading training institute in Bangalore for Embedded Systems and IoT domains. It is a pioneer in training freshers since 2003 by providing excellent placement opportunities for freshers. Over the years 70000+ students have made their...

Q