Debug code

Last updated

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.

Contents

Uses of debug code

Debug code's main function is to help debug code. This can do this in several ways, such as using print statements, assert commands and unit testing.

Use in coding

Small statements can be added to code in order to find the presence and the location of bugs within a program. It can also be used to provide test inputs to simulate possible use cases that a program might need to be able to accept. It can also be used as a place holder for code that is still in development.

Use in video games

Many video gaming mod, cheat codes, such as level cheat code, 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.

It is recommended as a best practice that debugging code be removed from production versions of applications, as it can slow them down. [1] 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 The Elder Scrolls V: 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. These commands include giving the player invincibility, teleportation and unlimited gold. [2]

Examples of debug code

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. It is temporary and usually removed when the bug is solved. The use of many print statements can affect the actual output of a program and slow down the run-time, depending on how often print statements are called. In some cases print statements do not help find the problem, for example the C++ stdout has a buffered output, and sometimes the contents of the buffer are lost leading to incorrect debugging information. [3]

C++ example

voidTestFunction(inttimesToRun){cout<<"the algorithm should run "<<timesToRun<<" times"<<std::endl;for(inti=0;i<=timesToRun;i++){// run algorithmalgorithm();// debug print statementcout<<"algorithm run "<<i++<<" times."<<std::endl;}}

There is a bug in the above code. On an input of 5 the program should print the following to the console.

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. 

The actual output is the following, which is incorrect.

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. 

Our function is running through the algorithm an extra time, and upon closer inspection it is clear that our loop is coded wrong.

Assert statements

Usually the best time to fix a bug is before the program is run. This can be done by inserting assertions into the code. In C this can be done using the assert() command. An assert command can check to see if the program is running the correct conditions at this point in the program. [4]

C example

inti,a[10];for(i=0;i<10;++i){a[i]=10-i;}for(i=0;i<10;++i){a[a[i]]=a[i];}

The above code will cause has an out of bounds error which can lead to some unexpected results. The code can be written in a safer way, using assertions, as shown below.

#include<assert.h>inti,a[10];for(i=0;i<10;++i){assert(0<=i&&i<10);a[i]=10-i;}for(i=0;i<10;++i){assert(0<=i&&i<10);assert(0<=a[i]&&a[i]<10);a[a[i]]=a[i];}

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 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. [5]

Related Research Articles

Computer programming or coding is the composition of sequences of instructions, called programs, that computers can follow to perform tasks. It involves designing and implementing algorithms, step-by-step specifications of procedures, by writing code in one or more programming languages. Programmers typically use high-level programming languages that are more easily intelligible to humans than machine code, which is directly executed by the central processing unit. Proficient programming usually requires expertise in several different subjects, including knowledge of the application domain, details of programming languages and generic code libraries, specialized algorithms, and formal logic.

<span class="mw-page-title-main">GNU Debugger</span> Source-level debugger

The GNU Debugger (GDB) is a portable debugger that runs on many Unix-like systems and works for many programming languages, including Ada, Assembly, C, C++, D, Fortran, Haskell, Go, Objective-C, OpenCL C, Modula-2, Pascal, Rust, and partially others.

<span class="mw-page-title-main">Quine (computing)</span> Self-replicating program

A quine is a computer program that takes no input and produces a copy of its own source code as its only output. The standard terms for these programs in the computability theory and computer science literature are "self-replicating programs", "self-reproducing programs", and "self-copying programs".

In computer programming, an infinite loop is a sequence of instructions that, as written, will continue endlessly, unless an external intervention occurs, such as turning off power via a switch or pulling a plug. It may be intentional.

In object-oriented programming, the decorator pattern is a design pattern that allows behavior to be added to an individual object, dynamically, without affecting the behavior of other instances of the same class. The decorator pattern is often useful for adhering to the Single Responsibility Principle, as it allows functionality to be divided between classes with unique areas of concern as well as to the Open-Closed Principle, by allowing the functionality of a class to be extended without being modified. Decorator use can be more efficient than subclassing, because an object's behavior can be augmented without defining an entirely new object.

Template metaprogramming (TMP) is a metaprogramming technique in which templates are used by a compiler to generate temporary source code, which is merged by the compiler with the rest of the source code and then compiled. The output of these templates can include compile-time constants, data structures, and complete functions. The use of templates can be thought of as compile-time polymorphism. The technique is used by a number of languages, the best-known being C++, but also Curl, D, Nim, and XL.

Unit testing, a.k.a. component or module testing, is a form of software testing by which isolated source code is tested to validate expected behavior.

In computer programming, specifically when using the imperative programming paradigm, an assertion is a predicate connected to a point in the program, that always should evaluate to true at that point in code execution. Assertions can help a programmer read the code, help a compiler compile it, or help the program detect its own defects.

<span class="mw-page-title-main">D (programming language)</span> Multi-paradigm system programming language

D, also known as dlang, is a multi-paradigm system programming language created by Walter Bright at Digital Mars and released in 2001. Andrei Alexandrescu joined the design and development effort in 2007. Though it originated as a re-engineering of C++, D is now a very different language drawing inspiration from other high-level programming languages, notably Java, Python, Ruby, C#, and Eiffel.

<span class="mw-page-title-main">Foreach loop</span> Control flow statement for traversing items in a collection

In computer programming, foreach loop is a control flow statement for traversing items in a collection. foreach is usually used in place of a standard for loop statement. Unlike other for loop constructs, however, foreach loops usually maintain no explicit counter: they essentially say "do this to everything in this set", rather than "do this x times". This avoids potential off-by-one errors and makes code simpler to read. In object-oriented languages, an iterator, even if implicit, is often used as the means of traversal.

Code injection is a class of computer security exploits in which a vulnerable computer program is tricked into misinterpreting external data as part of its code. An attacker thereby introduces code into the program and changes the course of its execution. The result of successful code injection can be disastrous, for example, by allowing computer viruses or computer worms to propagate.

In computing, an uninitialized variable is a variable that is declared but is not set to a definite known value before it is used. It will have some value, but not a predictable one. As such, it is a programming error and a common source of bugs in software.

Hard coding is the software development practice of embedding data directly into the source code of a program or other executable object, as opposed to obtaining the data from external sources or generating it at runtime.

In computer programming, unreachable code is part of the source code of a program which can never be executed because there exists no control flow path to the code from the rest of the program.

In computer programming, an entry point is the place in a program where the execution of a program begins, and where the program has access to command line arguments.

Compilation error or compile error refers to a state when a compiler fails to compile a piece of computer program source code, either due to errors in the code, or, more unusually, due to errors in the compiler itself. A compilation error message often helps programmers debugging the source code. Although the definitions of compilation and interpretation can be vague, generally compilation errors only refer to static compilation and not dynamic compilation. However, dynamic compilation can still technically have compilation errors, although many programmers and sources may identify them as run-time errors. Most just-in-time compilers, such as the Javascript V8 engine, ambiguously refer to compilation errors as syntax errors since they check for them at run time.

In engineering, debugging is the process of finding the root cause of and workarounds and possible fixes for bugs.

Random testing is a black-box software testing technique where programs are tested by generating random, independent inputs. Results of the output are compared against software specifications to verify that the test output is pass or fail. In case of absence of specifications the exceptions of the language are used which means if an exception arises during test execution then it means there is a fault in the program, it is also used as a way to avoid biased testing.

Although C++ is one of the most widespread programming languages, many prominent software engineers criticize C++ for being overly complex and fundamentally flawed. Among the critics have been: Robert Pike, Joshua Bloch, Linus Torvalds, Donald Knuth, Richard Stallman, and Ken Thompson. C++ has been widely adopted and implemented as a systems language through most of its existence. It has been used to build many pieces of very important software.

pytest Software testing framework

Pytest is a Python testing framework that originated from the PyPy project. It can be used to write various types of software tests, including unit tests, integration tests, end-to-end tests, and functional tests. Its features include parametrized testing, fixtures, and assert re-writing.

References

  1. "Archived copy". Archived from the original on 2010-04-02. Retrieved 2010-03-26.{{cite web}}: CS1 maint: archived copy as title (link)
  2. Gamer, P. C. (10 November 2021). "Skyrim console commands: Become a giant, a ghost, or a living god". PC Gamer.
  3. "Debugging techniques". oopweb.com. Archived from the original on 2002-08-17.
  4. "V-Business Card".
  5. "Home". junit.org.