User:Tommyht1/sandbox

Debug code is computer code introduced to a computer program to test for errors or to help determine the cause of an error. It can be as simple as an echo command to print the value of a variable at certain points of a program. Modern integrated development environments sometimes render this unnecessary by allowing the placement of stop points at specific places in the program, and providing the ability to view the value of variables through the IDE rather than program output.

Function
Debug code's main function is to help the programmer debug code. It can do this in several ways. One way is to show the programmer when their program fails, and on which command the program failed on. This can be done by using print statements or assert statements in the program to be run in order to check your code.

Video games
Many videogame cheat codes, such as level select, invincibility, etc. were originally introduced as debug code to allow the programmers and/or testers to skip hindrances that would prevent them from rapidly getting to parts of the game that needed to be tested; and in these cases cheat modes are often referred to as debugging mode. Embedded systems sometimes contains debugging code; an example is the Background Debug Mode interface.

It is recommended as a best practice that debugging code be removed from production versions of applications, as it can slow them down. However some games leave these commands and cheats available for the players to use as a way to enhance their play experience. For example the pc version of Skyrim allows the player access to the command console, giving them the ability to modify certain aspects of their game as it is being run live. These commands include giving the player invincibility, teleportation and unlimited gold.

Print debugging
Print debugging is making use of print statements in order to find and isolate bugs in a program. It can be used to track the flow of data values of a piece of code. This type of debug code has some distinct disadvantages. The code is very ad hoc. It is temporary and usually removed when the bug is solved, leading to better ways to add debugging information to your program. The use of many print statements can affect the actual output of your program and slow down the run-time of your program, depending on how often the print statements are called. Sometimes print statements do not help find the problem, in C++ stdout has a buffered output, and sometimes the contents of the buffer are lost, and you will lose important information causing you to incorrectly fix the problem.

C++ Example
Here we see that that there is a bug in our program. If our input is 5 we expect our output to be something like the following.

the algorithm should run 5 times algorithm run 1 times algorithm run 2 times algorithm run 3 times algorithm run 4 times algorithm run 5 times

However what we actually get is the following.

the algorithm should run 5 times algorithm run 1 times algorithm run 2 times algorithm run 3 times algorithm run 4 times algorithm run 5 times algorithm run 6 times

We can conclude that our function is running through the algorithm and extra time, and upon close inspection we see that our for loop is implemented incorrectly for what we want to do.

Assert Statements
Usually the best time to fix a bug is before you run your program. This can be done by inserting assertions into your code. In C this can be done using the assert command. by calling an assert you can check to see if you program is running with what you think should be the correct conditions at this point in the program.

C Example
The above code will cause has a bug that will cause an out of bounds error which can lead to some unexpected results. The code can be written to be safer by using assertions as shown below.

JUnit
JUnit is a simple framework used to write repeatable test available for java, and allows programmers to create their own unit test. A unit test is code that is written to execute a specific function in the code to be tested and usually targets a small unit of code, such a a single method or class. Using a combination of assert statements and other test statements, programmers can create suites of test cases in order to tell if a method or function is being executed properly.