Questions tagged [dead-code]

Dead code is code in the source code of a program which is executed but whose result is never used or a code that can never be reached or used.

Dead code is code in the source code of a program which is executed but whose result is never used or a code that can never be reached or used.

Dead code is waste of computing power and might result of unexpected behavior to the program.

Examples:

int foo (int a, int b)
{
    int c = a + b;  // dead code - executes and result never used 
    return a - b;
}

void foo ()
{
     bool a = true;
     if (!a)
     {
         // dead code - never called
     }
}
238 questions
8
votes
3 answers

.NET find dead code across multiple solutions

We have a product with ~15 solutions with each a number of projects. The question is quite simple: Which tool will enable us to search the entire codebase for dead code? Searching within a single solution is easy enough (Lots of answers on SO for…
8
votes
2 answers

Java Compiler: Stop complaining about dead code

For testing purposes, I often start typing some code in an already existing project. So, my code I want to test comes before all the other code, like this: public static void main(String[] args) { char a = '%'; System.out.println((int)a); …
Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287
7
votes
0 answers

Vite and conditional dead code elimination

I use Vite bundler and in my code I have the following function: function doSomething() { if (!import.meta.env.VITE_SOMETHING) { return; } console.log("Hello"); } I would expect that after building my app (npm run build) without defining…
Martin Ždila
  • 2,998
  • 3
  • 31
  • 35
7
votes
2 answers

Eclipse gives dead code warning for reachable code (variant)

I have following code: public String myMethod(String keyValue) { Map keyValueToRowIndex = ... Integer rowIndex = (keyValue == null) ? 0 : keyValueToRowIndex.get(keyValue); if (rowIndex == null) return null; …
geert3
  • 7,086
  • 1
  • 33
  • 49
7
votes
2 answers

Dead code detection in ruby

Does anyone know of a production worthy package commercial or OSS that can detect which lines of code have been executed or not? We're looking around for some tools that can help us detect dead code in a production environment, running Ruby On Rails…
Daniel
  • 7,006
  • 7
  • 43
  • 49
7
votes
3 answers

How can I detect dead code after always-throwing methods?

consider the following code: @Test public void testDeadCode() { letsThrow(); System.out.println("will never be reached"); } private final void letsThrow() { throw new RuntimeException("guess you didnt see this one coming"); } To me it…
GhostCat
  • 137,827
  • 25
  • 176
  • 248
7
votes
1 answer

Xcode offers strange solution for explicitly dead code in c++?

I'm writing a program that is supposed to solve a sudoko-like puzzle, Hashiwokakero. I have some code that looks like this: if (bridgesLeft[row][col] == 1) { doSomething(); } else if (bridgesLeft[row][col] == 2) { doSomethingElse(); } else if…
DJMcMayhem
  • 7,285
  • 4
  • 41
  • 61
7
votes
3 answers

How do you add verbose logging code to functions without affecting performance?

Performance is important for a certain class I'm writing. I thought about calling a function like so: debug('This is a debug message, only visible when debugging is on'); And the contents would be like function debug(message) { if (DEBUG)…
Jelle De Loecker
  • 20,999
  • 27
  • 100
  • 142
7
votes
4 answers

javac code elimination capabilities

I'm having a hard time finding information about javac's code elimination capabilities: I read that if you have something like the following, the if-statement will be eliminated: static final boolean DEBUG = false; if (DEBUG)…
Markus A.
  • 12,349
  • 8
  • 52
  • 116
7
votes
4 answers

Unwanted Dead Code Warning in Eclipse

The following code gives me a 'Dead Code' warning in Eclipse: private void add(Node n, E element) { Node e = new Node(element); if (n == null) root = e; else if (n.compareTo(e) > 0) if…
golddove
  • 1,165
  • 2
  • 14
  • 32
6
votes
7 answers

Is there a way to locate unused event handlers in Delphi?

Finding dead code in Delphi is usually real simple: just compile and then scan for routines missing their blue dots. The smart linker's very good about tracking them down, most of the time. Problem is, this doesn't work for event handlers because…
Mason Wheeler
  • 82,511
  • 50
  • 270
  • 477
6
votes
2 answers

Getting the use and def of an llvm instruction

I am attempting to carry out liveness analysis and in order to do so I need to obtain the def and use sets for all of my nodes, where they are defined as follows: def[n] = set of all variables defined at node n use[n] = set of all variables used…
Alk
  • 5,215
  • 8
  • 47
  • 116
6
votes
2 answers

Xcode -- finding dead methods in a project

I am curious if there are any tools that provide partial solutions for this. It is a tricky problem because of performSelector . . . but a tool ought to at least be able to come up with candidates, making the human's job easier.
William Jockusch
  • 26,513
  • 49
  • 182
  • 323
6
votes
0 answers

Is it bad to enable Dead Code Stripping?

My iOS project uses dlsym to dynamically point to an optional C library. Optional as-in the project can run with our without it, it just adds features. For background info: Detect and use optional external C library at runtime in Objective-C The…
Nathan H
  • 48,033
  • 60
  • 165
  • 247
6
votes
2 answers

Detect unused functions in C

I'm looking for a way to check if my C project, that compiles to an ELF, has unused functions, and locate them. That is functions that are declared but are not called anywhere in my code. The solution can be one of: A utility that goes through my…
speller
  • 1,641
  • 2
  • 20
  • 27
1 2
3
15 16