What Causes Bugs in Code?
Learn what can cause bugs in a codebase.
We'll cover the following...
Bugs are software defects that result in the product behaving incorrectly by producing wrong results. When a software system does not handle a state because its design or implementation does not handle it, it leads to a bug. Let’s look into how bugs can arise:
Programmer oversight
Programmer oversights or lapses occur when programmers fail to consider specific situations their program might encounter, which can result in buggy code.
Press + to interact
# Define a function called BinarySearch that takes a list and an elementdef BinarySearch(A, elem):left = 0 # Set the leftmost index to 0right = len(A)-1 # Set the rightmost index to the last index of the list# While the left index is less than the rightwhile left < right:# Calculate the middle index by adding the left index to half of the difference between right and left indicesmid = (right-left)//2+ left# If the middle element equals the target elementif A[mid] == elem:return True # Return True indicating that the element was found# If the middle element is greater than the target elementif A[mid] > elem:right = mid-1 # Move the right boundary to the mid index - 1# If the middle element is less than the target elementelse:left = mid+1 # Move the left boundary to the mid index + 1# After all elements are checked and the target is not found, return Falsereturn False# Create a test arraytestArr1 = [1,2,3,4,5,6]# Test the BinarySearch function with the number 3 (which is present in the list)print(BinarySearch(testArr1, 3)) # Expected output: True# Test the BinarySearch function with the number 5 (which is present in the list)print(BinarySearch(testArr1, 5)) # Expected output: True# Test the BinarySearch function with the number 10 (which is not present in the list)print(BinarySearch(testArr1, 10)) # Expected output: False
For example, the code above needs to handle a particular use case. What is it? Before checking the answer below, try to debug it yourself, ...