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 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 is the process of performing a particular computation, usually by designing/building an executable computer program. Programming involves tasks such as analysis, generating algorithms, profiling algorithms' accuracy and resource consumption, and the implementation of algorithms. The source code of a program is written in one or more languages that are intelligible to programmers, rather than machine code, which is directly executed by the central processing unit. The purpose of programming is to find a sequence of instructions that will automate the performance of a task on a computer, often for solving a given problem. Proficient programming thus usually requires expertise in several different subjects, including knowledge of the application domain, specialized algorithms, and formal logic.

GNU Debugger 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, C, C++, Objective-C, Free Pascal, Fortran, Go, and partially others.

A "Hello, World!" program is generally a computer program that outputs or displays the message "Hello, World!". This program is very simple to write in many programming languages, and is often used to illustrate a language's basic syntax. "Hello, World!" programs are often the first a student learns to write in a given language, and they can also be used as a sanity test to ensure computer software intended to compile or run source code is correctly installed, and that its operator understands how to use it.

Quine (computing) Self-replicating program

A quine is a computer program which 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".

OCaml is a general-purpose, multi-paradigm programming language which extends the Caml dialect of ML with object-oriented features. OCaml was created in 1996 by Xavier Leroy, Jérôme Vouillon, Damien Doligez, Didier Rémy, Ascánder Suárez, and others.

In computer programming, an infinite loop is a sequence of instructions that, as written, will continue endlessly, unless an external intervention occurs. It may be intentional.

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.

In computer programming, unit testing is a software testing method by which individual units of source code—sets of one or more computer program modules together with associated control data, usage procedures, and operating procedures—are tested to determine whether they are fit for use.

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.

D (programming language) 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 a distinct language. It has redesigned some core C++ features, while also sharing characteristics of other languages, notably Java, Python, Ruby, C#, and Eiffel.

Code injection Xss attack vectors how to exploit and take advantage of and steal cookies

Code injection is the exploitation of a computer bug that is caused by processing invalid data. The injection is used by an attacker to introduce code into a vulnerable computer program and change the course of execution. The result of successful code injection can be disastrous, for example, by allowing computer viruses or computer worms to propagate.

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. Hard-coded data typically can only be modified by editing the source code and recompiling the executable, although it can be changed in memory or on disk using a debugger or hex editor. Data that are hard-coded is best for unchanging pieces of information, such as physical constants, version numbers and static text elements. Softcoded data, on the other hand, encode arbitrary information through user input, text files, INI files, HTTP server responses, configuration files, preprocessor macros, external constants, databases, command-line arguments, and are determined at runtime.

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

In the C++ programming language, argument-dependent lookup (ADL), or argument-dependent name lookup, applies to the lookup of an unqualified function name depending on the types of the arguments given to the function call. This behavior is also known as Koenig lookup, as it is often attributed to Andrew Koenig, though he is not its inventor.

assert.h is a header file in the standard library of the C programming language that defines the C preprocessor macro assert . In C++ it is also available through the <cassert> header file.

Compilation 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, it is important to note that 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.

The erase–remove idiom is a common C++ technique to eliminate elements that fulfill a certain criterion from a C++ Standard Library container.

In computer programming and software development, debugging is the process of finding and resolving bugs within computer programs, software, or systems.

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.

The C++ programming language is one of the most widespread programming languages, however; despite its widespread use, many well known reputable software engineers criticize C++ for being overly complex, and fundamentally flawed. C++ was 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. Because of C++'s significant role as a software language, and how essential software has become for humanity, any flaws that the language has is an essential topic. This article attempts to list the flaws, and the arguments made in the discussions surrounding the flaws.

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. http://oopweb.com/CPP/Documents/DebugCPP/Volume/techniques.html [ dead link ]
  4. "V-Business Card".
  5. http://junit.org/