2

We have a project with a lot of code, part of it is legacy. As part of the work flow, every once in a while, all the functionality of the product is checked. I wonder if there is a way to use this fact to dynamically check which parts of the code were never used? (The difficult part is the C++ code, the .Net and Java are more under control and have less legacy).

Also - are there dynamic dead code elimination tools are there that can work with lots of code and complex projects (i.e. ~1M lines)?

All the similar questions I found talked about static analysis which we all ready do.

Thank you!

borod108
  • 766
  • 1
  • 6
  • 16
  • What exactly do you mean with **'dynamic'** code elimination? – πάντα ῥεῖ Nov 15 '12 at 10:32
  • Do a search for "code coverage". – Some programmer dude Nov 15 '12 at 10:38
  • What "static analysis" have you done? This should include "code coverage" and show already dead/unused code. – Olaf Dietsche Nov 15 '12 at 10:39
  • If you talk of finding dead code - yes, there are (commercial and non-commercial) tools that can do this, also for complex projects. Wikipedia has a list of them, look for static analysis. – Zane Nov 15 '12 at 11:10
  • Thanks for the replays, 'dynamic' means that if I have reachable code that is never used I will never know from static analysis. We use Coverity it is a really good tool for static analysis. I finally came to the same answer Kirill Kobelev gave. – borod108 Nov 19 '12 at 08:33
  • How good is Coverity at detecting infeasible paths, e.g., that that can *never* be executed no matter what the input is? This requires the construction of a symbolic formula for each conditional, computing their conjunction, and then proving the whole condition is true; not something I thought Coverity was good at. – Ira Baxter Nov 22 '12 at 04:49
  • .... the question about what code doesn't get executed may not be a property of your code, but the organization context in which it sits. "I'm sorry sir, the company doesn't accept checks over $100K" means your handle-$1M check never gets executed, but not because of the code. This is called a dead business rule. – Ira Baxter Nov 22 '12 at 04:50

2 Answers2

3

You might want to look at the code coverage tools that are used in testing. The idea of these tools is that they instrument the code and after running the set of tests you know what lines of code were executed at least once and what lines were never executed. After that you can improve tests.

The same thing can be used to identify dead code in case if you have diverse enough execution environment.

Kirill Kobelev
  • 10,252
  • 6
  • 30
  • 51
  • I think you are right, so it seems there is no real solution unless you are willing to spend months of human hours of testers who will try out every possible scenario or write full test automation (which is good to have any way, but not always possible in practice). I kinda thought that but was hoping someone might have some magical framework which you can maybe attach to your product and then check the logs automatically to see which code was never used for the last year or something. would be nice to have such framework... – borod108 Nov 19 '12 at 08:32
  • 2
    @borod108: "No real solution"? A test coverage tool is a quite real "magical framework" you want. You instrument the code, and run it. A good test coverage tool adds extremely low overhead; you can afford to run it on production code. At the end of months, you gets "months" of test coverage data, and you find out what code got dynamically executed. The unexecuted code is the stuff you seem interested in. – Ira Baxter Nov 22 '12 at 04:46
  • I do confirm that code coverage can be used in production environment. Thanks, Ira. – Kirill Kobelev Nov 22 '12 at 17:40
0

I don't know what platform you are on but we have used Gcov with success if you're compiling with the gnu toolchain:

http://gcc.gnu.org/onlinedocs/gcc/Gcov.html

Andrew Tomazos
  • 66,139
  • 40
  • 186
  • 319