We do not write our code randomly. When we program, we always have a mental understanding on how the code is supposed to do. We write code to reflect our intentions, and our understanding of our program is exactly that of our intentions.
The bugs are the reality that our program do not always match our understanding.
Bugs are almost unavoidable because the program become more complex, it is more difficult to comprehend.
There are two solutions to this understanding problem. One is to delegate. We use third party libraries and trust them to be bug free. This creates two additional problems. First, it is impossible to really understand a library that is hidden behind an abstraction layer. Second, the trust is never absolute. The libraries always contains bugs themselvs.
The second solution is to try our best to comprehend the program anyway. How do we comprehend complex things? We organize the information in a way that we can comprehend piece by piece.
Programming is all about orgnization.