Wednesday, June 9, 2021

Avoiding mistakes

What are mistakes?

The computers don't make mistakes, but user can tell the computer what to do and cause mistakes!

Hence it is very important to learn how to make computer avoid mistakes.

Because the mistakes turn out into big blunder

  • as in banking process balance will be made  nil within second
  • as in medical instrument wrong reading makes wrong diagnosis and leads to severe problem for the patient
  • as in design software the building may collapse due to mistake in building design

The mistakes or bugs can cause a program to show unexpected results or sometimes make the computer to stop working!

Hey Computer:

Create my_number

Remember 10 as my_number

Repeat until my_number is less than 9

Add 1 to my_number

Display my_number

 

Output:

     Not responding…

The above sequence is not responding or hangs out as my_number will never be less than 9 and the program never display the output (and get into an infinite loop).

What is debugging?

The debugging is simply means finding and correcting the bugs. It is an important part of programming.

For example the following sequence is having bug:

Hey Computer:

Remember “Dog” as my_pet

Add “Cat” to my_pet

Display my pet

 

Output:

     ----No output----

The bug is in last instruction:

 Display my pet

There is no underscore between two words. Such mistakes are called syntax errors. Note that this type of errors is easily found out by just reading the instructions carefully. The process of reading instructions is called debugging.

Logic bug

When you press space bar, the text moves to the next line, it is called as logical bug.

Sometimes, programs have logic bug so the program doesn't crash but doesn't give the desired result.

For example observe the below sequence

Hey Computer:

Create my_age

Remember 30 as my_age

IF my_age is about 35

Display “Your offer 35%!”

IF my_age is below 25

Display “Your offer 25%!”

     Output:

          ---unpredictable---

Here no check for age between 25 and 35 is done. So it is hard to find the logic bugs like this. Finding the logic bugs is also called as a debugging.

While writing the sequence, there should be a condition to check for all scenarios.

Avoiding bug

When programs become longer and complex their instructions become harder to read.

How can we avoid mistakes and understand it better? The solution is to providing comments to understand the code better.

In other words comments and meaningful variable and function names make the code easier to debug and understand.

The programmer should carry a series of tests before releasing a program as there might be critical bugs in it.

 

Summary

The mistakes in programs are called as bugs.

 The debug is the process of finding bugs in a program.

The bugs are usually logic or syntax based one.

The bugs can be avoided by commenting, using proper names and testing the program.

 

PS: I have composed this post-series based on my pdf eBook on playstore:

Basics of Computer, Programming and C

The series is posted continuously one each next day, and reader can found any post by referring blogs’ time line tab.

--till next post, bye-bye and take care.

 

No comments:

Post a Comment