r/PythonLearning • • May 21 '26

If code runs fine, where do bugs come from?

I have been seeing loads of reel on insta this days for coder, where they talk about bugs n fixing bugs, but in real term what is bug, i have been writing codes/script it never failed the pipeline so what actually trigger a bug or they call it fixing bug

1 Upvotes

13 comments sorted by

9

u/tiredITguy42 May 21 '26

Firstly, most code is not that simple and short as your learning excercises so errors may happen, but this may be true even for few lines of code as well.

Bug is usually some unhandled situation you did not count for or mistake in the code which was not found during coding and testing as your code never hit that branch.

Imagine some try-except block, where you handle failed network connection, but the error was never raised during your testing or debugging and you have a mistake there.

Second example can be some data you never counted for, like some API which usually provides integer value, so you handle only integers, but then DST change cames and one hour is missing, so they send NaN string your code is not prepared to handle.

Some bugs are so hidden andmso sporatic, that you may spend weeks just trying to reproduce that failure.

7

u/Rscc10 May 21 '26

Bugs don't mean errors. Bugs are typically more tricky to handle since the program runs just fine at compile time but some functionalities don't work as expected.

These often stem from logic mistakes or oversights. Basically, anytime your program runs without error but doesn't produce the expected output.

Example

listA = \[1,2\]  
listB = listA  
listB.append(3)  
\# List A has 2 elements  
\# List B has 3 elements

for i in range(len(listA)):  
\# Loops through 0 to 1

	for j in range(len(listB)):      
	\# Loops through 0 to 2

		print(i+j)

So I expect to see output
0+0, 0+1, 0+2, 1+0, 1+1, 1+2 or
0,1,2, 1,2,3

Instead I will get
0,1,2, 1,2,3, 2,3,4

Because I forgot about the mutability and pointer-logic of the lists. This is a bug. It works perfectly fine, there's no internal error with the code. It is simply a human error in the logic of the code.

2

u/autoglitch May 21 '26 edited May 21 '26

Suppose you made a function to evenly divide gold to your party members in an online game.

def divy(gold, party_size):
    portion = gold/party_size
    return portion

In most normal uses this will work as expected. But consider the following scenarios:

  • What if it's not an even distribution? Can you break up gold in smaller denominations?

  • What if your party disbanded before you handed out the gold? You would get a divide_by_zero error.

  • What if the "party_size" came from the client and not the server? A hacker could put "party_size=1/2" and double the gold output.

These are all different types of bugs that may not be caught without thorough testing. Many bugs are way more complicated and harder to discover.

1

u/jpgoldberg May 21 '26

You have probably seen the typical factorial example for illustrating recursion, some thing like

def factorial(n: int) -> int:
  if n == 1:
      return 1
  else:
      return n * factorial(n-1)

What happens if a negative number is passed to that factorial function?

You might be thinking that your functions and scripts will only ever be fed on sane, reasonable data, and so you don’t have to worry about stuff like that. But the facto fo the matter is that the functions you write today may end up in use in different situations tomorrow, where malformed input might occur.

What’s worse is that attackers will deliberately set up situations to deliberately feed a system maliciously contrived input.

1

u/JGhostThing May 21 '26

Bugs are the programmer's word for mistakes. In any large program, it is almost impossible to avoid bugs. Basically, you will spend at least half your time debugging. Some of this is proactive, and some is in reaction to users. The worst ones are "This doesn't work!" with no more information.

I worked for a major US university in a non-faculty position. I implemented programs to allow faculty to use computers for their students.

My oldest program, one of the first webapps called CourseWeb was written in Perl because it was pre-java. After seven years, I believe it was finally bug free. It was a fairly large one-programmer process.

1

u/WorkAroundG60 May 23 '26

Our Engineering team is about 70 people, Now, 10 of those 70 are the types you want, they have a passion for it, inside and outside of work. The others 60, out of Comp-Science degrees, in it for the money and it's just a job.

Our product is in 3 countries, and includes government contracts, and is actually 6 products rolled into 1.

We have a 9-10 releases a year,

Each release, we'll have approx 5 major bugs, and a few smaller ones.

For instance, the last release, one on page, users are able to enter notes on a record, but if you hit enter, it would add random characters into it.

Turns out there was something else changed that had a knock on effect.

The reason is engineering is split into different teams that look after different parts of the product, so they don't really consider the overall process. and our "testing team" if you can call it that, are absolutely shit at their job.

With small projects, bugs are rare, but the larger it gets, the more people working on it, the more people using it in ways you didn't expect, bugs will come in to it.

0

u/SUQMADIQ63 May 21 '26

Order of your project maybe. I remember trying to add new defined function into my model but it messed up the order and another example is two different libraries using the same syntax can cause error too even though the code is fine.

0

u/csabinho May 21 '26

For instance: if you use "their" instead of "they're"(and vice versa), it's syntactically correct, but a "bug" in your sentence.

-1

u/JaleyHoelOsment May 21 '26

imagine writing a 20 line script vs a 200k line monolith. there will be bugs more often than not

5

u/[deleted] May 21 '26

[removed] — view removed comment

0

u/tiredITguy42 May 21 '26

Bugs do raise errors and exceptions and they do that very often.