Software quality. What is a bug?
Theory
A software bug is a problem that causes a program to crash or produce invalid output. There are many reasons for software bugs; the most common of them are human mistakes in software design, coding, or understanding of the requirements.
A program is usually called stable if it doesn't have a lot of obvious bugs. If the program has a large number of bugs that affect the functionality and cause incorrect results, it is considered buggy or unstable. The user cannot successfully interact with such software. It is important to find bugs before users start interacting with your program.
§1. Bug etymology
You've probably already heard the term "bug" and can imagine what it means. Here is a little story about this.
The first computer bug was discovered by Grace Murray at Harvard University in 1947, while she was working on the Mark II computer. During the investigation of an issue, an actual moth was found between the contact of the relays. The moth was removed and added to the logbook, which now is located at the Smithsonian Institution's National Museum of American History in Washington, D.C.
So, computer errors are often called bugs. This term is used to describe any incorrect program behavior until the specific nature of the error is determined.
Some Internet sources give a different story of this term. You can google it if you are interested. Let us know if you find a story most credible to you.
§2. Why do programs have bugs?
Developers often say that a program without bugs doesn't exist. There are some common reasons for bugs in software:
- communication issues in the team;
- misunderstanding of the requirements;
- software complexity;
- programming errors (programmers, like anyone else, can make mistakes);
- time pressure;
- use of unfamiliar technologies;
- an error in a third-party library (yes, that happens too).
During this course, you will mainly encounter bugs caused by misunderstanding of the given requirements or programming errors.
§3. Avoiding bugs
It is almost impossible to avoid all bugs in a large program, but it is possible to reduce their number. Here we give you five steps that can help to do that in both educational courses and industrial programming.
- Make sure you know what to do. As a programmer, you need to understand the requirements of a program that you are going to work on. If you have doubts, you can always find some help on the Internet or among fellow developers.
- Decompose a program into small units that are easy to look through and understand. A good architecture reduces software complexity, and, as a consequence, the number of errors.
- Write easy-to-read-code and follow all the standards of the language. It will also allow you to make fewer errors.
- Run the program with boundary input values. Do not forget to consider different cases: 0 or a huge number as an input value, 0 or 1 element as an input sequence. Such cases often reveal bugs.
- Write automated tests that will check the program at the build time.
We will not discuss automated tests in this topic, but we will return to that later. At this moment, you can simply create a set of input values and run the program manually (as it was described in step 4).
§4. Debugging
Suppose you know that your program does not work correctly for some input values. To fix this bug, you need to find it in the code and then make some changes.
To locate a buggy place, you can:
- read the code and try to understand what it does for the input values;
- start the debugger and see the current values of variables and the control flow of the program;
- print the current state of the program in critical parts of the code (logging) and then analyze it.
The combination of the approaches above will allow you to find most of the bugs in your program.
§5. Conclusion
Let's sum this topic up. A bug is the incorrect behavior of a program. However, there are ways to reduce the number of bugs. For example, understanding the requirements of the program, following the standards of the language, building a clear-cut architecture of the program, checking the program with boundary values, and using automated tests.
If you still happen to have a bug, first of all, you need to locate it in the code. For that, you can use a debugger or logging, or try to understand what the code does with input values by reading it. Use these tips, and, most likely, you will avoid writing buggy programs and spending time debugging them.
You can also view Lesson on hyperskill.org.
Practical tasks and answers
Tasks and answer options are given. The correct option is highlighted in blue color.
№1. The reason for a bug
Question: A development team was tasked with writing code that processes the input data on the site. They wrote a program that can read passwords in the form of numbers, names, and logins.
But they got an error while testing the program. Passwords, in addition to numbers, must also contain letters. You already know what reasons lead to bugs. So, which of them contributed to the fact that the developers failed?
Select one or more options from the list:
- computers in the office are not good enough
- misunderstanding requirements as they were not clear ✔
- weak internet connection
- bad mood of the customer
Explanation. Based on the information provided, the most likely reason that led to the bug in the program is "misunderstanding requirements as they were not clear". The developers may have misinterpreted the password requirements and did not include the necessary code to ensure that passwords contain letters in addition to numbers, names, and logins. It is important for requirements to be clear and unambiguous to avoid such issues.
№2. Importance of debugging
Question: Why is it important to debug?
Select one option from the list:
- because everyone on your team does that
- to avoid errors in programs ✔
- because that's what the main developer said
- just for fun
Explanation. It is important to debug code to avoid errors in programs. Debugging helps to identify and fix issues in the code, which can prevent errors and improve the overall quality of the program. Debugging is a crucial part of the software development process and helps to ensure that the program functions as intended.
№3. Software bugs
Question: Select essential reasons for software bugs:
Select one or more options from the list:
- programming errors ✔
- weather outside
- problem with communication in the team (when you are not working alone) ✔
- misunderstanding of requirements ✔
Explanation. The essential reasons for software bugs are "programming errors" and "misunderstanding of requirements". Programming errors can occur due to mistakes made during coding, such as syntax errors, logic errors, or incorrect use of variables. Misunderstanding of requirements can lead to bugs if the developers do not fully understand what is expected of the program or if the requirements are not clear or complete. "Weather outside" is not a reason for software bugs, and "problem with communication in the team" can contribute to issues but is not an essential reason for bugs.
№4. A program with lots of bugs
Question: How can you describe a program if it has a large number of bugs that affect the functionality and cause incorrect results?
Select one option from the list:
- it is not a program at all
- unnecessary
- unstable ✔
- unnatural
Explanation. If a program has a large number of bugs that affect its functionality and cause incorrect results, it can be described as "unstable". This means that the program is not reliable and may not perform as expected. It is important to identify and fix these bugs to improve the stability and functionality of the program.
№5. The first bug
Question: In which year was the first computer bug discovered?
Select one option from the list:
- 1948
- 1947 ✔
- 1946
- 1945
№6. What is a bug?
Question: A bug is:
Select one option from the list:
- a problem that causes a computer to crash
- a new task a programmer is unable to solve
- a new part of the code that causes a computer to crash or produces invalid output
- a problem that causes a program to crash or produce invalid output ✔
Explanation. A bug is "a problem that causes a program to crash or produce invalid output". It is an error or flaw in the code that can cause unexpected behavior or incorrect results when the program is run. Bugs can occur due to programming errors, misunderstandings of requirements, or other issues in the development process. It is important to identify and fix bugs to ensure that the program functions as intended.
№7. Where is the bug?
Question: You are writing a project using various third-party libraries. At some point, you discover that the program outputs incorrect values. You checked your code but did not find any errors. Where can you most probably find the bug?
Select one option from the list
- in a third-party library ✔
- in your code, check it again
- in the computer system
- in the internet connection
Explanation. If you have checked your code and did not find any errors, the most probable place to find the bug is in a third-party library. It is possible that there is a bug in the library's code that is causing the incorrect output. You may need to review the documentation for the library or reach out to the library's developers for support in identifying and fixing the issue.
№8. How to locate a bug?
Question: What can one do in order to locate a buggy place?
Select one or more options from the list:
- start the debugger ✔
- analyze the code ✔
- print the current program state ✔
- rewrite the whole code
- just run the code again
Explanation. To locate a buggy place, one can start by analyzing the code to identify any potential errors or issues. Additionally, using a debugger can help to step through the code and identify the specific location where the bug is occurring. Printing the current program state at various points in the code can also be helpful in identifying where the bug is occurring. Rewriting the whole code is not necessary and is generally not an efficient solution. Just running the code again without making any changes is unlikely to fix the issue.
№9. Reducing the number of bugs
Question: Which of the following recommendations can reduce the number of bugs in your program?
Select one or more options from the list:
- write all the code in one place (i.e. file, class, method)
- run the program with boundary input values ✔
- understand what you need to develop ✔
- write intricate and unobvious code
Explanation. The following recommendations can reduce the number of bugs in your program:
- Run the program with boundary input values: This can help to identify any issues with the program's handling of edge cases or unexpected inputs.
- Understand what you need to develop: Having a clear understanding of the program's requirements and expected behavior can help to avoid misunderstandings and errors in the code.
- Avoid writing intricate and unobvious code: Writing clear, simple, and easy-to-understand code can reduce the likelihood of errors and make it easier to identify and fix any issues that do arise.
Writing all the code in one place is not recommended as it can make the code difficult to read, understand, and maintain. It is generally better to organize the code into separate files, classes, or methods based on their functionality.
№10. How to get rid of bugs?
Question: Imagine you wrote some code, and it does not work right due to bugs. What can you do to fix the code? Choose several possible actions below:
Select one or more options from the list:
- write automated tests ✔
- create a set of input values and run the program manually ✔
- hope everything is just fine
- look through the program and guess if it is correct
Explanation. If your code is not working correctly due to bugs, the following actions can help to fix the code:
- Write automated tests: This can help to identify specific issues in the code and ensure that they are fixed before the code is deployed.
- Create a set of input values and run the program manually: This can help to identify issues with the program's handling of specific inputs or edge cases.
- Look through the program and identify potential issues: This can help to identify specific areas of the code that may be causing issues and allow you to focus your debugging efforts.
Hoping everything is just fine is not a recommended action as it is unlikely to fix any issues and can lead to further problems down the line. Guessing if the program is correct is also not recommended as it is unlikely to be an effective debugging strategy.