A computer bug is an error in a computer program that prevents it from doing what it was intended to do. The term derives from hardware engineering, in which hardware errors are also referred to as bugs.
Typically, bugs arise from mistakes in the source code of a computer program. However, they can also arise from either faulty software design. Almost all computer programs contain bugs of some kind.
Bugs have widely varying effects on computer programs. Some bugs do not prevent the program from working, and may therefore lie undetected for a long time. Other bugs may cause the program to crash or freeze. In some operating systems, such as Microsoft Windows, crashing or freezing programs may render the computer unusable until it is rebooted; see Blue Screen of Death.
The cost of bugs may be considerable. Steve McConnell recounts several bugs that cost over $100 million. The most spectacular case was Europe's [[[Ariane 5]] rocket, which cost over $1 billion. It was destroyed shortly after launch, because of a bug in the on-board guidance computer.
It became clear early in the history of computers that finding and fixing bugs would be a major part of computer programming. Maurice Wilkes, an early computing pioneer, describes his realization in the late 1940s that much of the rest of his life would be spent finding mistakes in his own programs. He was proven correct; as computer programs grew more complex, bugs become more common and difficult to fix. Often, computer programmers spend more time and effort finding and fixing bugs than writing new code.
The proliferation of bugs is a natural consequence of the nature of the programming task. Computer programs often are very complex, and it is impossible for programmers to mentally keep track of every detail. One very common type of error occurs when the input to a section of a program violates the assumptions (be they explicit or implicit) of the implementer or designer of that section. Much effort has gone into design and programming techniques to reduce the frequency of this error type.
The academic discipline of software engineering has evolved as a response to the challenge of improving software quality, which in part means dealing with computer bugs and reducing their associated costs. (Software engineering also deals with more fundamental software design issues, which, as mentioned above, are not generally categorized as "bugs".) Modern computer languages, programming environments, common programming techniques and development methodologies include many of the lessons learned.
For example, most programmers are taught to write their programs in such a way that they can recover from some bugs, or at least detect their presence and inform the user of their existence. Programming language features such as exception handling support this concept. Modern languages also attempt to eliminate features that are known to be bug-prone - for instance the Java programming language does not support pointer arithmetic.
Once bugs have been reported, it is the task of the programmer to locate and fix them. Such bug fixing is an art, and whilst guidelines have been developed to assist novice programmers to learn this skill, it generally comes with experience. Generally, the first step in locating a bug is finding a way to reproduce it easily. Some bugs are very difficult to reproduce - for instance, some bugs disappear when the program is run under the microscope of a debugger. Once reproduced, the programmer usually uses specialised tools to monitor the execution of the program in the faulty region and figures out precisely what is causing the problem.