Redundant code

from Wikipedia, the free encyclopedia

In programming, redundant code is the term for the source code of a computer program or parts of it that are redundant ('superfluous').

In technology in particular, “redundant” generally refers to multiple existing components . In relation to the program code, redundant code means source code parts ( source code clones ) that are duplicated .

As a rule, “redundant code” is understood to mean text copies in the instruction section of a program. In principle, redundancies (like dead code ) are also possible in the data definitions of a program.

Code redundancy can also occur across different programs - if, for example, identical functional parts are programmed / coded individually in several programs instead of being called up as a subroutine or integrated using an include statement.

Different meaning / delimitation :
Even dead code is sometimes referred to as redundant code because it appears to be formally 'superfluous', but is not redundant i. S. from 'several times available'.

meaning

All forms of redundant codes are considered undesirable or lack in software quality for various reasons . Redundant code wastes CPU time that other threads lack. In addition, duplicate code wastes memory and can lead to unnecessary caching of instructions in the CPU's instruction cache . Redundant code reduces maintainability , increases the effort to document code and leads to more errors. It also often negatively impacts metrics and makes program verification difficult .

Depending on the situation, redundant code can also arise deliberately : It is intended to preserve a temporary or former part of the source text, for example, is kept several times for certain reasons (see source code clone ), has no disadvantageous meaning given the advantages (e.g. with very small code fragments) - or there is a program error (especially dead / inaccessible code), the detection of which is a goal in the software test .

example

1 int foo (int x) {
2     int y = 100 / x;  // Code löst bei x = 0 eine Exception aus und beendet das Programm
3     int z = x * x;    // Berechnung, die zwei Zeilen weiter unten noch Mal ausgeführt wird
4     if (z >= 0)
5         return x * x; // redundante Berechnung, optimieren Compiler heraus, nennt sich Common Subexpression Elimination
6     return -1;        // Code wird erreicht für Inputs 46341…65535, 80265…92681, 103622…113511, 122607…131071, ... (Annahme: 32 bit int)
7 }
  • Dead Code - In the example, the number 100 is xdivided by in line 2 , but the result is never used. It is therefore dead code. However, if x is zero , an exception is thrown. Removing this code leads to a change in functionality. Since raising exceptions should never be part of the functionality, it is a faulty functionality that must be removed.
  • Redundant code - In the example, xlines 3 and 5 are squared without changing anything in xbetween. The result is always the same, so the code is redundant. return zwould therefore be appropriate in line 5.
  • Unreachable code - In the example, line 7 is never reached because the query z >= 0in line 4 is always true and the method is exited in line 5. Line 7 is therefore unreachable code.
  • Unnecessary Code - Since the query z >= 0on line 4 is always true, it is pointless and can be removed as well. Unnecessary code does not fall into any of the three categories mentioned, but is commonly referred to as dead code.

reasons

Redundant code is created, among other things, by

  • Programming errors in complex conditional branches;
  • a sequence of internal conversions performed by an optimizing translator;
  • incomplete testing of a new or changed program
  • incomplete maintenance, such as the subsequent dismantling of temporary special regulations without understanding the old functions.

analysis

Discovering redundant code is a form of static code analysis and requires a detailed flow control analysis to find the code independent of the variables and other runtime conditions. With the help of suitable analysis tools, redundant code parts can be found.

Individual evidence

  1. Saumya K. Debray, William Evans, Robert Muth, Bjorn De Sutter: Compiler techniques for code compaction . In: ACM Transactions on Programming Languages ​​and Systems (TOPLAS) . tape 22 , no. 2 , March 2000, p. 378-415 , doi : 10.1145 / 349214.349233 (English).
  2. computerlexikon.com 2012: More often available than actually needed
  3. neuewort.de Examples with the meaning 'multiple'
  4. linguee.de all examples mean 'multiple'
  5. ^ David Noel Card, Victor E Church, William W. Agresti: An empirical study of software design practices . In: IEEE Transactions on Software Engineering . tape 12 , no. 2 , February 1986, ISSN  0098-5589 , pp. 264-271 (English). "Table X further implies that a high proportion of unreferenced local and calling sequence variables signifies sloppy workmanship leading to high cost and fault rates."